Elasticsearch 基本查询学习
在 Elasticsearch 当中最简单的查询是使用 URI 请求查询,例如下面的查询: 上面的这两个查询返回的结果是相同的,上面的是 URI 方式的查询,下面的是 DSL 查询。 当然如果是需要分页查询的话可以进行如下方式: 也可以添加如下的参数: 它仅匹配在给定字段中含有该词条的文档,而且是确切的、未经分析的词条。请注意是未经过分析的。 如果想查询索引中的所有文档,只需要如下查询: match 查询把 query 参数中的值拿出来,加以分析,然后构建对应的查询。使用 match 查询时,ElasticSearch 将对一个字段选择合适的分析器。 match 查询的几种类型: 布尔值匹配查询分析提供的文本,然后做出布尔查询,有如下参数可以控制布尔值匹配行为: 类似布尔值查询,不同的是,它从分析后的文本中构建短语查询,而不是布尔子句。 match_phrase_prefix 查询 基本上与 match_phrase 查询一样,它允许查询文本的最后一个词条只做前缀匹配。 query_string 提供了如下的参数: 标识符查询是一个简单的查询,仅用提供的标识符来过滤返回的文档。 如果想查询所有 title 字段以 cri 开头的文档,可以如下: 查询与提供的文本类似的文档,它利用模糊字符串并选择生成的最佳差分词条: fuzzy_like_this 支持如下参数: fuzzy_like_this_field 查询与 fuzzy_like_this 查询类似,但它只能对应单个字段。 模糊匹配的最简单形式,比较耗费 CPU 资源: 在查询当中允许使用 * 和 ? 等通配符: 【参考资料】 ---EOF---简单查询
http :9200/test/_search q==name:rcx { "query" : { "query_string" : {"query" : "name:rcx"} } }
{ "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"} } }
理解查询过程
搜索类型
基本查询
词条查询
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 查询
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_phrase查询
query_string 查询
标识符查询
{ "query" : { "ids" : { "values" : ["10", "11", "12"] } } }
前缀查询
{ "query" : { "prefix" : { "title" : "cri" } } }
fuzzy_like_this 查询
{ "query":{ "fuzzy_like_this":{ "fields": ["title", "otitle"], "like_text":"crime punishment" } } }
fuzzy_like_this_field 查询
fuzzy 查询
{ "query":{ "fuzzy":{ "title":"crke" } } }
通配符查询
{ "query":{ "wildcard":{ "title":"cr?e" } } }