我正在使用 Elasticsearch 6.6,尝试根据在日期范围内传递给查询 (Bool) 的多个值 (email_address) 提取多个结果/记录。例如:我想根据他们的电子邮件地址(annie@test.com、charles@test.com、heman@test.com)和时间段(即项目日期(2019-01-01))提取有关少数员工的信息。
我确实使用了 should 表达式,但不幸的是,它根据日期范围从 elasticsearch 中提取所有记录,即它甚至从 project_date 2019-01-01 中提取其他员工信息。
{
"query": {
"bool": {
"should": [
{ "match": { "email_address": "annie@test.com" }},
{ "match": { "email_address": "chalavadi@test.com" }}
],
"filter": [
{ "range": { "project_date": { "gte": "2019-08-01" }}}
]
}
}
}
我也试过 must 表达式但没有结果。你能帮我找到使用日期范围内的 email_address 的员工吗?
提前致谢。
最佳答案
应该(或)子句是可选的
引自this文章。 “在查询中,如果存在 must 和 filter 查询,则 should 查询的出现将有助于影响分数。但是,如果 bool 查询在过滤上下文中或既没有 must 也没有 filter 查询,则至少有一个 should 查询必须匹配文档。”
因此在您的查询中,should 只会影响分数,而不会实际过滤文档。您必须将 should 包装在 must 中,或将其移动到过滤器中(如果不需要评分)。
GET employeeindex/_search
{
"query": {
"bool": {
"filter": {
"range": {
"projectdate": {
"gte": "2019-01-01"
}
}
},
"must": [
{
"bool": {
"should": [
{
"term": {
"email.raw": "abc@text.com"
}
},
{
"term": {
"email.raw": "efg@text.com"
}
}
]
}
}
]
}
}
}
您也可以将 should 子句替换为 @AlwaysSunny 的回答中的 terms 子句。
关于 Elasticsearch : filter results based on the date range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58043263/