Elasticsearch 实战Query DSL搜索语法
Elasticsearch实战
本课时主要介绍以下三种语法
must 必须满足;
should 如果包含了must或者filter查询,那么should的查询语句就不是或者的意思了,而是有或者没有都行的含义;使用minimum_should_match
,至少匹配一项should
子句;
must_not 必须不满足;
step1
准备三条介绍技术的文档
POST /technology/type/_bulk
{"index":{"_id":1}}
{"title":"ES搜索引擎","content":"Elasticsearch是一个基于Lucene的搜索服务器。","creator":"Lucene","cost":60}
{"index":{"_id":2}}
{"title":"Solr搜索引擎","content":"Solr是一个高性能,采用Java开发,基于Lucene的全文搜索服务器","creator":"Lucene","cost":20}
{"index":{"_id":3}}
{"title":"RocketMQ","content":"消息队列 RocketMQ 版(原ONS)是阿里云基于 Apache RocketMQ构建的低延迟、高并发、高可用、高可靠的分布式消息中间件。","creator":"Alibaba","cost":30}
step2
需求查询‘’搜索引擎“,内容可以包含“消息队列”也可以不包含,不可以来自Alibaba
GET /technology/type/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "搜索引擎"
}
}
],
"should": [
{
"match": {
"content": "消息队列"
}
}
],
"must_not": [
{
"match": {
"creator": "Alibaba"
}
}
]
}
}
}
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 530,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.6365592,
"hits" : [
{
"_index" : "technology",
"_type" : "type",
"_id" : "1",
"_score" : 1.6365592,
"_source" : {
"title" : "ES搜索引擎",
"content" : "Elasticsearch是一个基于Lucene的搜索服务器。",
"creator" : "Lucene",
"cost" : 60
}
},
{
"_index" : "technology",
"_type" : "type",
"_id" : "2",
"_score" : 1.6365592,
"_source" : {
"title" : "Solr搜索引擎",
"content" : "Solr是一个高性能,采用Java开发,基于Lucene的全文搜索服务器",
"creator" : "Lucene",
"cost" : 20
}
}
]
}
}
step3
使用"minimum_should_match": 1
GET /technology/type/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "搜索引擎"
}
}
],
"should": [
{
"match": {
"content": "消息队列"
}
}
],
"must_not": [
{
"match": {
"creator": "Alibaba"
}
}
],
"minimum_should_match": 1
}
}
}
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
step4
没有must或者filter查询表示或
GET /technology/type/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"creator": "Lucene"
}
},
{
"match": {
"title": "RocketMQ"
}
}
]
}
}
}
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.3962393,
"hits" : [
{
"_index" : "technology",
"_type" : "type",
"_id" : "3",
"_score" : 1.3962393,
"_source" : {
"title" : "RocketMQ",
"content" : "消息队列 RocketMQ 版(原ONS)是阿里云基于 Apache RocketMQ构建的低延迟、高并发、高可用、高可靠的分布式消息中间件。",
"creator" : "Alibaba",
"cost" : 30
}
},
{
"_index" : "technology",
"_type" : "type",
"_id" : "1",
"_score" : 0.4700036,
"_source" : {
"title" : "ES搜索引擎",
"content" : "Elasticsearch是一个基于Lucene的搜索服务器。",
"creator" : "Lucene",
"cost" : 60
}
},
{
"_index" : "technology",
"_type" : "type",
"_id" : "2",
"_score" : 0.4700036,
"_source" : {
"title" : "Solr搜索引擎",
"content" : "Solr是一个高性能,采用Java开发,基于Lucene的全文搜索服务器",
"creator" : "Lucene",
"cost" : 20
}
}
]
}
}
step5
GET /technology/type/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"creator": "Lucene"
}
},
{
"match": {
"title": "RocketMQ"
}
}
],
"minimum_should_match": 2
}
}
}
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
欢迎关注公众号《小马JAVA》
推荐阅读
-
《ElasticSearch6.x实战教程》之简单搜索、Java客户端(上)
-
Elasticsearch Query DSL 整理总结(二)—— 要搞懂 Match Query,看这篇就够了
-
ElasticSearch实战系列二: ElasticSearch的DSL语句使用教程---图文详解
-
Elasticsearch结构化搜索_在案例中实战使用term filter来搜索数据
-
ElasticSearch 搜索引擎入门到实战 22-- 查询出的结果高亮显示
-
Elasticsearch 实战Query DSL搜索语法
-
elasticsearch结合canal实战-问题搜索系统
-
Elasticsearch实战系列(五)--搜索数据
-
Elasticsearch系列---实战搜索语法
-
Elasticsearch7.x搜索实战