ElasticSearch和Regex查询 [英] ElasticSearch and Regex queries

查看:170
本文介绍了ElasticSearch和Regex查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查询在内容字段的正文内有日期的文档。

  curl -XGET'http:// localhost:9200 / index / _search'-d'{
:{
regexp:{
content:^(0 [1-9] | [12] [0-9] | 3 [01])[ - /。] 0 [1-9] | 1 [012])[ - /.]((19|20)\\\\\\d)$
}
}
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ p p p p p p p p p p p p p p p p p p p p p p p $ p> 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
}
}
}'

我开始认为我的索引可能不会被设置为这样的查询。您必须使用什么类型的字段才能使用正则表达式?

 映射:{
doc: {
属性:{
content:{
type:string
} title:{
type:string
} host:{
type :string
} cache:{
type:string
}段:{
type:string
}查询:{
属性:{
match_all:{
type:object
}
}
}摘要:{
type:string
} boost:{
type: string
} tstamp:{
格式:dateOptionalTimetype:date
} url:{
type:string
} fields:{
type:string
} anchor:{
type:string
}
}
}

我想查找具有日期的任何记录,并在该日期之前绘制文档的数量。第一步是让这个查询工作。第二步是将日期推迟,并将它们分组。有人可以建议一种办法让第一部分工作,因为我知道第二部分将非常棘手。



谢谢!

解决方案

你应该阅读Elasticsearch的正则表达式查询文档,您正在对正则表达式查询的工作方式做出一些不正确的假设。



可能这里最重要的是要了解你尝试匹配的字符串是。您正在尝试匹配术语,而不是整个字符串。如果这是与StandardAnalyzer进行索引,我怀疑,您的日期将被分为多个术语:



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