如何在Elasticsearch中基于索引字段值(数据)的优先级获取搜索结果

问题描述 投票:2回答:1

假设我正在搜索关键字“数据结构”,我们有3本不同的书,其中包括关键字“数据结构”,它们具有3种不同的装订风格(例如平装本,精装本和电子书),每种装订风格的ISBN都不同。如果我分别为每本书编索引,那么我总共获得了9(3 * 3)个索引文件,所有文件都会出现在搜索结果中。假设我的装订风格优先:1-平装本,2-精装本,3-电子书。因此,根据装订样式的优先级,应该只有三个结果(第一册的平装本,第二册的平装本,第三册的平装本)。如果没有第二册的平装本,那么结果应该是(第一册的平装本,第二册的精装本,第三册的平装本。)

我的索引文件是:

        "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"
            }
        ]

我写的搜索查询是:

   "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.
elasticsearch search indexing
1个回答
0
投票
如果可以为不同的绑定样式添加新字段bindStyleId例如:平装本:1,精装本:2和电子书:3可以使用Field collapsing解决问题折叠将每个折叠键返回前1个排序文档(分组依据)`查询
© www.soinside.com 2019 - 2024. All rights reserved.