Elasticsearch:根据日期范围过滤结果 [英] Elasticsearch : filter results based on the date range
问题描述
我正在使用Elasticsearch 6.6,尝试根据在日期范围内传递给查询(Bool)的多个值(email_address)提取多个结果/记录.例如:我想根据他们的email_address(annie@test.com,charles@test.com,heman@test.com)以及时期(即project_date(2019-01-01))提取有关几名员工的信息.
I'm using Elasticsearch 6.6, trying to extract multiple results/records based on multiple values (email_address) passed to the query (Bool) on a date range. For ex: I want to extract information about few employees based on their email_address (annie@test.com, charles@test.com, heman@test.com) and from the period i.e project_date (2019-01-01).
我确实使用过应表达式,但不幸的是它根据日期范围从Elasticsearch中提取所有记录,即甚至从project_date 2019-01-01中提取其他员工信息.
I did use should expression but unfortunately it's pulling all the records from elasticsearch based on the date range i.e. it's even pulling other employees information from 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" }}}
]
}
}
}
我也尝试过必须表达,但没有结果.您能帮我使用日期范围内的email_address查找员工吗?
I also tried must expression but getting no result. Could you please help me on finding employees using their email_address with the date range?
谢谢.
推荐答案
应(或)子句是可选的
引用这篇文章.在查询中,如果存在必须查询和过滤器查询,则应查询应该会影响分数.但是,如果布尔查询处于过滤器上下文中,或者既没有必须查询也没有过滤器查询,则至少应有一个查询查询必须与文档匹配."
Quoting from this article. "In a query, if must and filter queries are present, the should query occurrence then helps to influence the score. However, if bool query is in a filter context or has neither must nor filter queries, then at least one of the should queries must match a document."
因此,在您的查询中,仅应影响分数,而不实际过滤文档.您必须将must包装在must中,或将其移动到过滤器中(如果不需要评分).
So in your query should is only influencing the score and not actually filtering the document. You must wrap should in must, or move it in filter(if scoring not required).
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"
}
}
]
}
}
]
}
}
}
您也可以使用@AlwaysSunny的答案中的条款子句替换should子句.
You can also replace should clause with terms clause as in @AlwaysSunny's answer.
这篇关于Elasticsearch:根据日期范围过滤结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!