elasticsearch dsl 经常使用过滤和查询语句

过滤语句为 从全部数据中查找一个结果集,查询语句则是标示的是查找一个精确的结果集合信息;sql

查询语句会询问每一个文档的字段值与特定值的匹配程度如何数组

一条过滤语句会询问每一个文档的字段值是否包含着特定值:curl

一条查询语句会计算每一个文档与查询语句的相关性,会给出一个相关性评分 _score ,而且 按照相关性对匹配到的文档进行nosql

排序。 这种评分方式很是适用于一个没有彻底配置结果的全文本搜索url

注:红色部分没有明白什么意思,若是有知道的烦请给予帮助,谢谢。。。。spa

一、索引中的数据为:code

curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty'
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "dsltest",
        "_type" : "dsltesttype",
        "_id" : "2",
        "_score" : 0.2876821,
        "_source" : {
          "age" : 30,
          "date" : "2017-09-01",
          "public" : true,
          "tag" : [
            "full_text"
          ],
          "title" : "how to make second"
        }
      },
      {
        "_index" : "dsltest",
        "_type" : "dsltesttype",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "age" : 26,
          "date" : "2014-09-01",
          "public" : true,
          "tag" : [
            "full_text",
            "nosql"
          ],
          "title" : "how to make millions"
        }
      }
    ]
  }
}

过滤语句:对象

term 过滤的是单个字段,用的是对象的方式,若是多个字段会报 [term] query does not support array of values term为过滤语句,因此不能添加match匹配信息排序

语法:
curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty' -d '{"query":{"term":{"age":26}}}'

terms 过滤的是多个字段,用的是数组的方式;若是用对象的方式则会报错索引

terms中数组中的多个字段 标示的是 或者(OR)的关系

curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty' -d '{"query":{"terms":{"title":["make"]}}}'

range 查找范围内的数据:包括 gt大于  gte 大于等于  lt小于 lte小于等于

查找 年龄大于等于30的数据
curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty' -d '{"query":{"range":{"age":{"gte":30}}}}'

exists 查找包含 某一个字段的数据 

获取包含title字段的数据
curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty' -d '{"query":{"exists":{"field":"title"}}}'

若是存在多个过滤条件则经过 bool进行处理

must :: 多个查询条件的彻底匹配,至关于 and 。

must_not :: 多个查询条件的相反匹配,至关于 not 。

should :: 至少有一个查询条件匹配, 至关于 or 。

多个过滤条件使用的方式为:

表示的是:过滤title中包含 make 同时age大于等于 27的数据
curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty' -d '{"query":{"bool":{"must":{"term":{"title":"make"}},"must":{"range":{"age":{"gte":27}}}}}}'

查询语句:

match_all 查询全部的数据

curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty' -d '{"query":{"match_all":{}}}'
相似于:
curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty'

match 查询匹配对应的词 对title进行分词查询结果

curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty' -d '{"query":{"match":{"title":"make"}}}'

multi_match 多字段匹配:查找 字段title中为make的数据,其中fields中的字段不必定存在

curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty' -d '{"query":{"multi_match":{"query":"make","fields":["title"]}}}'

若是同时存在多个一样也是用的 bool 查询bool和过滤bool相同 查询条件的使用方式为:

查询语句中 title中包含 make和second 或者 age包含26的数据
curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty' -d '{"query":{"bool":{"must":[{"match":{"title":"make"}},{"match":{"title":"second"}}],"should":{"match":{"age":26}}}}}'

同时包含 过滤 条件 和查询条件的语句方式为:[查询的为:title为make 同时 age 大于等于26,同时title为make过滤]

当前版本 5.2.2已经没有啦 filtered过滤字段,更改成bool 若是对查询和过滤合并处理的方式为
curl -i -XGET 'http://localhost:9200/dsltest/dsltesttype/_search?pretty' -d '{"query": {"bool": {"must":{"match":{"title":"make"}},"filter": {"bool": {"must": [{"term": {"title": "make"}}, {"range": {"age": {"gte": 26}}}]}}}}}'
相关文章
相关标签/搜索