Solr - 基于孩子排序文档

时间:2015-08-20 11:14:29

标签: solr

我有一个包含嵌套文档的模式,如下所示:

{
    "id":"227686",
    "ProductID":"227686",
    "type":"product",
    "SKU":"DAFA2A1F047E438B8462667F987D80A5",
    "Name":"product name",
    "ShortDescription":"s description",
    "UOM":"Unit",
    "UomSize":"48",
    "CategoryID":59,
    "CategoryName":"Produce",
    "ManufacturerID":322,
    "ManufacturerName":"-------",
    "Active":"true",
    "_version_":1509403723402575872,
    "_childDocuments_":[
    {
      "id":"227686_83",
      "type":"buyer",
      "BuyerID":83,
      "DisplayOrder":0,
      "ProductID":"227686"},
    {
      "id":"227686_86",
      "type":"buyer",
      "BuyerID":86,
      "DisplayOrder":10,
      "ProductID":"227686"},
    {
      "id":"227686_83_84",
      "type":"seller",
      "BuyerID":83,
      "SellerID":84,
      "SellerName":"-----",
      "ProductID":"227686"},
    {
      "id":"227686_83_89",
      "type":"seller",
      "BuyerID":83,
      "SellerID":89,
      "SellerName":"-----",
      "ProductID":"227686"},
    ]},

有没有办法查询,所以我可以按子文档的DisplayOrder字段排序父文档?

我想查询产品名称并获取特定买家的结果,我这样做:

http://localhost:8983/solr/dine/select?q=Name:"product name"&fq={!parent%20which=type:product v="type:buyer AND BuyerID=83"}&wt=json&indent=true&fl=*,[child%20parentFilter=type:product%20childFilter=%22((type:buyer%20AND%20BuyerSiteID:83)%20OR%20(type:seller%20AND%20BuyerSiteID:83))%22%20%20limit=1000]&rows=1000

但结果未按子文档的DisplayOrder字段排序。

感谢。

2 个答案:

答案 0 :(得分:0)

使用函数排序可以看到:https://blog.griddynamics.com/how-to-sort-parent-documents-by-child-attributes-in-solr

&sort={!parent which=doc_type:parent score=max v=’+doc_type:child +{!func}DisplayOrder’} asc

这意味着您需要添加一个字段来标识父级和子级。

答案 1 :(得分:-2)

您可以使用sort参数来实现此目的。

  

sort:sort参数以升序(asc)或降序(desc)顺序排列搜索结果。该参数可以与数字或字母内容一起使用。

您需要索引DisplayOrder字段或在架构中将其定义为DocValue,它不应该是多值字段。

如果你没有将DisplayOrder字段定义为DocValues,那么它不应被任何分析器标记,或者它应该使用KeywordTokenizer,它基本上产生单个术语。

您可以在此处找到更多信息。 Common Query Parameters