首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在elasticsearch中根据索引字段值(Data)的优先级获取搜索结果

如何在elasticsearch中根据索引字段值(Data)的优先级获取搜索结果
EN

Stack Overflow用户
提问于 2020-03-03 19:38:34
回答 1查看 40关注 0票数 2

让我们假设我正在搜索一个关键字“数据结构”,我们有3本不同的书,包括关键字“数据结构”,它们有3种不同的装订风格(例如。平装本、精装本和电子书)和ISBN是不同的装订风格。如果我对每本书分别进行索引,那么我总共会得到9 (3*3)个索引文档,所有文档都会出现在搜索结果中。假设我优先选择装帧风格: 1-平装本,2-精装本,3-电子书。因此,根据装帧方式的优先级,只有三种结果(图书-1的平装,图书-2的平装,图书-3的平装)。如果Book-2的平装本不可用,则结果应该是( book-1的平装本,Book-2的精装本,Book-3的平装本)。

我的索引文档是:

代码语言:javascript
复制
        "bindingStyle": "HBK3",
        "keywords": "V AMERICAN 30 ADOLESCENT PSYCHIATRY ANNALS SOCIETY",
        "subjectGroup": "Professional (DRM-Free)",
        "isbn": "9780881634624",
        "imprint": "Routledge",
        "edition": 1,
        "subjectGroupCode": "150",
        "originator": [
            {
                "role": "HG",
                "name": "Lois T. Flaherty",
                "id": 2808300
            }
        ],
        "title": "Adolescent Psychiatry, V. 30",
        "reviewsForExport": null,
        "features": null,
        "cpdClassification": false,
        "titleNotTokenized": false,
        "id": 74300,
        "recentBooks": true,
        "relatedBindings": [
            "9781138005921",
            "9780203837597"
        ],
        "sku": "ER9247",
        "publicationDate": 1191369600000,
        "backCoverCopy": "<P>Lois T Flaherty, M.D., is a child and adolescent psychiatrist on the teaching faculty of Harvard University and the University of Maryland School of Medicine. A past president of the American Society for Adolescent Psychiatry and a consultant to the Center for School Mental Health Assistance in Baltimore, Dr. Flaherty remains active in school-based mental health programs and community psychiatry.</P>",
        "hdclassification": false,
        "countryRestriction": [],
        "shortDescription": "The period of adolescence can be a time of great creativity, as new intellectual capacities emerge, and as the individual adolescent attempts to make sense out of inner and outer experience. Volume 30 of Adolescent Psychiatry addresses the ways in which adolescent experience is transmuted into ",
        "pdClassification": false,
        "bestSellerStatus": false,
        "application": "UBW",
        "series": [],
        "sortByPublicationDate": 1191369600000,
        "subtitle": "The Annals of the American Society for Adolescent Psychiatry",
        "gtLastUpdate": 1536932063000,
        "imprintCode": "IMPR",
        "isbn10": "088163462X",
        "category": [
            "SCBE0505",
            "SCBE0545"
        ],
        "inventoryStatusCode": [
            {
                "distributionCenterCode": "USNY",
                "inventoryStatusCode": "LFB"
            },
            {
                "distributionCenterCode": "LOC1",
                "inventoryStatusCode": "LFB"
            },
            {
                "distributionCenterCode": "SING",
                "inventoryStatusCode": "LFB"
            },
            {
                "distributionCenterCode": "USFL",
                "inventoryStatusCode": "LFB"
            },
            {
                "distributionCenterCode": "AUS",
                "inventoryStatusCode": "LFB"
            }
        ]

我写的搜索查询是:

代码语言:javascript
复制
   "query":{ 
      "bool":{ 
         "must":[ 

         ],
         "must_not":[ 
            { 
               "match":{ 
                  "countryRestriction":"US"
               }
            },
            { 
               "match":{ 
                  "inventoryStatusCode":"PLZ"
               }
            },
            { 
               "match":{ 
                  "imprintCode":"IMPGP"
               }
            }
         ],
         "should":[ 
            { 
               "query_string":{ 
                  "query":"Handbook of Bipolar Disorder",
                  "fields":[
                     "title",
                     "subtitle",
                     "isbn",
                     "isbn10",
                     "keywords"
                  ]
               }
            },
            { 
               "nested":{ 
                  "path":"originator",
                  "score_mode":"avg",
                  "query":{ 
                     "query_string":{ 
                        "query":"Handbook of Bipolar Disorder",
                        "fields":[ 
                           "originator.name",
                           "originator.role"
                        ]
                     }
                  }
               }
            },
            { 
               "nested":{ 
                  "path":"series",
                  "score_mode":"avg",
                  "query":{ 
                     "query_string":{ 
                        "query":"Handbook of Bipolar Disorder",
                        "fields":[ 
                           "series.series",
                           "series.seriesCode"
                        ]
                     }
                  }
               }
            }
         ],
         "minimum_should_match": 1,
         "filter":[ 
            { 
               "range":{ 
                  "publicationDate":{ 
                     "gte":-1574400600000
                  }
               }
            },
            { 
               "range":{ 
                  "publicationDate":{ 
                     "lte":1597775400000
                  }
               }
            }
         ]
      }
   },
   "from":0,
   "size":10,
   "sort":[],
   "aggs":{ 

   }
}```

How can I apply priority on bindingStyle so that It will reject all lower priority documents for a Book.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-09 13:33:24

如果可以为不同的装订风格添加新字段bindStyleId,例如平装本:1,精装本:2和电子书:3。使用Field collapsing可以解决问题。折叠返回每个折叠关键字(Group by) `查询的前1个排序文档

代码语言:javascript
复制
{
   "query": {
                ...... --> your query
            },
   "collapse" : {
                 "field" : "bindStyleId"-->new numeric Id for binding style to enable sort
                },
    "sort": ["bindStyleId"]
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60506423

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档