【发布时间】:2014-10-08 09:36:08
【问题描述】:
我正在尝试查询在“内容”字段的正文中包含日期的文档。
curl -XGET 'http://localhost:9200/index/_search' -d '{
"query": {
"regexp": {
"content": "^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.]((19|20)\\d\\d)$"
}
}
}'
也许会更近一些?
curl -XGET 'http://localhost:9200/index/_search' -d '{
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"regexp":{
"content" : "^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.]((19|20)\\d\\d)$"
}
}
}
}'
我的正则表达式似乎已关闭。此正则表达式已在 regex101.com 上得到验证 以下查询仍然没有从我拥有的 175k 文档中返回任何内容。
curl -XPOST 'http://localhost:9200/index/_search?pretty=true' -d '{
"query": {
"regexp":{
"content" : "/[0-9]{4}-[0-9]{2}-[0-9]{2}|[0-9]{2}-[0-9]{2}-[0-9]{4}|[0-9]{2}/[0-9]{2}/[0-9]{4}|[0-9]{4}/[0-9]{2}/[0-9]{2}/g"
}
}
}'
我开始认为我的索引可能未针对此类查询设置。您必须使用什么类型的字段才能使用正则表达式?
mappings: {
doc: {
properties: {
content: {
type: string
}title: {
type: string
}host: {
type: string
}cache: {
type: string
}segment: {
type: string
}query: {
properties: {
match_all: {
type: object
}
}
}digest: {
type: string
}boost: {
type: string
}tstamp: {
format: dateOptionalTimetype: date
}url: {
type: string
}fields: {
type: string
}anchor: {
type: string
}
}
}
我想查找任何具有日期的记录并绘制该日期之前的文档量。第 1 步是让这个查询工作。步骤 2. 将提取日期并相应地按它们分组。有人可以建议一种让第一部分工作的方法,因为我知道第二部分真的很棘手。
谢谢!
【问题讨论】:
标签: regex elasticsearch