简体   繁体   English

使用Java API在Spring Boot上的Elasticsearch中对数组元素排序不起作用

[英]Sort on arrays element don't work in Elasticsearch on Spring boot using java API

I got problem (or lack of knowledge) in sorting documents in elasticsearch. 我在Elasticsearch中对文档进行排序时遇到问题(或缺乏知识)。 Elasticsearch is local and managed by spring boot. Elasticsearch是本地的,由spring boot管理。 What im trying to do is to, using java API, search documents and sort them. 我试图用Java API搜索文档并对文档进行排序。 Te docs are looking something like that (little simplified): 文档看起来像这样(小简化):

{
(...)
"relatedDocuments": [{
    "_id": ObjectId("123123"),
    (...)
    "relationSet": [{
        "type": {
            "name": "Some name",
            "version": NumberLong(1)
        },
        "documentId": "123123",
        "content": {
            "numberToSearch": "U-2016-8"<---element to sort by
        }
    }]
    (...)
}]
(...)
}

As you can see the element to sort by is in object which is in array which is in other object holdet by another array... 如您所见,要排序的元素是在对象中,该对象在数组中,而在另一个数组中的其他对象保持架中...

Now I'm making some query which seems to work fine but sorting... like sorting never occured... the Java code looks something like: 现在,我正在进行一些查询,看起来不错,但是可以进行排序……就像从未发生过排序……Java代码看起来像这样:

        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        //some bool query created
        boolQuery.must(query);
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withSort(SortBuilders.fieldSort("relatedDocuments.relationSet.content.numberToSearch").order(SortOrder.ASC).sortMode("min")).withQuery(boolQuery).build();

        return elasticsearchTemplate.queryForList(searchQuery, GenericDocumentIndex.class);

the result is properly searched but not sorted at all... 结果已正确搜索,但根本没有排序...

If I change the fieldSort to something simplier like "relatedDocuments.id" then sorting work... 如果我将fieldSort更改为诸如“ relatedDocuments.id”之类的简单名称,则排序工作...

What is wrong here? 怎么了

EDIT 编辑
for reference resulted rekords are sorted as below: 供参考,结果rekords排序如下:

U-2016-5 U-2016-5
U-2016-6 U-2016-6
U-2016-7 U-2016-7
U-2016-4 U-2016-4
U-2016-8 U-2016-8
U-2016-9 U-2016-9
U-2016-12 U-2016-12
U-2016-11 U-2016-11
U-2016-10 U-2016-10

relationSet seems like a nested field in your schema. 

For Sorting on nested fields, you should refer to the link below 有关对嵌套字段进行排序,请参考以下链接

Reference : https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-sorting.html 参考: https : //www.elastic.co/guide/en/elasticsearch/guide/current/nested-sorting.html

"sort": [{
        "relatedDocuments.relationSet.content.numberToSearch": {
            "nested_path": "relatedDocuments.relationSet",
            "mode": "min",
            "order": "desc",
            "ignore_unmapped": false
        }
    }],

UPDATE : 更新:

You need to explicitly mark relationSet as nested via spring-boot 您需要通过spring-boot显式地将relationSet标记为嵌套

@Document(indexName = "indexName", type = "inxedType", shards = 1, replicas = 0)
public class RelatedDocuments {
@Field(type= FieldType.Nested)  // <-- mark it as nested
private List<Relation> relationSet;   
}

PS : delete the index and recreate before proceeding PS:删除索引并在继续之前重新创建

Ok
the issue wasn't in query or document structure but in field value... 问题不是查询或文档结构,而是字段值...

it was dash separated so elastic search didnt keep it as whole string but as separated tokens. 它用破折号分隔开,因此弹性搜索没有将其保留为整个字符串,而是分隔为多个标记。 Solution is to set this field (or add sub field) with index not_analyzed propertie... In java it whould be done by adding annotetion over the field like this or similar: 解决方案是使用索引not_analyzed属性设置该字段(或添加子字段)。在Java中,应通过在该字段上添加这样或类似的注释来完成:

@Field(
        type = FieldType.String,
        index = FieldIndex.not_analyzed
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM