我在将辅助过滤器应用于以下我的elasticsearch查询时遇到问题。仅第一个过滤器匹配。我希望两个过滤器都适用于查询。
"query": {
"bool": {
"must": [
{
"bool": {
"filter": {
"range": {
"@timestamp": {
"gte": "2019-03-12",
"lte": "2019-03-13"
}
}
}
}
},
{
"bool": {
"filter": {
"bool": {
"must": {
"match": {
"msg_text": "foo AND bar"
}
}
}
}
}
}
]
}
}
最佳答案
我已经提到了两种解决方案,第一种使用Match Query,第二种使用Query String。
我还假设msg_text
字段的类型为text
。
区别在于, query_string
使用解析器,该解析器将根据AND, OR
等运算符来解析您提到的文本。
虽然 match query
会读取文本,但会分析文本并基于该文本构造bool
查询。从某种意义上说,您无需提及运算符,它将不起作用
您可以在我提到的链接中阅读有关它们的更多信息。
1.使用匹配查询
POST <your_index_name>/_search
{
"query":{
"bool":{
"filter":{
"bool":{
"must":[
{
"range":{
"@timestamp":{
"gte":"2019-03-12",
"lte":"2019-03-13"
}
}
},
{
"match":{
"msg_text":"foo bar"
}
}
]
}
}
}
}
}
2.使用查询字符串
POST <your_index_name>/_search
{
"query":{
"bool":{
"filter":{
"bool":{
"must":[
{
"range":{
"@timestamp":{
"gte":"2019-03-12",
"lte":"2019-03-13"
}
}
},
{
"query_string":{
"fields": ["msg_text"], <----- You can add more fields here using comma as delimiter
"query":"foo AND bar"
}
}
]
}
}
}
}
}
从技术上讲,从某种意义上讲,您的解决方案没有问题,这是可行的,但是我希望我的答案清楚,可以简化查询并帮助您了解您要做什么。
让我知道是否有帮助!
关于elasticsearch - 使用ElasticSearch查询进行过滤和匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56246709/