通过(无痛)脚本对ElasticSearch进行排序-神秘地对数组值进行重复数据删除

时间:2019-06-02 12:48:48

标签: elasticsearch elasticsearch-painless

我正在PUT在ElasticSearch中放置以下文档:

{
    "_rootId": "327d3aba-4f7c-4abb-9ff3-b1608c354c7c",
    "_docId": "ID_3",
    "_ver": 0,
    "val_labels": [
        "x1",
        "x1",
        "x1"
    ]
}

然后,我GET使用一个painless脚本进行以下查询:

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "_rootId": "77394e08-32be-4611-bbf7-818dfe4bc853"
                    }
                }
            ]
        }
    },
    "sort": [
        {
            "_script": {
                "order": "desc",
                "type": "string",
                "script": {
                    "lang": "painless",
                    "source": "return doc['val_labels'].toString()"
                }
            }
        }
    ]
}

这是我收到的回复:

{
    "took": 30,
    "timed_out": false,
    "_shards": {
        "total": 12,
        "successful": 12,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": null,
        "hits": [
            {
                "_index": "my-index",
                "_type": "views",
                "_id": "77394e08-32be-4611-bbf7-818dfe4bc853.ID_3",
                "_score": null,
                "_source": {
                    "_rootId": "77394e08-32be-4611-bbf7-818dfe4bc853",
                    "_docId": "ID_3",
                    "_ver": 0,
                    "val_labels": [
                        "x1",
                        "x1",
                        "x1"
                    ]
                },
                "sort": [
                    "[x1]"
                ]
            }
        ]
    }
}

奇怪的是,响应中的val_labels字段显示["x1", "x1", "x1"](按预期,请参见插入的对象),而sort字段仅显示单个x1值。

对此有什么解释吗?

1 个答案:

答案 0 :(得分:1)

结果

字段_source是原始的未修改文档,而排序脚本正在访问doc values doc['val_labels'],它们是已处理的字段。可以通过显式获取docvalue_fields进行调试:

{
    "query": {
        "match_all": {}
    },
    "docvalue_fields": [
      "val_labels"
    ]
}

这会产生以下结果(我只索引了一个文档)

{
  "hits": [
    {
      "_index": "test",
      "_type": "_doc",
      "_id": "ID_3",
      "_score": 1,
      "_source": {
        "val_labels": [
          "x1",
          "x1",
          "x1"
        ]
      },
      "fields": {
        "val_labels": [
          "x1"
        ]
      }
    }
  ]
}

请注意结果中的重复数据删除值。这是因为多个相同的值导致词频出现增加

GET /test/_doc/ID_3/_termvectors?fields=val_labels
{
  "term_vectors": {
    "val_labels": {
      "field_statistics": {
        "sum_doc_freq": 1,
        "doc_count": 1,
        "sum_ttf": -1
      },
      "terms": {
        "x1": {
          "term_freq": 3,
          "tokens": [
            {
              "position": 0,
              "start_offset": 0,
              "end_offset": 2
            },
            {
              "position": 1,
              "start_offset": 3,
              "end_offset": 5
            },
            {
              "position": 2,
              "start_offset": 6,
              "end_offset": 8
            }
          ]
        }
      }
    }
  }
}