【问题标题】:ElasticSearch:filtering documents based on field lengthElasticSearch:根据字段长度过滤文档
【发布时间】:2013-12-29 22:43:06
【问题描述】:


我在 SO 上阅读了几个类似的问题,并建议解决方案不起作用..
我想找到单词短于 8 的所有字段

我的数据库屏幕:

我尝试使用这个查询来做到这一点

{
  "query": {
    "match_all": {}
  },
  "filter": {
    "script": {
      "script": "doc['word'].length < 5"
    }
  }
}

我做错了什么?我错过了什么?

【问题讨论】:

    标签: lucene elasticsearch


    【解决方案1】:

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

    你可以例如使用regexp-filter 来查找具有一定长度的术语,其模式类似于.{0,4}

    这是一个您可以使用的可运行示例: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}"
                        }
                    }
                }
            }
        }
    }
    '
    

    【讨论】:

    • 谢谢你的作品可爱! :D 像 jsfiddle 这样的伟大网站,但用于弹性搜索 :) 我的错误是通过 get 发送请求;] 再次感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 2021-02-27
    • 2019-08-19
    • 2023-03-04
    相关资源
    最近更新 更多