使用 Elastic Search 进行条件查询
Conditional query with Elastic Search
我的 Elasticsearch 索引如下所示。
title:The Godfather
year:1972
genres:Crime & Drama
title:Lawrence of Arabia
year:1962
genres:Adventure,Biography &Drama
title:To Kill a Mockingbird
year:1973
genres:Mystery
title:Apocalypse Now
year:1975
genres:Thriller
我正在尝试在 elasticsearch 中编写查询,它应该首先检查 generes 字段是否包含 & 如果它包含则对相同字段执行其他匹配操作。如果generes不包含&,则需要进行其他匹配操作。基本上我在 Elasticsearch 中寻找 if 条件。
下面是我的查询,但它似乎工作不正常..
{
"query": {
"bool": {
"should": [
{
"regexp": {
"genres": ".*&.*"
}
},
{"match": {"genres": {"query": "Adventure"}}}
]
}
}
}
我正在关注以下关于 Whosebug 的建议。
您可以嵌套 bool 查询,所以您可以做的是仅使用 should 子句进行顶级 bool 查询,然后在该 should 子句内,您还有两个 bool 查询。每个都包含一个 must
部分,其中包含对 &
和 whatever else
的搜索。像这样
bool:
should:
- bool:
must: [ _search for & and whatever else_ ]
- bool:
must: [ _search for another criteria_]
希望对您有所帮助!
我的 Elasticsearch 索引如下所示。
title:The Godfather
year:1972
genres:Crime & Drama
title:Lawrence of Arabia
year:1962
genres:Adventure,Biography &Drama
title:To Kill a Mockingbird
year:1973
genres:Mystery
title:Apocalypse Now
year:1975
genres:Thriller
我正在尝试在 elasticsearch 中编写查询,它应该首先检查 generes 字段是否包含 & 如果它包含则对相同字段执行其他匹配操作。如果generes不包含&,则需要进行其他匹配操作。基本上我在 Elasticsearch 中寻找 if 条件。
下面是我的查询,但它似乎工作不正常..
{
"query": {
"bool": {
"should": [
{
"regexp": {
"genres": ".*&.*"
}
},
{"match": {"genres": {"query": "Adventure"}}}
]
}
}
}
我正在关注以下关于 Whosebug 的建议。
您可以嵌套 bool 查询,所以您可以做的是仅使用 should 子句进行顶级 bool 查询,然后在该 should 子句内,您还有两个 bool 查询。每个都包含一个 must
部分,其中包含对 &
和 whatever else
的搜索。像这样
bool:
should:
- bool:
must: [ _search for & and whatever else_ ]
- bool:
must: [ _search for another criteria_]
希望对您有所帮助!