Elasticsearch Query DSL备忘(1)(Constant score query和Bool Query)

Query DSL (Domain Specific Language),基于json的查询方式

1、Constant score query,常量分值查询,目的就是返回指定的score,一般都结合filter使用,因为filter context忽略score。

GET /customer/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "match": {
          "addr": "天津,北京"
        }
      },
      "boost": 5.2
    }
  }
}

result:返回结果中score都是被指定的5.2
{
  "took" : 8,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 5.2,
    "hits" : [
      {
        "_index" : "customer",
        "_type" : "doc",
        "_id" : "510221197801023611",
        "_score" : 5.2,
        "_source" : {
          "name" : "王刚",
          "id" : "510221197801023611",
          "addr" : "北京市朝阳区未名路109号",
          "tel" : "13901004491"
        }
      },
      {
        "_index" : "customer",
        "_type" : "doc",
        "_id" : "51228199001013611",
        "_score" : 5.2,
        "_source" : {
          "name" : "白开心",
          "id" : "51228199001013611",
          "addr" : "天津市海港路1021号",
          "tel" : "13590850990"
        }
      }
    ]
  }
}

2、bool query,布尔查询

Bool查询对应Lucene中的BooleanQuery,它由一个或者多个子句组成,每个子句都有特定的类型。

  • must  返回的文档必须满足must子句的条件,并且参与计算分值
  • filter  返回的文档必须满足filter子句的条件。但是不会像must一样参与计算分值
  • should 返回的文档可能满足should子句的条件。bool查询在query context中,并且有一个must或filter子句,即使没有一个should查询匹配,文档也会进行bool匹配。在这种情况下,这些should仅用于影响分数。如果在filter context中,或者没有must或filter子句,那么should子句必须和文档匹配,才能匹配bool查询。这种行为由minimum_should_match 参与决定。
  • must_not 返回的文档必须不满足must_not定义的条件。

官网的例子:

POST _search
{
  "query": {
    "bool" : {
      "must" : {
        "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,
      "boost" : 1.0
    }
  }
}

bool查询案例分解:

第一步:查询name为“李云龙”的文档

GET /customer/_search
{
  "query": {
    "bool": {
      "must": {
        "term":{"name.keyword":"李云龙"}
      }
    }
  }
}
返回三个文档:
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 1.4916549,
    "hits" : [
      {
        "_index" : "customer",
        "_type" : "doc",
        "_id" : "4",
        "_score" : 1.4916549,
        "_source" : {
          "name" : "李云龙",
          "id" : "510221197001013611",
          "addr" : "昆明市滇池路阳光时代1栋1单元",
          "tel" : "13808712808"
        }
      },
      {
        "_index" : "customer",
        "_type" : "doc",
        "_id" : "224",
        "_score" : 1.4916549,
        "_source" : {
          "name" : "李云龙",
          "id" : "224",
          "addr" : "天津市阳光路2008号",
          "tel" : "13908712808"
        }
      },
      {
        "_index" : "customer",
        "_type" : "doc",
        "_id" : "510221197001013611",
        "_score" : 1.4916549,
        "_source" : {
          "name" : "李云龙",
          "id" : "510221197001013611",
          "addr" : "上海市浦东区华北路8号",
          "tel" : "13908712808"
        }
      }
    ]
  }
}

第二步:加入过滤条件,只保留id为510221197001013611的文档

GET /customer/_search
{
  "query": {
    "bool": {
      "must": {
        "term":{"name.keyword":"李云龙"}
      },
      "filter": {
        "term": {
          "id": "510221197001013611"
        }
      }
    }
  }
}

返回结果减少到2个文档,并且score相同:
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.4916549,
    "hits" : [
      {
        "_index" : "customer",
        "_type" : "doc",
        "_id" : "4",
        "_score" : 1.4916549,
        "_source" : {
          "name" : "李云龙",
          "id" : "510221197001013611",
          "addr" : "昆明市滇池路阳光时代1栋1单元",
          "tel" : "13808712808"
        }
      },
      {
        "_index" : "customer",
        "_type" : "doc",
        "_id" : "510221197001013611",
        "_score" : 1.4916549,
        "_source" : {
          "name" : "李云龙",
          "id" : "510221197001013611",
          "addr" : "上海市浦东区华北路8号",
          "tel" : "13908712808"
        }
      }
    ]
  }
}

第三步:使用should,判断addr中必须有昆明市,这种情况下should子句会影响计分

GET /customer/_search
{
  "query": {
    "bool": {
      "must": {
        "term":{"name.keyword":"李云龙"}
      },
      "filter": {
        "term": {
          "id": "510221197001013611"
        }
      },
      "should": [
        {"match": {
          "addr": "昆明市"
        }}
      ]
    }
  }
}
返回结果中,地址是昆明市的文档score加重
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 3.408528,
    "hits" : [
      {
        "_index" : "customer",
        "_type" : "doc",
        "_id" : "4",
        "_score" : 3.408528,
        "_source" : {
          "name" : "李云龙",
          "id" : "510221197001013611",
          "addr" : "昆明市滇池路阳光时代1栋1单元",
          "tel" : "13808712808"
        }
      },
      {
        "_index" : "customer",
        "_type" : "doc",
        "_id" : "510221197001013611",
        "_score" : 1.5720221,
        "_source" : {
          "name" : "李云龙",
          "id" : "510221197001013611",
          "addr" : "上海市浦东区华北路8号",
          "tel" : "13908712808"
        }
      }
    ]
  }
}

第四步:加入must_not排除上海

GET /customer/_search
{
  "query": {
    "bool": {
      "must": {
        "term":{"name.keyword":"李云龙"}
      },
      "filter": {
        "term": {
          "id": "510221197001013611"
        }
      },
      "should": [
        {"match": {
          "addr": "昆明市"
        }}
      ],
      "must_not": [
        {"match": {
          "addr": "上海"
        }}
      ]
    }
  }
}

只返回一个文档:
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 3.408528,
    "hits" : [
      {
        "_index" : "customer",
        "_type" : "doc",
        "_id" : "4",
        "_score" : 3.408528,
        "_source" : {
          "name" : "李云龙",
          "id" : "510221197001013611",
          "addr" : "昆明市滇池路阳光时代1栋1单元",
          "tel" : "13808712808"
        }
      }
    ]
  }
}

原文地址:https://www.cnblogs.com/asker009/p/10201051.html

时间: 2024-08-08 21:36:48

Elasticsearch Query DSL备忘(1)(Constant score query和Bool Query)的相关文章

Query DSL for elasticsearch Query

Query DSL Query DSL (资料来自: http://www.elasticsearch.cn/guide/reference/query-dsl/) http://elasticsearch.qiniudn.com/ --简介-- elasticsearch 提供基于JSON的完整的Query DSL查询表达式(DSL即领域专用语言). 一般来说, 普通的查询如 term 或者 prefix. 另外还有混合查询如 bool 等. 另外查询表达式(Queries)还能够关联特定的过

Elasticsearch Query DSL 整理总结(三)—— Match Phrase Query 和 Match Phrase Prefix Query

目录 引言 Match Phase Query slop 参数 analyzer 参数 zero terms query Match Phrase 前缀查询 max_expansions 小结 参考文档 系列文章列表 Query DSL Java Rest Client API 引言 今天再读庄子的<逍遥游>,其中鲲鹏之扶摇直上九万里之气势,蜩(tiao)与学鸠之渺小之对比,令人印象深刻,并对鲲鹏之志心生向往.而郭象在注<庄子>卷中却说,"苟足于其性,则虽大鹏无以自贵于小

Elasticsearch使用备忘

最近我们需要对大约2T(6.5亿条)日志做全文检索,Elasticsearch看起来很火爆,又有很多产品使用(Facebook.github.stackoverflow),值得一试.以下是一些基础知识笔记. Elasticsearch是一个基于Lucene构建的开源.分布式.RESTful的搜索引擎,能够实现近实时(NRT)搜索,稳定.可靠.安装方便.性能不错.水平扩展.文档齐全.社区火爆,这几点很重要. 如果之前已经了解过分布式存储系统.query切词.检索相关性策略,Elasticsearc

Elasticsearch的DSL之query and filter

在Elasticsearch的DSL中, 有两个概念需要搞清楚, query 和 filter, 对ES的检索效率是很有影响的. 下面就来搞清楚这两个关键字的具体函数. query context: 回答的是这个文档在多大程度上匹配查询语句(How well does this document match this query clause?),会计算出一个分数_score. filter context: 回答的是这个文档与查询语句是否匹配,是 或者 不是(Does this documen

Elasticsearch Index API & Aggregations API & Query DSL

这篇小菜给大家演示和讲解一些Elasticsearch的API,如在工作中用到时,方便查阅. 一.Index API 创建索引库 curl -XPUT 'http://127.0.0.1:9200/test_index/' -d '{     "settings" : {       "index" : {       "number_of_shards" : 3,       "number_of_replicas" : 1

Elasticsearch学习笔记(二)Search API 与 Query DSL

一. Search API eg: GET /mall/product/_search?q=name:productName&sort=price desc 特点:search的请求参数都是以HTTP请求的的query stirng 附带的 适用范围:适用于临时的在命令行使用一些工具,比如curl,快速的发出请求,来检索想要的信息: 适用于简单的查询条件 二.Query DSL 将Query DSL视为ASL查询则有两种类型的查询语句: 叶子查询语句(Leaf Query clause) : 叶

ElasticSearch search api的基础语法+Query DSL搜索+filter与query对比+组合查询+定位不合法的搜索

一. search api的基础语法 1.search语法 GET /search{} GET /index1,index2/type1,type2/search{} GET /_search{ "from": 0, "size": 10} 2.http协议中get是否可以带上request body HTTP协议,一般不允许get请求带上request body,但是因为get更加适合描述查询数据的操作,因此还是这么用了 GET /_search?from=0&a

SQL注入备忘单

Find and exploit SQL Injections with free Netsparker SQL Injection Scanner SQL Injection Cheat Sheet, Document Version 1.4 About SQL Injection Cheat Sheet Currently only for MySQL and Microsoft SQL Server, some ORACLE and some PostgreSQL. Most of sam

Query DSL(1)

https://www.elastic.co/guide/en/elasticsearch/reference/2.3/query-dsl.html Query DSL GET _search { "query": { "bool": { "must": [ { "match": { "title": "Search" }}, { "match": { "c