Elasticsearch 基本查询学习

jopen 8年前

简单查询

在 Elasticsearch 当中最简单的查询是使用 URI 请求查询,例如下面的查询:

http :9200/test/_search q==name:rcx    {      "query" : {          "query_string" : {"query" : "name:rcx"}      }  }

上面的这两个查询返回的结果是相同的,上面的是 URI 方式的查询,下面的是 DSL 查询。

当然如果是需要分页查询的话可以进行如下方式:

{      "from" : 10,      "size" : 10,      "query" : {          "query_string" : {"query" : "name:rcx"}      }  }

也可以添加如下的参数:

{      "from" : 10,      "size" : 10,      "version" : true,//返回文档的版本信息      "min_score" : 0.75,//查询返回的文档得分高于0.75的      "fields" : ["title", "age"],//查询返回的字段      "query" : {          "query_string" : {"query" : "name:rcx"}      }  }

理解查询过程

搜索类型

  • query_then_fetch :第一步,执行查询得到对文档进行排序和分级所需要信息,在所有分片上执行。然后,只在相关分片上查询文档的实际内容。返回结果的最大数量是 size 参数的值。这个类型是默认的查询类型。
  • query_and_fetch :查询在所有分片上并行执行,所有分片返回等于 size 值的结果数。返回文档的最大数等于 size 乘以 分片的数量。
  • dfs_query_and_fetch :与 query_and_fetch 类似,在初始查询中执行分布式词频的计算,以得到返回文件的更精确的得分,从而让查询结果更想相关。
  • dfs_query_then_fetch :与 query_then_fetch 类似,在初始查询中执行分布式词频的计算,以得到返回文件的更精确的得分,从而让查询结果更想相关。
  • count :特殊搜索,只返回匹配查询的文档数。
  • scan :在发送第一个请求后,响应一个滚动标识符,类似于数据库当中的游标。

基本查询

词条查询

它仅匹配在给定字段中含有该词条的文档,而且是确切的、未经分析的词条。请注意是未经过分析的。

http :9200/test/bulk/_search    {      "_shards": {          "failed": 0,          "successful": 5,          "total": 5      },      "hits": {          "hits": [              {                  "_id": "2",                  "_index": "test",                  "_score": 1.0,                  "_source": {                      "title": "this is a new titld"                  },                  "_type": "bulk"              },              {                  "_id": "1",                  "_index": "test",                  "_score": 1.0,                  "_source": {                      "title": "this is a new title"                  },                  "_type": "bulk"              },              {                  "_id": "3",                  "_index": "test",                  "_score": 1.0,                  "_source": {                      "title": "this is a bad title"                  },                  "_type": "bulk"              }          ],          "max_score": 1.0,          "total": 3      },      "timed_out": false,      "took": 2  }      http :9200/test/bulk/_search query:='{"term":{"title":"title"}}'  {      "_shards": {          "failed": 0,          "successful": 5,          "total": 5      },      "hits": {          "hits": [              {                  "_id": "1",                  "_index": "test",                  "_score": 0.4375,                  "_source": {                      "title": "this is a new title"                  },                  "_type": "bulk"              },              {                  "_id": "3",                  "_index": "test",                  "_score": 0.13424811,                  "_source": {                      "title": "this is a bad title"                  },                  "_type": "bulk"              }          ],          "max_score": 0.4375,          "total": 2      },      "timed_out": false,      "took": 2  }    http :9200/test/bulk/_search query:='{"term":{"title":"this is"}}'  {      "_shards": {          "failed": 0,          "successful": 5,          "total": 5      },      "hits": {          "hits": [],          "max_score": null,          "total": 0      },      "timed_out": false,      "took": 1  }

多词条查询

http :9200/test/bulk/_search query:='{"terms":{"title":["this", "is"]}}'    {      "_shards": {          "failed": 0,          "successful": 5,          "total": 5      },      "hits": {          "hits": [              {                  "_id": "1",                  "_index": "test",                  "_score": 0.61871845,                  "_source": {                      "title": "this is a new title"                  },                  "_type": "bulk"              },              {                  "_id": "2",                  "_index": "test",                  "_score": 0.18985549,                  "_source": {                      "title": "this is a new titld"                  },                  "_type": "bulk"              },              {                  "_id": "3",                  "_index": "test",                  "_score": 0.18985549,                  "_source": {                      "title": "this is a bad title"                  },                  "_type": "bulk"              }          ],          "max_score": 0.61871845,          "total": 3      },      "timed_out": false,      "took": 7  }

match_all 查询

如果想查询索引中的所有文档,只需要如下查询:

{      "query" : {          "match_all" : {}      }  }

match 查询

match 查询把 query 参数中的值拿出来,加以分析,然后构建对应的查询。使用 match 查询时,ElasticSearch 将对一个字段选择合适的分析器。

http :9200/test/bulk/_search query:='{"match":{"title":"this is"}}'    {      "_shards": {          "failed": 0,          "successful": 5,          "total": 5      },      "hits": {          "hits": [              {                  "_id": "1",                  "_index": "test",                  "_score": 0.61871845,                  "_source": {                      "title": "this is a new title"                  },                  "_type": "bulk"              },              {                  "_id": "2",                  "_index": "test",                  "_score": 0.18985549,                  "_source": {                      "title": "this is a new titld"                  },                  "_type": "bulk"              },              {                  "_id": "3",                  "_index": "test",                  "_score": 0.18985549,                  "_source": {                      "title": "this is a bad title"                  },                  "_type": "bulk"              }          ],          "max_score": 0.61871845,          "total": 3      },      "timed_out": false,      "took": 7  }

match 查询的几种类型:

布尔值匹配查询

布尔值匹配查询分析提供的文本,然后做出布尔查询,有如下参数可以控制布尔值匹配行为:

  • operator:可以接受 or 和 and。or 是匹配其中一个,and 是匹配所有。
  • analyzer:这个参数定义了分析查询文本用到的分析器。
  • fuzziness:构建模糊查询,可以传入 0.0 ~ 1.0 之间的值来设置相似度。
  • prefix_length:控制模糊查询的行为。
  • max_expansions:控制模糊查询的行为。

match_phrase查询

类似布尔值查询,不同的是,它从分析后的文本中构建短语查询,而不是布尔子句。

match_phrase_prefix 查询

基本上与 match_phrase 查询一样,它允许查询文本的最后一个词条只做前缀匹配。

query_string 查询

query_string 提供了如下的参数:

  • query:参数指定查询文本
  • default_field :参数指定默认的查询字段,默认是 _all
  • default_operator:默认值是 or
  • 等其他参数

标识符查询

标识符查询是一个简单的查询,仅用提供的标识符来过滤返回的文档。

{      "query" : {          "ids" : {              "values" : ["10", "11", "12"]          }      }  }

前缀查询

如果想查询所有 title 字段以 cri 开头的文档,可以如下:

{      "query" : {          "prefix" : {              "title" : "cri"          }      }  }

fuzzy_like_this 查询

查询与提供的文本类似的文档,它利用模糊字符串并选择生成的最佳差分词条:

{      "query":{          "fuzzy_like_this":{              "fields": ["title", "otitle"],              "like_text":"crime punishment"          }      }  }

fuzzy_like_this 支持如下参数:

  • fields:此参数定义应该执行查询的字段数组,默认是 _all 字段。
  • like_text:必须参数,参数文本
  • ignore_tf:计算相似度是否忽略词频,默认是 false,意味着使用词频。
  • max_query_terms :此参数指定生成的查询中能包括的最大查询词条数,默认是 25。
  • min_similarity:指定差分词条应该有的最小相似性,默认是 0.5。
  • prefix_length:指定差分词的公共前缀长度,默认是0。
  • boost:加权值,默认是1。
  • analyzer:分析器名称

fuzzy_like_this_field 查询

fuzzy_like_this_field 查询与 fuzzy_like_this 查询类似,但它只能对应单个字段。

fuzzy 查询

模糊匹配的最简单形式,比较耗费 CPU 资源:

{      "query":{          "fuzzy":{              "title":"crke"          }      }  }

通配符查询

在查询当中允许使用 * 和 ? 等通配符:

{      "query":{          "wildcard":{              "title":"cr?e"          }      }  }

【参考资料】

  1. Elasticsearch服务器开发

---EOF---

来自: http://renchx.com/Elasticsearch3/

</code></code></code></code></code></code></code></code></code></code></code></code>