主要知识点

1、Query DSL的理解及基本语法

2、如何组合多个搜索条件 bool

 
 

一、Query DSL的理解

Query DSL的查询形式如下:

GET /_search

{

"query": {

"match_all": {}

}

}

在37小节中我们学到到query string 的语法,这里学习另外一种搜索语法,

Query DSL(Domain Specific Language),这个方法是在"query"字段中定义我们要搜索的内容,包括匹配的方式等信息。

 
 

二、Query DSL的基本语法

1、

{

QUERY_NAME: {

ARGUMENT: VALUE,

ARGUMENT: VALUE,...

}

}

2、

{

QUERY_NAME: {

FIELD_NAME: {

ARGUMENT: VALUE,

ARGUMENT: VALUE,...

}

}

}

 
 

示例:

 
 

GET /test_index/test_type/_search

{

"query": {

"match": {

"test_field": "test"

}

}

}

查询test_field这个字段中必修包含test

三、如何组合多个搜索条件

1、先构造数据

PUT /website/article/1

{

"title":"es",

"content":"I love es",

"author_id":111

}

PUT /website/article/2

{

"title":"python",

"content":"I love python",

"author_id":112

}

PUT /website/article/3

{

"title":"scrapy",

"content":"I love scrapy",

"author_id":113

}

2、提供需求

title必须包含es,content可以包含es也可以不包含,author_id必须不为113

3、书写es bool查询语句

GET /website/article/_search

{

"query": {

"bool": {

"must": [

{"match": {

"title": "es"

}}

],

"should": [

{"match": {

"content": "es"

}}

],

"must_not": [

{"match": {

"author_id": "113"

}}

]

}

}

}

执行结果如下:

{

"took": 269,

"timed_out": false,

"_shards": {

"total": 5,

"successful": 5,

"failed": 0

},

"hits": {

"total": 1,

"max_score": 0.5408423,

"hits": [

{

"_index": "website",

"_type": "article",

"_id": "1",

"_score": 0.5408423,

"_source": {

"title": "es",

"content": "I love es",

"author_id": 111

}

}

]

}

}

 
 

另一个较为复杂的示例

GET /test_index/_search

{

"query": {

"bool": {

"must": { "match": { "name": "tom" }},

"should": [

{ "match": { "hired": true }},

{ "bool": {

"must": { "match": { "personality": "good" }},

"must_not": { "match": { "rude": true }}

}}

],

"minimum_should_match": 1

}

}

}

 
 

四、延伸阅读

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html

 
 

 
 

48.Query DSL的更多相关文章

  1. Query DSL for elasticsearch Query

    Query DSL Query DSL (资料来自: http://www.elasticsearch.cn/guide/reference/query-dsl/) http://elasticsea ...

  2. Elasticsearch(入门篇)——Query DSL与查询行为

    ES提供了丰富多彩的查询接口,可以满足各种各样的查询要求.更多内容请参考:ELK修炼之道 Query DSL结构化查询 Query DSL是一个Java开源框架用于构建类型安全的SQL查询语句.采用A ...

  3. ElasticSearch的 Query DSL 和 Filter DSL

    Elasticsearch支持很多查询方式,其中一种就是DSL,它是把请求写在JSON里面,然后进行相关的查询. Query DSL 与 Filter DSL DSL查询语言中存在两种:查询DSL(q ...

  4. Query DSL(1)

    https://www.elastic.co/guide/en/elasticsearch/reference/2.3/query-dsl.html Query DSL GET _search { & ...

  5. Elasticsearch Query DSL

    Elasticsearch Query DSL By:授客 QQ:1033553122 1. match_all 1 2. match 2 3. match_phrase 5 4. match_phr ...

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

    一. Search API eg: GET /mall/product/_search?q=name:productName&sort=price desc 特点:search的请求参数都是以 ...

  7. zombodb  query dsl

    zombodb query dsl 是为了简化es 查询的处理,同时可以兼容基本上所有的es 操作 一个简单的查询,查询任何字段包含cats 以及dogs 的 SELECT * FROM table ...

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

    目录 引言 Match Phase Query slop 参数 analyzer 参数 zero terms query Match Phrase 前缀查询 max_expansions 小结 参考文 ...

  9. Elasticsearch Query DSL 整理总结(二)—— 要搞懂 Match Query,看这篇就够了

    目录 引言 构建示例 match operator 参数 analyzer lenient 参数 Fuzziness fuzzniess 参数 什么是模糊搜索? Levenshtein Edit Di ...

随机推荐

  1. 菜鸟nginx源代码剖析 配置与部署篇(一) 手把手实现nginx "I love you"

    菜鸟nginx源代码剖析 配置与部署篇(一) 手把手配置nginx "I love you" Author:Echo Chen(陈斌) Email:chenb19870707@gm ...

  2. 在Android中创建文件

    import java.io.File; import java.io.IOException; import android.app.Activity; import android.os.Bund ...

  3. B1060 [ZJOI2007]时态同步 dfs

    两遍dfs,第一遍有点像找重链,第二遍维护答案,每个点维护一个当前深度,然后就没啥了. ps:memset(lst,-1,sizeof(lst));这一句多余的话让我debug半天... 题干: De ...

  4. Shredding Company(dfs)

    http://poj.org/problem?id=1416 题意:将一个数分成几部分,使其分割的各个数的和最大并且小于所给的数. 凌乱了..参考的会神的代码..orz... #include < ...

  5. thinkphp data方法

    data方法也是模型类的连贯操作方法之一,用于设置当前要操作的数据对象的值,可能大家不太习惯用这个方法,今天来讲解下如何用好data方法. 用法 写操作 通常情况下我们都是通过create方法或者赋值 ...

  6. python 5:str(某一变量)(将其他数字解释为字符串)

    age = messege = "Your's age is " + str(age) #将其他数字更改为字符串 print(messege) 运行结果应该是: Your's ag ...

  7. D - Replacement

    Problem description Little Petya very much likes arrays consisting of n integers, where each of them ...

  8. C - Lucky Numbers (easy)

    Problem description Petya loves lucky numbers. Everybody knows that positive integers are lucky if t ...

  9. PL/SQL之基础篇

    参考文献:<Oracle完全学习手册>第11章 1.PL/SQL概述 PL/SQL(Procedure Language/Structuer Query Language)是Oracle对 ...

  10. php常见报错

    Php常见错误提示 一.Fatal error: Call to undefined function……函数不存在,可能的原因:系统不存在这个函数且你也没自定义 二.syntax error, un ...