弹性搜索条件字段排序:无法从 [java.util.ArrayList] 转换为 [double]

问题描述 投票:0回答:1

是否可以有条件地对Elasticsearch文档进行排序?情况很清楚,在我的脚本中可见。当存在某个字段值时,我想按某些字段集合进行排序,否则按不同的字段集合进行排序。字段有不同的类型(字符串、数字)。我想将数字排序为数字(而不是字符串),以便在升序排序时这是正确的顺序(假设 field1 是字符串,field2 是数字)。

只取field1和field2,field3为空:

ASC:(A 是第一部分,因此转到顶部,然后对数字进行排序)

doc1: AA 2
doc2: H 1
doc3: H 2

描述:

doc1: H 2
doc2: H 1
doc3: AA 2

当我们添加其他文档时,字段field4-field7我们应该有

ASC:

H 1 (doc with field1 and field2) 
H 2 (doc with field1 and field2)
1 X 25/2024 (doc with fields4-7)
12 AR 25/2024 (doc with fields4-7)

我使用 spring-data-elasticsearch:

 Script script = new Script("myscriptcontent ");
                    ScriptSortBuilder order = SortBuilders.scriptSort(script, ScriptSortBuilder.ScriptSortType.NUMBER)
                            .order(direction.isAscending() ? SortOrder.ASC : SortOrder.DESC);
return nativeSearchQueryBuilder.withSort(order).build();

ScriptSortBuilder
需要存在scriptsorttype,但是只能有String和Number,这可能不适合这里..是否可以使用spring-data-elasticsearch构建这样的查询?

我在日志中看到的查询排序部分:

{
  "_script" : {
    "script" : {
      "source" : "if (doc['somefield.keyword'].size() > 0) { return [doc['field1.keyword'].value, doc['field2.keyword'].value,  doc['field3.keyword'].value]; } else { return [doc['field4.keyword'].value, doc['field5.keyword'].value, doc['field6.keyword'].value, doc['field6.keyword'].value]; } ",
      "lang" : "painless"
    },
    "type" : "number",
    "order" : "desc"
  }
}

效果是这样的例外:

Suppressed: org.elasticsearch.client.ResponseException: method [POST], host [http://localhost:9200], URI [/myindex/_search?typed_keys=true&max_concurrent_shard_requests=5&search_type=query_then_fetch&batched_reduce_size=512], status line [HTTP/1.1 400 Bad Request]
{"error":{"root_cause":[{"type":"script_exception","reason":"compile error","script_stack":["... d'].size() > 0) { return [doc['field1.k ...","                             ^---- HERE"],"script":"if (doc['somefield.keyword'].size() > 0) { return [doc['field1.keyword'].value, doc['field2.keyword'].value,  doc['field3.keyword'].value]; } else { return [doc['field4.keyword'].value, doc['field5.keyword'].value, doc['field6.keyword'].value, doc['field7.keyword'].value]; } ","lang":"painless","position":{"offset":56,"start":31,"end":81}}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"rt","node":"xZy_xw9-R0e-JSfMIeyzBg","reason":{"type":"script_exception","reason":"compile error","script_stack":["... d'].size() > 0) { return [doc['field1.k ...","                             ^---- HERE"],"script":"if (doc['somefield.keyword'].size() > 0) { return [doc['field1.keyword'].value, doc['field2.keyword'].value,  doc['field3.keyword'].value]; } else { return [doc['field4.keyword'].value, doc['field5.keyword'].value, doc['field6.keyword'].value, doc['field7.keyword'].value]; } ","lang":"painless","position":{"offset":56,"start":31,"end":81},"caused_by":{"type":"class_cast_exception","reason":"Cannot cast from [java.util.ArrayList] to [double]."}}}]},"status":400}
..............
3.0.0.war//org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:2171)
        ... 153 more
elasticsearch spring-data-elasticsearch elasticsearch-painless
1个回答
0
投票

您正在使用本机查询(NativeSearchQuery,即来自以前版本的 Spring Data Elasticsearch,使用已弃用的

RestHighLEvelCLient
),当 Spring Data Elasticsearch 无法生成所需的查询、脚本等时,可以而且必须使用本机查询。因此答案是否定的。 Spring Data Elasticsearch 无法做到这一点。

© www.soinside.com 2019 - 2024. All rights reserved.