elasticsearch - Elasticsearch Query DSL:字段的长度,如果字段存在
问题描述
假设我有一个字段,data.url
. 我们的一些日志包含此字段,有些则不包含。我只想返回data.url
长度超过 50 个字符的结果。真的,我只需要一个 URL 列表。
我正在努力:
GET _search
{
"query": {
"bool": {
"filter": {
"script": {
"script": {
"source": "doc['data.url'].value.length() > 50",
"lang": "painless"
}
}
}
}
}
}
但得到混合错误:
{
"error" : {
"root_cause" : [
{
"type" : "script_exception",
"reason" : "runtime error",
"script_stack" : [
"org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:90)",
"org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:41)",
"doc['data.url'].value.length() > 50",
" ^---- HERE"
],
"script" : "doc['data.url'].value.length() > 50",
"lang" : "painless",
"position" : {
"offset" : 4,
"start" : 0,
"end" : 35
}
},
或者
"type" : "script_exception",
"reason" : "runtime error",
"script_stack" : [
"org.elasticsearch.index.fielddata.ScriptDocValues$Strings.get(ScriptDocValues.java:496)",
"org.elasticsearch.index.fielddata.ScriptDocValues$Strings.getValue(ScriptDocValues.java:503)",
"doc['data.url'].value.length() > 50",
" ^---- HERE"
],
"script" : "doc['data.url'].value.length() > 50",
"lang" : "painless",
"position" : {
"offset" : 15,
"start" : 0,
"end" : 35
}
和
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "No field found for [data.url] in mapping with types []"
}
而有时
"caused_by" : {
"type" : "illegal_state_exception",
"reason" : "A document doesn't have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!"
}
这个领域肯定存在;我可以在日志中看到它,在搜索字段中搜索,并使用术语作品:
GET _search
{
"query": {
"bool": {
"filter": {
"term": {
"data.url": "www.google.com"
}
}
}
}
}
我错过了什么?
我正在使用 Elasticsearch 7.8。
解决方案
由于您使用的是版本 7.*,因此您需要使用以下脚本查询
{
"query": {
"bool": {
"filter": {
"script": {
"script": {
"source": "doc['data.url.keyword'].length > 50",
"lang": "painless"
}
}
}
}
}
}
如果data.url
字段是keyword
类型,则忽略".keyword"
字段末尾的
推荐阅读
- ios - 在单个 CAShapeLayer 中为多个 UIBezier 路径设置动画
- swift - Swift - 使用 Eureka 获取 textField 表单的值
- python - Python:为什么一个生成器比其他带有“yield”的生成器快?
- gensim - Gensim Word2Vec Model trained but not saved
- swift -
- r - r2pmml Error in .convert(tempfile, file, converter, converter_classpath, verbose) : 1
- spring-boot - Spring Boot Rest Controller API 不返回 json
- pandas - pd.describe 返回非零均值?
- php - my wamp 3.0.6 is not starting giving apache error.Can someone pleaserecommend?
- android - 需要通过登录从服务器上的自定义 API 获取令牌