ElasticSearch搜索得到不好的结果

本文关键字:结果 搜索 ElasticSearch | 更新日期: 2023-09-27 18:08:26

我对ElasticSearch相当陌生,并且在获得我认为很好的搜索结果方面存在问题。我的目标是能够根据用户输入的短语搜索药物索引(6个字段)。可以再多说几个字。我已经尝试了几种方法,但我将在下面概述我迄今为止发现的最好的方法。告诉我哪里做错了。我想我错过了一些基本的东西。

下面是我正在处理的字段的一个子集

...
    "hits": [
         {
            "_index": "indexus2",
            "_type": "Medication",
            "_id": "17471",
            "_score": 8.829264,
            "_source": {
               "SearchContents": " chew chewable oral po tylenol",
               "MedShortDesc": "Tylenol PO Chew",
               "MedLongDesc": "Tylenol Oral Chewable"
               "GenericDesc": "ACETAMINOPHEN ORAL"
               ...
            }
         }
         ...

我正在搜索的字段使用Edge NGram分析器。我正在使用c# Nest库来索引

 settings.Analysis.Tokenizers.Add("edgeNGram", new EdgeNGramTokenizer()
            {
                MaxGram = 50,
                MinGram = 2,
                TokenChars = new List<string>() { "letter", "digit" }
            });
    settings.Analysis.Analyzers.Add("edgeNGramAnalyzer", new CustomAnalyzer()
            {
                Filter = new string[] { "lowercase" },
                Tokenizer = "edgeNGram"
            });

我对有问题的字段使用more_like_this查询

GET indexus2/Medication/_search
{
  "query": {
    "more_like_this" : {
        "fields" : ["MedShortDesc", 
                    "MedLongDesc", 
                    "GenericDesc",
                    "SearchContents"],
        "like_text" : "vicodin",
        "min_term_freq" : 1,
        "max_query_terms" : 25,
        "min_word_len": 2
    }
  }
}

问题是,对于这个搜索'vicodin',我希望首先看到与完整工作的匹配,但我没有。下面是该查询结果的一个子集。维柯丁直到第七次结果才出现

"hits": [
         {
            "_index": "indexus2",
            "_type": "Medication",
            "_id": "31192",
            "_score": 4.567309,
            "_source": {
               "SearchContents": " oral po victrelis",
               "MedShortDesc": "Victrelis PO",
               "MedLongDesc": "Victrelis Oral",
               "RepresentativeRoutedGenericDesc": "BOCEPREVIR ORAL",
               ...
            }
         }
         <5 more similar results>
         {
            "_index": "indexus2",
            "_type": "Medication",
            "_id": "26198",
            "_score": 2.2836545,
            "_source": {
               "SearchContents": " (original 5 500 feeding mg strength) tube via vicodin",
               "MedShortDesc": "Vicodin 5 mg-500 mg (Original Strength) via feeding tube",
               "MedLongDesc": "Vicodin 5 mg-500 mg (Original Strength) via feeding tube",
               "GenericDesc": "HYDROCODONE BITARTRATE/ACETAMINOPHEN ORAL",
             ...
            }
          }

字段映射
"OrderableMedLongDesc": {
      "type": "string",
      "analyzer": "edgeNGramAnalyzer"
},
"OrderableMedShortDesc": {
       "type": "string",
       "analyzer": "edgeNGramAnalyzer"
},
"RepresentativeRoutedGenericDesc": {
       "type": "string",
       "analyzer": "edgeNGramAnalyzer"
},
"SearchContents": {
       "type": "string",
        "analyzer": "edgeNGramAnalyzer"
},

下面是ES显示的我的_settings for analyzers

          "analyzer": {
             "edgeNGramAnalyzer": {
                 "type": "custom",
                 "filter": [
                    "lowercase"
                 ],
                 "tokenizer": "edgeNGram"
              }
           },
           "tokenizer": {
              "edgeNGram": {
                 "min_gram": "2",
                 "type": "edgeNGram",
                 "max_gram": "50"
              }
           }

ElasticSearch搜索得到不好的结果

根据上面的映射,edgeNGramAnalyzer是字段的搜索分析器,因此搜索查询也将获得"边缘编程"。你可能不希望这样。

修改映射,只将index_analyzer选项设置为edgeNgramAnalyzer

search_analyzer将默认为standard

的例子:

"SearchContents": {
       "type": "string",
        "index_analyzer": "edgeNGramAnalyzer"
},