弹性搜索过滤部分日期 [英] Elasticsearch filtering by part of date

查看:84
本文介绍了弹性搜索过滤部分日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

QUESTION 也许有人有解决方案如何按月或日期过滤/查询ElasticSearch数据?让我们来看看今天庆祝生日的所有用户。



映射

  
映射:
dob:{type:date,format:dd-MM-yyyy HH:mm:ss || yyyy-MM-dd'THH:mm:ss'Z '|| yyyy-MM-dd'T'HH:mm:ss + SSSS}

以这种方式存储:

 dob:1950-06-03T00:00:00Z 

主要问题是如何按月和日搜索用户。因为生日是每年都不知道的。






解决方案
我发现使用通配符查询生日的解决方案。我们知道如果我们想要使用通配符,则字段的映射必须是一个字符串,所以我使用多字段映射。

  
映射:
dob:
类型:multi_field
字段:
dob:{type:date,format:yyyy-MM-dd 'T'HH:mm:ss'Z'}
string:{type:string,index:not_analyzed}

,并通过仅限月份和日期获取用户的查询是:

  { 
query:{
wildcard:{
dob.string:* -06-03 *
}
}

注意
此查询可能很慢,因为它需要重复许多条款。



结论
这不是很好的方式,但它是唯一一个我'

解决方案

根据您的问题,我假设您要查询, ñ一个过滤器(它们不同),您可以使用与范围查询相结合的日期数学/格式。



请参阅:范围查询用于



为了解释日期数学,请参阅以下链接



curl -XPOST http:// localhost:9200 / twitter / tweet / _search -d

  {
查询:{
range:{
birthday:{
gte:2014-01-01,
lte:2014-01-01
}
}
}
}

我用最新的弹性搜索测试了这个。 p>

QUESTION Maybe anyone has solution how to filter/query ElasticSearch data by month or day ? Let's say I need to get all users who celebrating birthdays today.

mapping


mappings:
    dob: { type: date, format: "dd-MM-yyyy HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss'Z'||yyyy-MM-dd'T'HH:mm:ss+SSSS"}

and stored in this way:

 dob: 1950-06-03T00:00:00Z 

main problem is how to search users by month and day only. Ignore the year, because birthday is annually as we know.


SOLUTION I found solution to query birthdays with wildcards. As we know if we want use wildcards, the mapping of field must be a string, so I used multi field mapping.


mappings:
    dob:
        type: multi_field
        fields:
            dob: { type: date, format: "yyyy-MM-dd'T'HH:mm:ss'Z'}
            string: { type: string, index: not_analyzed }

and query to get users by only month and day is:

{
    "query": {
        "wildcard": {
            "dob.string": "*-06-03*"
        }
    }
}

NOTE This query can be slow, as it needs to iterate over many terms.

CONCLUSION It's not pretty nice way, but it's the only one I've found and it works!.

解决方案

Based on your question I am assuming that you want a query, and not a filter (they are different), you can use the date math/format combined with a range query.

See: range query for usage

For explanation of date math see the following link

curl -XPOST http://localhost:9200/twitter/tweet/_search -d

{
  "query": {
    "range": {
        "birthday": {
            "gte" : "2014-01-01",
            "lte" : "2014-01-01"
        }
    }
  }
}

I have tested this with the latest elastic search.

这篇关于弹性搜索过滤部分日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆