主要知识点

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. Android Notification状态栏通知

    没有添加额外的震动及声音效果,这里直接实现了通知的功能,看效果吧: MainActivity.java package com.example.notification; import android ...

  2. Unreal Engine 4 Camera Lag(摄影机延迟)

    以官方的Third Person Template为样例,Character蓝图中的USpringArmComponent就实现了摄影机和场景碰撞和交互等大部分的功能了. 要实现摄影机延时,仅仅须要改 ...

  3. POJ 3233 Matrix Power Series 二分+矩阵乘法

    链接:http://poj.org/problem?id=3233 题意:给一个N*N的矩阵(N<=30),求S = A + A^2 + A^3 + - + A^k(k<=10^9). 思 ...

  4. jQuery - 广告图片轮播切换

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  5. n阶导函数存在与n阶可导的区别

    1.f(x)n阶导函数存在 <=======>  f(n)(x)存在  指的是在某个区间内有定义 2.f(x)n阶可导根据题意可以有两种不同的解释: ①.题目中说的是在某点即在x=x0处n ...

  6. Codeforces--630E--A rectangle(规律)

     E - A rectangle Crawling in process... Crawling failed Time Limit:500MS     Memory Limit:65536KB  ...

  7. hdoj--5625--Clarke and chemistry(枚举)

    Clarke and chemistry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  8. Python 39 数据库

    一:数据存储引擎 1. 什么是引擎? 一个功能的核心部分 引擎可以被分类 例如: 自然 增压 汽油 柴油 混合动力 天然气 核动力 汽油:动力弱,噪音小,震动小 柴油:动力强,污染大,噪音大,震动大 ...

  9. Mvc程序字体加载失败问题

    在我们开发的asp.net-mvc项目中,有时会出现字体加载失败的现象,但是一检查字体文件目录,发现文件目录都是存在的且有效的,这是为何呢?原来需要再web.config文件中添价少许配置代码就搞定. ...

  10. Cracking the Coding Interview 8.5

    Implement an algorithm to print all valid combinations of n-pairs of parentheses #include<stdio.h ...