并以弹性返回前10位

并以弹性返回前10位

本文介绍了对嵌套数组进行排序,并以弹性返回前10位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在弹性索引中有一个嵌套数据类型,并希望对所有返回结果按升序进行排序.我尝试了以下方法:

I have a nested data type in an elastic index and want to sort this ascending for all returned results. I have tried the following:

GET indexname/_search
{
                "_source" : ["m_iTopicID", "m_iYear", "m_Companies"],
                "query": {
                    "terms":{
                        "m_iTopicID": [11,12,13]
                    }
                },
   "sort" : [
       {
          "m_Companies.value" : {
             "order" : "asc",
             "nested_path" : "m_Companies"
          }
       }
    ]
}

索引的映射如下:

{
  "indexname": {
    "mappings": {
      "topicyear": {
        "properties": {
          "m_Companies": {
            "type": "nested",
            "properties": {
              "name": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "value": {
                "type": "float"
              }
            }
          },
          "m_People": {
            "type": "nested",
            "properties": {
              "name": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "value": {
                "type": "float"
              }
            }
          },
          "m_Places": {
            "type": "nested",
            "properties": {
              "name": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "value": {
                "type": "float"
              }
            }
          },
          "m_Subtopics": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "m_fActivation": {
            "type": "float"
          },
          "m_iDocBodyWordCnt": {
            "type": "long"
          },
          "m_iNodeID": {
            "type": "long"
          },
          "m_iTopicID": {
            "type": "long"
          },
          "m_iYear": {
            "type": "long"
          },
          "m_szDocID": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "m_szDocTitle": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "m_szGeo1": {
            "type": "nested",
            "properties": {
              "name": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "value": {
                "type": "float"
              }
            }
          },
          "m_szSourceType": {
            "type": "nested",
            "properties": {
              "name": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "value": {
                "type": "float"
              }
            }
          },
          "m_szSrcUrl": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "m_szTopicNames": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

这将返回ID为11、12或13的所有主题以及m_Companies的列表...,但列表未按值字段升序排列.

This returns all topics with ID 11, 12 or 13 with a list of m_Companies... but the lists aren't sorted ascending by the value field.

然后,我只想返回每个列表的前10名.因此,列表不会像当前一样返回数百,而仅返回n.如果我无法实现此选项,我将使用javascript splice(0,10)在前端获得前10名,但是如果Elastic可以为我做到这一点将是很棒的.

I would then like to only return the top 10 of each list. So the list doesn't return hundreds like currently but just n. If I can't achieve this option I will just obtain the top 10 at the front-end with a javascript splice(0,10) but it would be great if elastic could do this for me.

谢谢.

推荐答案

由于您在主/父级查询中提供了排序,因此将仅对父/根文档进行排序.您可能会发现,结果以m_Companes.value的最小值对文档进行排序.

Since you provided the sort in the main/parent level query, this will sort only the parent/root documents. As you might have observed with the results that documents are sorted with minimum value for m_Companes.value.

要对每个文档的嵌套文档进行排序,您必须深入嵌套文档内部并应用排序,因为m_Companies是父文档中的子文档.您必须使用嵌套的inner_hits,然后对 inner_hits .

To sort the nested documents for each document you have to go deep inside the nested document and apply sort as m_Companies are subdocuments in the parent document. You have to use nested inner_hits and then sort the inner_hits.

这个 github 问题有一个很好的例子,说明了我试图解释为如何根据嵌套文档中的值仅对父/根文档进行排序.

This github issue has very good example of what i was trying to explain as how this sorts only the parent/root document based on values in nested documents.

由于您希望嵌套所有文档,因此可以让嵌套查询使用match_all获取所有嵌套文档并根据值字段进行排序.

Since you want all documents in nested, so you can let the nested query to fetch all nested documents using match_all and sort based on value field.

您可以使用以下查询

{
    "_source": ["m_iYear", "m_Companies"],
    "query": {
        "bool": {
            "must": [{
                    "terms": {
                        "m_iTopicID": [11, 12, 13]
                    }
                },
                {
                    "nested": {
                        "path": "m_Companies",
                        "query": {
                            "match_all": {}
                        },
                        "inner_hits": {
                            "sort": [{
                                "m_Companies.value": "asc"
                            }]
                        }
                    }
                }
            ]
        }
    },
    "sort": [{
        "m_Companies.value": {
            "order": "asc",
            "nested_path": "m_Companies"
        }
    }]
}

希望这会有所帮助,谢谢

Hope this helps,Thanks

这篇关于对嵌套数组进行排序,并以弹性返回前10位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 05:39