具有多个 and 和 or 条件的 Elasticsearch 嵌套查询
Elasticsearch nested query with multiple and and or conditions
我正在尝试在 elasticsearch 中生成嵌套的 must
和 should
查询以模拟以下条件:
( (brandsvisited ilike '%adidas%' or brandsvisited ilike '%skechers%' ) or( placecategory ilike '%Pizza Restaurant Visitors%' or placecategory ilike '%Grocery Store Visitors%' ) and( gender='Male' ) ) and polygonid = '1465'
我在 elasticsearch 中创建了以下查询。
"query": {
"bool": {
"must": [
{"term": {"polygonid": "1465"}},
{"term": {"gender": "Male"}},
{
"bool": {
"should": [
{"term": {"brandsvisited": "adidas"}},
{"term": {"brandsvisited": "skechers"}}
],"minimum_should_match": 1
}
},
{
"bool": {
"should": [
{"term": {"placecategory": "Pizza Restaurant Visitors"}},
{"term": {"placecategory": "Grocery Store Visitors"}}
],"minimum_should_match": 1
}
}
]
}
}
以上查询returns 零个结果。但是,如果我删除最后一个布尔值应该查询,它 returns 结果。不确定我哪里出错了。
假设placecategory
字段为文本类型。您需要将其替换为 placecategory.keyword
字段(如果您尚未定义任何显式索引映射)
Term query returns 在提供的字段中包含确切术语的文档。如果您还没有定义任何显式索引映射,那么您需要将 .keyword 添加到该字段。这使用关键字分析器而不是标准分析器。
如下所示修改您的查询
{
"query": {
"bool": {
"must": [
{
"term": {
"polygonid": "1465"
}
},
{
"term": {
"gender": "Male"
}
},
{
"bool": {
"should": [
{
"term": {
"brandsvisited": "adidas"
}
},
{
"term": {
"brandsvisited": "skechers"
}
}
],
"minimum_should_match": 1
}
},
{
"bool": {
"should": [
{
"term": {
"placecategory.keyword": "Pizza Restaurant Visitors"
}
},
{
"term": {
"placecategory.keyword": "Grocery Store Visitors"
}
}
],
"minimum_should_match": 1
}
}
]
}
}
}
如果您想在 placecategory
中搜索确切的术语,那么您可以使用带有 should
子句的术语查询或 terms query, otherwise, if you want to match phrase in the placecategory
field, then you can use match_phrase 查询
我正在尝试在 elasticsearch 中生成嵌套的 must
和 should
查询以模拟以下条件:
( (brandsvisited ilike '%adidas%' or brandsvisited ilike '%skechers%' ) or( placecategory ilike '%Pizza Restaurant Visitors%' or placecategory ilike '%Grocery Store Visitors%' ) and( gender='Male' ) ) and polygonid = '1465'
我在 elasticsearch 中创建了以下查询。
"query": {
"bool": {
"must": [
{"term": {"polygonid": "1465"}},
{"term": {"gender": "Male"}},
{
"bool": {
"should": [
{"term": {"brandsvisited": "adidas"}},
{"term": {"brandsvisited": "skechers"}}
],"minimum_should_match": 1
}
},
{
"bool": {
"should": [
{"term": {"placecategory": "Pizza Restaurant Visitors"}},
{"term": {"placecategory": "Grocery Store Visitors"}}
],"minimum_should_match": 1
}
}
]
}
}
以上查询returns 零个结果。但是,如果我删除最后一个布尔值应该查询,它 returns 结果。不确定我哪里出错了。
假设placecategory
字段为文本类型。您需要将其替换为 placecategory.keyword
字段(如果您尚未定义任何显式索引映射)
Term query returns 在提供的字段中包含确切术语的文档。如果您还没有定义任何显式索引映射,那么您需要将 .keyword 添加到该字段。这使用关键字分析器而不是标准分析器。
如下所示修改您的查询
{
"query": {
"bool": {
"must": [
{
"term": {
"polygonid": "1465"
}
},
{
"term": {
"gender": "Male"
}
},
{
"bool": {
"should": [
{
"term": {
"brandsvisited": "adidas"
}
},
{
"term": {
"brandsvisited": "skechers"
}
}
],
"minimum_should_match": 1
}
},
{
"bool": {
"should": [
{
"term": {
"placecategory.keyword": "Pizza Restaurant Visitors"
}
},
{
"term": {
"placecategory.keyword": "Grocery Store Visitors"
}
}
],
"minimum_should_match": 1
}
}
]
}
}
}
如果您想在 placecategory
中搜索确切的术语,那么您可以使用带有 should
子句的术语查询或 terms query, otherwise, if you want to match phrase in the placecategory
field, then you can use match_phrase 查询