gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:30:13 31 4
gpt4 key购买 nike

我在 flex 搜索中对以下文档进行PUT编码:

{
"_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值。

有什么解释吗?

最佳答案

结果中的字段_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
}
]
}
}
}
}
}

关于elasticsearch - 通过(无痛)脚本对ElasticSearch进行排序-神秘地对数组值进行重复数据删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56415209/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com