1、Elasticsearch搜索数据有两种方式。

一种方式是通过REST请求URI,发送搜索参数;

另一种是通过REST请求体,发送搜索参数。而请求体允许你包含更容易表达和可阅读的JSON格式。这个是DSL查询(Query DSL).

2、Elasticsearch搜索基本语法。

[GET|POST] http://domain.com/your_index/type1,type2/_search{?search_type=count|scan|...}

  注意,随着ES版本变化,搜索语法也有小调整。本文以5.3为准。

  • 全基于rest式http调用。 其中GET方法支持在body传参数。
  • _search 是关键字,以此结束表示搜索行为,可以同时搜索多个index与type。
  • search_type值现在有query_then_fetch和 dfs_query_then_fetch两中选择,默认是query_then_fetch;资料信息请参考:IT虾米网
  • body部分必需是json;同时支持 URL 中使用query_string传参;
  • 搜索请求会以query_string参数优先,且在 URL 中的参数有可能使用简写,如q==query。
  • 可以同时索引多个index或type,逗号隔开,或直接使用通配符。
  • 更丰富官方索引文档在此 IT虾米网

3、搜索例子。

1)查询单个文档

格式: get /_index/_type/_id  
例如:http://localhost:9200/library/article/57508457556e30622882ba58  
说明:查询索引是library下面的,类型是article的,_id是57508457556e30622882ba58的这个文档。 
{ 
  "_index":"library", //索引 
  "_type":"article", //类型 
  "_id":"57508457556e30622882ba58", //文档id(这个id在导入时不指定会自动生成) 
  "_version":1, //版本号,每次改动会+1 
  "found":true, //true表示document存在 
  "_source":{ //文档全部内容 
    "id":"57508457556e30622882ba58", 
    "title":"Adipisicing pariatur quis magna do et.", 
    "journal":"quis nostrud", 
    "volume":61, 
    "number":11, 
    "pages":"42-59" 
    ... 
  } 
}

2)以query_string参数查询

// 查询标题title中含有pariatur的数据 
get http://localhost:9200/library/article/_search?q=title:pariatur 
{ 
  "took": 8,  //查询花费的时间,单位毫秒 
  "timed_out": false, //查询是否超时 
  "_shards": {   //描述分片的信息 
    "total": 5,  //查询了多少个分片 
    "successful": 5,  //成功的分片数量 
    "failed": 0     //失败的分片数量 
  }, 
  "hits": {  //搜索的结果 
    "total": 145,  //全部的满足条件的文档数目 
    "max_score": 2.8908465,  //文档最大得分 
    "hits": [  
      { 
        "_index": "library",  //索引 
        "_type": "article",  //类型 
        "_id": "57508457556e30622882ba58", //文档id 
        "_score": 2.8908465,  //文档的分数信息,与排名相关度有关,参考各大搜索引擎的搜索结果 
        "_source": {  //文档原数据 
          "id": "57508457556e30622882ba58", 
          "title": "Adipisicing pariatur quis magna do et.", 
          "journal": "quis nostrud", 
          "volume": 61, 
          "number": 11,

3)使用DSL查询,提交JSON格式参数。 

//实现上面例子一样的查询结果,可以用工具ElasticSearch Toolbox测试效果 
请求:get http://localhost:9200/library/article/_search 
参数: 
{ 
  "query": { 
    "match": { 
      "title": "pariatur" 
    } 
  }, 
  "from": 0, //表示从第几行开始(默认0)  
  "size": 10 //表示查询多少条文档(默认10) 
}  
//注意:如果搜索size大于10000,需要设置index.max_result_window参数,size的大小不能超过index.max_result_window这个参数的设置,默认为10,000。

4)多条件查询 bool:must、filter、should

官方例子是:Query-DSL-bool-query

{ 
  "query": { 
    "bool" : {  //成为query的过滤器,还有其它的,如:and,or,not,limit 
      "must" : {  //must,filter,should为过滤条件,如果有多个子条件,使用[] 
        "term" : { "user" : "kimchy" } 
      }, 
      "filter": { 
        "term" : { "tag" : "tech" } 
      }, 
      "must_not" : { 
        "range" : { 
          "age" : { "gte" : 10, "lte" : 20 } 
        } 
      }, 
      "should" : [ 
        { "term" : { "tag" : "wow" } }, 
        { "term" : { "tag" : "elasticsearch" } } 
      ], 
      "minimum_should_match" : 1,  //用于现在should的子条件匹配个数。 
      "boost" : 1.0 //表示此过滤器的权重,默认1.0 
    } 
  } 
}

由于 Query-DSL 查询语言过于复杂,关键字非常多,需要再用到时查询文档。

官网帮助文档地址 :IT虾米网


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!