主要知识点

1Query 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

优质内容筛选与推荐>>
1、虚拟机使用静态IP不能上网
2、对项目目标的一点想法
3、下载并保存图片Python2.7
4、Zip文件口令破解机
5、android:fillViewport="true"让ScrollView内的view强行match_parent


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号