使用Elasticsearch进行布尔查询 [英] Boolean Query with Elasticsearch
问题描述
我当前正在使用以下查询-
I am currently using the following query -
{
"_source": [
"title",
"bench",
"id_",
"court",
"date",
"content"
],
"size": 15,
"from": 0,
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "the",
"fields": [
"content"
], "operator": "and"
}
},
],
"should": {
"multi_match": {
"query": "the",
"fields": [
"content.standard^2"
], "operator": "and"
}
}
}
}
,
"highlight": {
"pre_tags": [
"<tag1>"
],
"post_tags": [
"</tag1>"
],
"fields": {
"content": {}
},
"fragment_size": 100
}
}
具有以下映射
{
"courts_2": {
"mappings": {
"properties": {
"author": {
"type": "text",
"analyzer": "my_analyzer"
},
"bench": {
"type": "text",
"analyzer": "my_analyzer"
},
"citation": {
"type": "text"
},
"content": {
"type": "text",
"fields": {
"standard": {
"type": "text"
}
},
"analyzer": "my_analyzer"
},
"court": {
"type": "text"
},
"date": {
"type": "text"
},
"id_": {
"type": "text"
},
"title": {
"type": "text",
"fields": {
"standard": {
"type": "text"
}
},
"analyzer": "my_analyzer"
},
"verdict": {
"type": "text"
}
}
}
}
}
我的分析仪是 Metaphone
分析器。
这是我的目标。我希望完全匹配(标准)首先出现,然后是语音匹配。我能够用代码实现。我非常确定其中有一些不需要的逻辑,如果有人可以指出它,我将深表感谢。
My analyzer is a Metaphone
analyzer.
Here is my Aim. I want the exact matches ( standard ) to appear first, followed by phonetic ones. I am able to achieve that with the code. I am pretty sure that there is (are) some unwanted logic in this and would deeply appreciate if someone can point to it.
此外,我想加入的是用户可以输入搜索逻辑的搜索逻辑,例如
Additionally what I would like to incorporate is a search logic where a user can enter a search like
皇家马德里和巴塞罗那或曼联。
在这里,我希望所有包含皇家马德里和巴塞罗纳/曼联/的文件都可以使用已有的当前查询(经过修改)如何实现?
Real Madrid AND Barcelona OR Manchester United. Here I want all documents that contain Real Madrid and either Barcelona/Man Utd/ How do I achieve with the current query I have in place (with modifications)?
推荐答案
我在您的查询中看不到任何副作用。关于您的搜索逻辑,最简单的方法可能是使用查询字符串查询。默认情况下,它支持布尔运算符,因此您的查询可能类似于:
I can't see any sideeffects in your query. Regarding your search logic, the simplest approach would probably by using a query string query instead. It supports boolean operators by default, so your query could look something like this:
{
"_source": [
"title",
"bench",
"id_",
"court",
"date",
"content"
],
"size": 15,
"from": 0,
"query": {
"bool": {
"must": [
{
"query_string" : {
"query" : "Real Madrid AND Barcelona OR Manchester United",
"fields" : ["content", "title"],
"operator": "and"
}
}
],
"should": {
"query_string" : {
"query" : "Real Madrid AND Barcelona OR Manchester United",
"fields" : ["content.standard", "title.standard"],
"boost": 2,
"operator": "and"
}
}
}
},
"highlight": {
"pre_tags": [
"<tag1>"
],
"post_tags": [
"</tag1>"
],
"fields": {
"content": {}
},
"fragment_size": 100
}
}
否则,您将不得不解析查询并使用多个布尔查询为操作员建模。
Otherwise you would have to parse your query and model the operators with multiple bool queries.
这篇关于使用Elasticsearch进行布尔查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!