Elasticsearch通过匹配数组项排序

我在索引文档中具有以下结构:

document1: "customLists":[{"id":8,"position":8},{"id":26,"position":2}]

document2: "customLists":[{"id":26,"position":1}]

document3: "customLists":[{"id":8,"position":1},{"id":26,"position":3}]

我可以使用匹配查询“ customLists.id =

26”来搜索属于给定列表的匹配文档。但是我需要根据该列表中的位置值对文档进行排序,并忽略其他列表的位置。

因此,预期结果将按document2,document1,document3的顺序排列

数据结构是否适合这种排序以及如何处理?

回答:

实现此目的的一种方法是将的映射类型设置

customLists为嵌套,然后使用按嵌套字段排序

范例:

put test

put test/test/_mapping

{

"properties": {

"customLists": {

"type": "nested",

"properties": {

"id": {

"type": "integer"

},

"position": {

"type": "integer"

}

}

}

}

}

    put test/test/1 

{

"customLists":[{"id":8,"position":8},{"id":26,"position":2}]

}

put test/test/2

{

"customLists":[{"id":26,"position":1}]

}

put test/test/3

{

"customLists":[{"id":8,"position":1},{"id":26,"position":3}]

}

post test/_search

{

"filter": {

"nested": {

"path": "customLists",

"query": {

"term": {

"customLists.id": {

"value": "26"

}

}

}

}

},

"sort": [

{

"customLists.position": {

"order": "asc",

"mode": "min",

"nested_filter": {

"term": {

"customLists.id": {

"value": "26"

}

}

}

}

}

]

}

以上是 Elasticsearch通过匹配数组项排序 的全部内容, 来源链接: utcz.com/qa/414313.html

回到顶部