ElasticSearch:根据字段长度过滤文档 [英] ElasticSearch:filtering documents based on field length

查看:3047
本文介绍了ElasticSearch:根据字段长度过滤文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在SO上阅读了几个类似的问题,建议解决方案不起作用。

我想查找字短于8的所有字段


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

我的数据库屏幕:

我尝试使用此查询执行此操作

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?

推荐答案

脚本中使用的任何字段都完全加载到内存中( http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/ modules-scripting.html#_document_fields ),所以你可能想考虑另一种方法。

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.

你可以使用正则表达式过滤器只需找到一定长度的条款,其格局就像。{0,4}

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

这是一个可运行的示例,您可以玩: https://www.found.no/play/gist/2dcac474797b0b2b952a

Here's a runnable example you can play with: https://www.found.no/play/gist/2dcac474797b0b2b952a

#!/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}"
                    }
                }
            }
        }
    }
}
'

这篇关于ElasticSearch:根据字段长度过滤文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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