Elasticsearch Reference [6.2] » Query DSL

参考官方文档 :https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html

一、组合查询 Compound queries

  1. Constant Score Query 指定_score分数查询
GET /_search
{
"query": {
"constant_score" : {
"filter" : {
"term" : { "user" : "kimchy"}
},
"boost" : 1.2
}
}
}
  1. Bool Query 布尔值查询

must:查询的条件必须在匹配的文档中,并计算相似度得分

filter:必须满足条件,不会计算相似度得分

should:满足子条件的一个或者多个,满足的格式可以通过"minimum_should_match" : 1设置,类似 OR (如果查询中包filter则至少满足一个should)

must_not:返回的文档必须不满足条件,类似 NOT

tips : 日期格式在添加文档和搜索的时候加上T,字符串不区分大小写 如pek

GET /stu/_search
{
"query": {
"bool": {
"must": [
{ "match": { "address":"上海市 保德路 闸北区"}}
],
"filter": [
{"term":{ "id": 11 }},
{"term":{ "city": "pek" }},
{"range":{"regdate": {"gte": "2018-03-03T15:33:32","lte":"2018-03-03T15:33:33"}}}
],
"should":[
{"term":{ "score": 80.8 }},
{"term":{ "score": 80.0 }}
],
"must_not":[{
"term" : { "age" : 30 }
}],
"minimum_should_match" : 1,
"boost" : 1.0
}
}
}

二、查询上下文

Query Context:文档和查询条件的匹配度,出了决定是否与文档匹配外,还会计算查询条件和文档的匹配度_score

GET /_search
{
"query" : {
"term" : { "user" : "kimchy" }
}
}

三、过滤上下文

Filter context: 精确搜索文档和查询是否匹配,不会去计算匹配度,主要用于过滤结构化数据.

经常使用的过滤器会被elasticsearch自动缓存,以提高查询效率

示例:title中包含search、content中包含elasticsearch 且 status="published" & publish_date >="2015-01-01"

filter里的term、range

GET /_search
{
"query": {
"bool": {
"must": [
{ "match": { "title": "Search"}},
{ "match": { "content": "Elasticsearch" }}
],
"filter": [
{ "term": { "status": "published" }},
{ "range": { "publish_date": { "gte": "2015-01-01" }}}
]
}
}
}

四、查询所有

查询所有_score的文档,(注:boost的默认值是 1.0)

GET /_search
{
"query": {
"match_all": {}
}
}

查询_score=1.2的所有文档

GET /_search
{
"query": {
"match_all": { "boost" : 1.2 }
}
}

五、全文查询

1.match query

query:查询字段message中包含 " this is test " , 注意 "to be or not to be" 属于停顿词,过滤器默认会把remove调,设置zero_terms_query:"all"启用

另operator/zero_terms_query非必填参数,更详细内容查看match query官方文档

GET /_search
{
"query": {
"match" : {
"message" : {
"query" : "to be or not to be,this is test",
"operator" : "and",
"zero_terms_query": "all"
}
}
}
}

2.Match Phrase Query

match_phrase从分析文本"this is a test"中创建一组词去查询,analyzer分词器也可以使用ik等中文分词器

GET /_search
{
"query": {
"match_phrase" : {
"message" : {
"query" : "this is a test",
"analyzer" : "standard"
}
}
}
}

3.Match Phrase Prefix Query

match_phrase_prefix与match_phrase类似,只是它允许在文本中的最后一个词的前缀匹配

GET /_search
{
"query": {
"match_phrase_prefix" : {
"message" : "quick brown f"
}
}
}

4.Multi Match Query

multi_match匹配查询上以允许多字段查询(subject/message字段):

GET /_search
{
"query": {
"multi_match" : {
"query": "this is a test",
"fields": [ "subject", "message" ]
}
}
}

5.Common Terms Query

common:停顿词配置相关如 " the to be "等

6.Query String Query

query_string:没理解看官方文档

7.Simple Query String Query

simple_query_string: 与query_string查询不同的是,simple_query_string查询永远不会抛出异常,并放弃查询的无效部分

GET /_search
{
"query": {
"simple_query_string" : {
"query": "\"fried eggs\" +(eggplant | potato) -frittata",
"fields": ["title^5", "body"],
"default_operator": "and"
}
}
}

elasticsearch入门使用(三) Query DSL的更多相关文章

  1. Elasticsearch入门教程(三):Elasticsearch索引&映射

    原文:Elasticsearch入门教程(三):Elasticsearch索引&映射 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文 ...

  2. ElasticSearch入门 第三篇:索引

    这是ElasticSearch 2.4 版本系列的第三篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...

  3. ElasticSearch入门 第六篇:复合数据类型——数组,对象和嵌套

    这是ElasticSearch 2.4 版本系列的第六篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...

  4. ElasticSearch入门 第九篇:实现正则表达式查询的思路

    这是ElasticSearch 2.4 版本系列的第九篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...

  5. ElasticSearch入门 第五篇:使用C#查询文档

    这是ElasticSearch 2.4 版本系列的第五篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...

  6. ElasticSearch查询 第三篇:词条查询

    <ElasticSearch查询>目录导航: ElasticSearch查询 第一篇:搜索API ElasticSearch查询 第二篇:文档更新 ElasticSearch查询 第三篇: ...

  7. ElasticSearch入门 第四篇:使用C#添加和更新文档

    这是ElasticSearch 2.4 版本系列的第四篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...

  8. ElasticSearch入门点滴

    这是Elasticsearch-6.2.4 版本系列的第一篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 ...

  9. ElasticSearch入门 第二篇:集群配置

    这是ElasticSearch 2.4 版本系列的第二篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...

随机推荐

  1. 【转】IntelliJ 创建main函数快捷

    http://blog.csdn.net/tiantiandjava/article/details/42269173 今天偶然发现了IntelliJ中 创建main函数的快捷键,依次还有for循环, ...

  2. 在Terminal中,如何打开Finder,并显示当前的目录

    这是一个非常方便实用的小技巧,在Terminal中输入如下命令: $ open . 有图有真相: 参考: Open Finder in Current Folder from Terminal

  3. NLP.TM | GloVe模型及其Python实现

    在进行自然语言处理中,需要对文章的中的语义进行分析,于是迫切需要一些模型去描述词汇的含义,很多人可能都知道word2vector算法,诚然,word2vector是一个非常优秀的算法,并且被广泛运用, ...

  4. bootstrap历练实例: 基本胶囊式的导航菜单

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  5. bootstrap历练实例:复选框或单选按钮作为输入框组的前缀或后缀

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  6. java在线聊天项目 实现基本聊天功能后补充的其他功能详细需求分析 及所需要掌握的Java知识基础 SWT的激活方法,swt开发包下载,及破解激活码

    补充聊天项目功能,做如下需求分析: 梳理项目开发所需的必要Java知识基础 GUI将使用更快速的swt实现 SWT(Standard Widget Toolkit) Standard Widget T ...

  7. ios开发--常用的高效开发的宏

    本次在做项目的时候使用了下面的一些宏定义 以及 建立宏定义的一些规则.虽然只用了其中的一点点,但是还是极大的提高了开发效率.. 将这些宏放到一个头文件里然后再放到工程中,在需要使用这些宏定义的地方体检 ...

  8. 洛谷 P1663 山

    https://www.luogu.org/problemnew/show/P1663 可能在这里看会好一点:[题解]

  9. Mac OS 终端强化美化:iterm2 + zsh + oh~my~zsh 设置教程

    为了获得更好的排版效果,文章改用markdown撰写,故重发一次. 前言 mac自带的terminal终端没有文件名高亮等功能,而且界面不是很好看,故今晚学舍友折腾了终端,可以让自己使用起来更加方便, ...

  10. day23 02 组合(继续人狗大战游戏)

    day23 02 组合(继续人狗大战游戏) 面向对象的三大特性:继承 多态 封装 先讲解一下组合 组合:一个对象的属性值是另一个类的对象:对象.属性.属性(一般有两个点) 继续扩展day22 01里面 ...