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"
}
}
]
}
}
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://elasticsea ...
- elasticsearch入门使用(三) Query DSL
Elasticsearch Reference [6.2] » Query DSL 参考官方文档 :https://www.elastic.co/guide/en/elasticsearch/refe ...
- Elasticsearch Query DSL 整理总结(三)—— Match Phrase Query 和 Match Phrase Prefix Query
目录 引言 Match Phase Query slop 参数 analyzer 参数 zero terms query Match Phrase 前缀查询 max_expansions 小结 参考文 ...
- 48.Query DSL
主要知识点 1.Query DSL的理解及基本语法 2.如何组合多个搜索条件 bool 一.Query DSL的理解 Query DSL的查询形式如下: GET /_search { &quo ...
- elasticsearch系列四:搜索详解(搜索API、Query DSL)
一.搜索API 1. 搜索API 端点地址 从索引tweet里面搜索字段user为kimchy的记录 GET /twitter/_search?q=user:kimchy 从索引tweet,user里 ...
- Elasticsearch使用备忘
最近我们需要对大约2T(6.5亿条)日志做全文检索,Elasticsearch看起来很火爆,又有很多产品使用(Facebook.github.stackoverflow),值得一试.以下是一些基础知识 ...
- Elasticsearch Query DSL 语言介绍
目录 0. 引言 1. 组合查询 2. 全文搜索 2.1 Match 2.2 Match Phase 2.3 Multi Match 2.4 Query String 2.5 Simple Query ...
- Elasticsearch(入门篇)——Query DSL与查询行为
ES提供了丰富多彩的查询接口,可以满足各种各样的查询要求.更多内容请参考:ELK修炼之道 Query DSL结构化查询 Query DSL是一个Java开源框架用于构建类型安全的SQL查询语句.采用A ...
- ElasticSearch的 Query DSL 和 Filter DSL
Elasticsearch支持很多查询方式,其中一种就是DSL,它是把请求写在JSON里面,然后进行相关的查询. Query DSL 与 Filter DSL DSL查询语言中存在两种:查询DSL(q ...
随机推荐
- springboot之websocket
一.WebSocket协议是基于TCP的一种新的网络协议.它实现了浏览器与服务器全双工(full-duplex)通信——允许服务器主动发送信息给客户端. 二.长久以来, 创建实现客户端和用户端之间双工 ...
- python学习笔记03 --------------程序交互与格式化输出
1.读取用户输入内容 语法:input() 例: name = input('你的名字是?) print('你好'+name) 程序会等待用户输入名字后打印:你好(用户输入的名字) 注意:input接 ...
- 逆波兰表达式[栈 C 语言 实现]
逆波兰表达式 逆波兰表达式又叫做后缀表达式.在通常的表达式中,二元运算符总是置于与之相关的两个运算对象之间,这种表示法也称为中缀表示.波兰逻辑学家J.Lukasiewicz于1929年提出了另一种表示 ...
- leetcode-颜色分类
颜色分类 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示 ...
- SIFT特征原理与理解
SIFT特征原理与理解 SIFT(Scale-invariant feature transform)尺度不变特征变换 SIFT是一种用来侦测和描述影像中局部性特征的算法,它在空间尺度中寻找极值点,并 ...
- Python3 小工具-ICMP扫描
from scapy.all import * import optparse import threading import os def scan(ipt): pkt=IP(dst=ipt)/IC ...
- HADOOP docker(三):HDFS高可用实验
前言1.机器环境2.配置HA2.1 修改hdfs-site.xml2.2 设置core-site.xml3.配置手动HA3.1 关闭YARN.HDFS3.2 启动HDFS HA4.配置自动HA4. ...
- Python3 深浅拷贝
一 定义 在Python中对象的赋值其实就是对象的引用.当创建一个对象,把它赋值给另一个变量的时候,python并没有拷贝这个对象,只是拷贝了这个对象的引用而已. 浅拷贝: 浅拷贝值只拷贝一层,具有自 ...
- POJ 2229 计数DP
dp[i]代表是数字i的最多组合数如果i是一个奇数,i的任意一个组合都包含1,所以dp[i] = dp[i-1] 如果i是一个偶数,分两种情况讨论,一种是序列中包含1,因此dp[i]=dp[i-1]一 ...
- C与C++,面向过程与面向对象
C与C++在电梯处理上的不同 (注:个人理解) 对比区别: C语言程序制定具体流程,按流程逐步进行. C++程序将过程结构化,需要使用时利用接口访问与调用不同功能的类结构结构. 电梯类代码 电梯类定义 ...