es根据字段长度过滤,ElasticSearch:根据字段长度过滤文档

I read couple of similar problems on SO and suggest solution not work..

I want to find all fields where word is shorter than 8

my database screen:

dfa1f71699a2dc675a194d7bac78b583.png

I tried to do this using this query

{

"query": {

"match_all": {}

},

"filter": {

"script": {

"script": "doc['word'].length < 5"

}

}

}

what I doing wrong? I miss something?

解决方案

Any field used in a script is loaded entirely into memory (http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html#_document_fields), so you may want to consider an alternative approach.

You can e.g. use the regexp-filter to just find terms of a certain length, with a pattern like .{0,4}.

#!/bin/bash

export ELASTICSEARCH_ENDPOINT="http://localhost:9200"

# Index documents

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d '

{"index":{"_index":"play","_type":"type"}}

{"word":"bar"}

{"index":{"_index":"play","_type":"type"}}

{"word":"barf"}

{"index":{"_index":"play","_type":"type"}}

{"word":"zip"}

'

# Do searches

# This will not match barf

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '

{

"query": {

"filtered": {

"filter": {

"regexp": {

"word": {

"value": ".{0,3}"

}

}

}

}

}

}

'

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值