搜索初步(search lite)

ES的GET动作是相当简单的。现在就来点高级点的,搜索。

首先看看最简单的搜索,搜索所有的员工信息,

curl -XGET 'http://localhost:9200/megacorp/employee/_search?pretty'

你看到了,我们使用了megacorp这个index和employee这个type,使用了_search而不是指定的文档ID,返回的结果hits集合中包含了所有的三个document。在默认情况下ES会返回前10条数据。

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "megacorp",
      "_type" : "employee",
      "_id" : "3",
      "_score" : 1.0, "_source" : {"first_name" : "Douglas","last_name" : "Fir","age" : 35,"about": "I like to build cabinets","interests": [ "forestry" ]}
    }, {
      "_index" : "megacorp",
      "_type" : "employee",
      "_id" : "1",
      "_score" : 1.0, "_source" : {"first_name" : "John","last_name" : "Smith","age" : 25,"about" : "I love to go rock climbing","interests": [ "sports", "music" ]}
    }, {
      "_index" : "megacorp",
      "_type" : "employee",
      "_id" : "2",
      "_score" : 1.0, "_source" : {"first_name" : "Jane","last_name" : "Smith","age" : 32,"about" : "I like to collect rock albums","interests": [ "music" ]}
    } ]
  }
}

这个结果不仅仅告诉我们那些document是匹配的,同时也包含了整个document,所有的信息都在结果中展示给用户。

接下来,尝试一下搜索姓中包含“Smith”的员工信息,这里在命令行下使用了一个轻量级的搜索方式,这个方式常常用于对一个字符串的搜索:

curl -XGET 'http://localhost:9200/megacorp/employee/_search?q=last_name:Smith&pretty'

这里依然使用了_search关键字结束路径,并且使用关键字q=添加搜索参数。结果如下:

{
   
...
   
"hits":{
     
"total":      2,
     
"max_score":  0.30685282,
     
"hits":[
         
{
           
...
           
"_source":{
               
"first_name":  "John",
               
"last_name":   "Smith",
               
"age":         25,
               
"about":       "I love to go rock climbing",
               
"interests":["sports","music"]
           
}
         
},
         
{
           
...
           
"_source":{
               
"first_name":  "Jane",
               
"last_name":   "Smith",
               
"age":         32,
               
"about":       "I like to collect rock albums",
               
"interests":["music"]
           
}
         
}
     
]
   
}
}

 

原文:http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/_search_lite.html

posted @ 2014-05-08 14:43  QQ1350995917  Views(312)  Comments(0)    收藏  举报