主要知识点

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. Java API 读取HDFS的单文件

    HDFS上的单文件: -bash-3.2$ hadoop fs -ls /user/pms/ouyangyewei/data/input/combineorder/repeat_rec_categor ...

  2. Extjs 3 控制Grid某行某列不可编辑

    var cmGoodsFee = new Ext.grid.ColumnModel([rmGoodsFee, { header : "id", tooltip : "id ...

  3. Parallel in C#

    https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-write-a-simple-parallel ...

  4. 虚拟机中的ip和本机的ip不是一个网段的

    将虚拟机的网络适配器 模式改为桥接模式 然后就会和主机处于同一个网段了

  5. 玩游戏(dfs)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2566 #include <stdio.h ...

  6. 原生JS---3

    原生js学习笔记3——数组 定义数组 两种方式定义一个数组: 1. var array1 = new array(1, 2, 3, 4); 2. var array2 = [1, 2, 3, 4]; ...

  7. python-day4 运算符,if判断, for循环

    1.运算符 算术运算符 +   -    *    /    % 赋值运算符 =   +=  -= 比较运算符 ==    <   <=    >    >=    != 逻辑 ...

  8. 通过DOM实现点击隐藏父元素

    HTML代码简单如下: <ul id='ul1'> <li><a href="javascript:">1</a></li&g ...

  9. A - Word

    Problem description Vasya is very upset that many people on the Net mix uppercase and lowercase lett ...

  10. 取消VS2017窗口置顶

    今天打开VS2017,莫名其妙窗口置顶了,百度了一下如何取消窗口置顶,就是Ctrl+Alt+Esc组合键,就可以取消窗口置顶了,至于到底怎么会突然置顶的我也不知道emmm... /********** ...