elasticsearch - 长度大于20的elasticsearch查询文本字段

标签 elasticsearch elasticsearch-5

我想使用以下方法查询名称长度为value(text)大于20的文件,但不起作用:

GET /groups/_search
{
    "query": {
        "bool" : {
            "must" : {
                "script" : {
                    "script" : "_source.name.values.length() > 20"
                }
            }
        }
    }
}

错误消息是:
{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "compile error",
        "script_stack": [
          "_source.name.values.lengt ...",
          "^---- HERE"
        ],
        "script": "_source.name.values.length() > 5",
        "lang": "painless"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "groups",
        "node": "exBbDVGeToSDRzLLmOh8-g",
        "reason": {
          "type": "query_shard_exception",
          "reason": "failed to create query: {\n  \"bool\" : {\n    \"must\" : [\n      {\n        \"script\" : {\n          \"script\" : {\n            \"inline\" : \"_source.name.values.length() > 5\",\n            \"lang\" : \"painless\"\n          },\n          \"boost\" : 1.0\n        }\n      }\n    ],\n    \"disable_coord\" : false,\n    \"adjust_pure_negative\" : true,\n    \"boost\" : 1.0\n  }\n}",
          "index_uuid": "_VH1OfpdRhmd_UPV7uTNMg",
          "index": "groups",
          "caused_by": {
            "type": "script_exception",
            "reason": "compile error",
            "script_stack": [
              "_source.name.values.lengt ...",
              "^---- HERE"
            ],
            "script": "_source.name.values.length() > ",
            "lang": "painless",
            "caused_by": {
              "type": "illegal_argument_exception",
              "reason": "Variable [_source] is not defined."
            }
          }
        }
      }
    ]
  },
  "status": 400
}

不知道我该怎么解决...

仅供引用:es的版本是5.4.0

我不知道以下相关问题:
无痛的script_fields无权访问_source变量#20068
https://github.com/elastic/elasticsearch/issues/20068

最佳答案

解决此问题的最佳方法是,也用name字段的长度来索引另一个字段,我们将其称为nameLength。这样,您就可以转移在索引时计算名称字段长度的负担,而不必(在重复时)在查询时进行计算。

因此,在建立索引时,如果您有一个像name这样的{"name": "A big brown fox"}字段,那么您将创建一个具有名称字段长度的新字段,例如{"name": "A big brown fox", "nameLength": 15}

在查询时,您将可以在range字段上使用简单快速的nameLength查询:

GET /groups/_search
{
    "query": {
        "bool" : {
            "must" : {
                "range" : {
                    "nameLength": {
                       "gt": 20
                    }
                }
            }
        }
    }
}

关于elasticsearch - 长度大于20的elasticsearch查询文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45811337/

相关文章:

javascript - Elasticsearch 按自定义项目重量排序

php - 合并键存储桶中的doc_count结果

elasticsearch - Elasticsearch自定义评分功能测试空日期值

elasticsearch - 更新符合特定条件的ElasticSearch文档

elasticsearch - 过滤ID值

database - ElasticSearch 7索引相对于ElasticSearch 5太大

c# - 如何使用Nest将具有数组值的settings属性添加到ES-index

elasticsearch - 使ElasticSearch显示上下文

elasticsearch - Elasticsearch 7 SQL CLI:./x-pack-env:无此类文件或目录错误

python - 使用 python 进行 Elastic Search 批量更新,如何使用新数据追加数组字段