elasticsearch实现传统数据库中的or功能,需要使用bool下面的should关键字,对于A or B的情况,应该至少返回A和B中的一个,但是如下语句,不仅返回A和B中的至少一个,也返回了没有A也没有B的情况:

 {
"query": {
"bool": {
"fileter":[
{"range":{"date.keyword":{"gt":"20170101","lt":"20170201"}}}
]
"should": [
{"term": {"A.keyword": "0000000000"}},
{"term": {"B.keyword": "0000000001"}}
]
}
}
}

参看elasticsearch官方文档,对should的说明如下:

should

The clause (query) should appear in the matching document. If the bool query is in a query context and has a must or filter clause then a document will match the bool query even if none of the should queries match. In this case these clauses are only used to influence the score. If the bool query is a filter context or has neither must or filter then at least one of the should queries must match a document for it to match the bool query. This behavior may be explicitly controlled by settings the minimum_should_match parameter.

表达的意思是:如果一个query语句的bool下面,除了should语句,还包含了filter或者must语句,那么should context下的查询语句可以一个都不满足,只是_score=0,所以上述查询语句,有无should语句,查询到的hits().total()是一样的,只是score不同而已。

为了达到传统数据库中or的功能,有如下两种方法:

  1. 将should语句写到must下面,然后让must和filter并列

    {
    "query": {
    "bool": {
    "fileter":[
    {"range":{"date.keyword":{"gt":"20170101","lt":"20170201"}}}
    ],
    "must":[
    {
    "bool":{
    "should": [
    {"term": {"A.keyword": "0000000000"}},
    {"term": {"B.keyword": "0000000001"}}
    ]
    }
    }
    ]
    }
    }
    }

2. 采用官方文档中的 minimum_should_match 参数

Type Example Description

Integer

3

Indicates a fixed value regardless of the number of optional clauses.

Negative integer

-2

Indicates that the total number of optional clauses, minus this number should be mandatory.

Percentage

75%

Indicates that this percent of the total number of optional clauses are necessary. The number computed from the percentage is rounded down and used as the minimum.

Negative percentage

-25%

Indicates that this percent of the total number of optional clauses can be missing. The number computed from the percentage is rounded down, before being subtracted from the total to determine the minimum.

Combination

3<90%

A positive integer, followed by the less-than symbol, followed by any of the previously mentioned specifiers is a conditional specification. It indicates that if the number of optional clauses is equal to (or less than) the integer, they are all required, but if it’s greater than the integer, the specification applies. In this example: if there are 1 to 3 clauses they are all required, but for 4 or more clauses only 90% are required.

Multiple combinations

2<-25% 9<-3

Multiple conditional specifications can be separated by spaces, each one only being valid for numbers greater than the one before it. In this example: if there are 1 or 2 clauses both are required, if there are 3-9 clauses all but 25% are required, and if there are more than 9 clauses, all but three are required.

minimum_should_match代表了最小匹配精度,如果设置minimum_should_match=1,那么should语句中至少需要有一个条件满足,查询语句如下:

{
"query": {
"bool": {
"fileter":[
{"range":{"date.keyword":{"gt":"20170101","lt":"20170201"}}}
]
"should": [
{"term": {"A.keyword": "0000000000"}},
{"term": {"B.keyword": "0000000001"}}
],
"minimum_should_match":1
}
}
}

第一种方法和第二种方法返回的结果是一致的。

另外,minimum_should_match的参数很多:

http://blog.csdn.net/xiao_jun_0820/article/details/51095521  讲的很清楚。

elasticsearch should实现or功能,设置minimum_should_match的更多相关文章

  1. browserCaps与浏览器功能设置

    使用 Web.config 文件中的 browserCaps 元素来定义浏览器的行为在 .NET Framework 2.0 中被否决,但仍受支持.此元素中的数据与浏览器定义文件 (.browser) ...

  2. TeeChart控件的安装与常用 功能设置

    TeeChart控件的安装 TeeChart 7.0 With Source在Delphi 7.0中的安装 一.删除Delphi7自带TeeChart  1.Component -> insta ...

  3. Eclipse自动补全功能和自动生成作者、日期注释等功能设置

    修改作者.日期注释格式:打开Windows->Preferences->Java->Code Style->Code Templates,点击右边窗口中的Comments,可以 ...

  4. Linux Eclipse代码提示功能设置(Java & C/C++)

    最近在Linux下开发,由于长期使用Visual Studio 2010,对代码提示功能情有独钟,现在在Linux下,使用Eclipse做开发,当然免不了怀念Visual Studio强悍的代码提示, ...

  5. Eclipse代码提示功能设置(Java & Eclipse+CDT C/C++)

    http://developer.51cto.com/art/200907/136242.htm http://blog.chinaunix.net/u/21684/showart_462486.ht ...

  6. WIN8 、WIN7 下IIS7.5、IIS8 的rewrite 伪静态功能设置方法

    原文 WIN8 .WIN7 下IIS7.5.IIS8 的rewrite 伪静态功能设置方法 win7和win8系统都自带有iis的功能.关于IIS的安装,上一篇已经讲述,这里就不重复了. 下面说下在w ...

  7. 最新亚马逊 Coupons 功能设置教程完整攻略!

    最新亚马逊 Coupons 功能设置教程完整攻略! http://m.cifnews.com/app/postsinfo/18479 亚马逊总是有新的创意,新的功能.最近讨论很火的,就是这个 Coup ...

  8. [NewLife.XCode]功能设置

    NewLife.XCode是一个有10多年历史的开源数据中间件,由新生命团队(2002~2019)开发完成并维护至今,以下简称XCode. 整个系列教程会大量结合示例代码和运行日志来进行深入分析,蕴含 ...

  9. Eclipse中代码自动提示功能设置

    Eclipse中代码自动提示功能设置 1 打开eclipse→Windows→Preferences→Java→Editor→Content Assist: 修改Auto Activation tri ...

  10. AS 功能 设置 SDK AVD 简介 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

随机推荐

  1. STM32 芯片锁死解决方法

    芯片锁死原因: 1.烧进去的工程对应器件与目标器件不一致: 2.烧进去的工程HSE_VALUE与目标板上晶振频率不一致: 3.... 解决方法: 1.工程设置 2.按住复位按键,或短接复位脚电容,点击 ...

  2. [转帖]Lightning 实操指南

    2.2.2 Lightning 实操指南 这一节将介绍如何使用 Lightning 导入数据的实操 2.2.2.1 TiDB Lightning 快速开始 注意 TiDB Lightning 运行后, ...

  3. [转帖]Linux平台shell脚本输入密码,不显示明文

    需求:shell脚本中输入密码,要求不显示明文,需要将其转换为"*"星号,或者不显示 实现方案:有两种实现方案,一是通过stty命令来实现,二是直接使用read来实现 方案一:使用 ...

  4. Mysql8.0.32 union all创建视图无法使用中文模糊查询的坑

    Mysql8.0.32 union all创建视图无法使用中文模糊查询的坑 摘要 本周研发同事反馈现场有一个问题. 客户使用mysql的数据库(Windows平台) 然后在多表union all 创建 ...

  5. [转帖]传输层安全协议真(TLS)的安全吗?

    https://zhuanlan.zhihu.com/p/305161227 随着数字通信,计算机网络,公钥密码体制等技术的迅速发展,安全网络通信已经成为了人们的日常需求.TLS 作为目前被广泛应用的 ...

  6. [转帖]linux 下 {}大括号的用法

    我们平时使用Linux的时候经常遇到这样一个问题,举例有这样一种情况:执行命令 $ cp /etc/apt/sources.list /etc/apt/sources.list.bak 这里面有个问题 ...

  7. Mysql 查看varchar和char类型表的列长度

    SELECT table_name, SUM(character_maximum_length) AS sum_length FROM information_schema.columns WHERE ...

  8. vue中v-model修饰符的使用和组件使用v-model

    1.lazy 修饰器 lazy修饰器在input框中的表现效果是: 当你失去焦点后值才会跟新. 它的跟新时机是失去焦点后 这个修饰器在项目中运用的场景较少 <template> <d ...

  9. 【JS 逆向百例】猿人学系列 web 比赛第二题:js 混淆 - 动态 cookie,详细剖析

    逆向目标 猿人学 - 反混淆刷题平台 Web 第二题:js 混淆,动态 cookie 目标:提取全部 5 页发布日热度的值,计算所有值的加和 主页:https://match.yuanrenxue.c ...

  10. ABP-VNext 用户权限管理系统实战05----扩展授权类型(单点登录)

    一.适合场景: 1.我方系统在集成到别人的集成本台时一般是拿别的平台的用户名,在我方系统进行登录 2.我方系统是前后端分离,前端要拿到token 二.解决方案:自定义授权类型 我们知道Identity ...