Dennis
Dennis

Reputation: 693

ElasticSearch search getting bad results

I am fairly new to ElasticSearch and am having issues getting search results that I perceive to be good. My objective is to be able to search an index of medications (6 fields) against a phrase that the user enters. It could be one ore more words. I've tried a few approaches, but I'll outline the best one I've found so far below. Let me know what I'm doing wrong. I'm guessing that I'm missing something fundamental.

Here is a subset of the fields that I'm working with

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

The fields that I'm searching against used an Edge NGram Analyzer. I'm using the C# Nest library for the indexing

 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"
            });

I am using a more_like_this query against the fields in question

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

The problem is that for this search for 'vicodin', I'd expect to see matches with the full work first, but I don't. Here is a subset of the results from this query. Vicodin doesn't show up until the 7th result

"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",
             ...
            }
          }

Field Mappings

"OrderableMedLongDesc": {
      "type": "string",
      "analyzer": "edgeNGramAnalyzer"
},
"OrderableMedShortDesc": {
       "type": "string",
       "analyzer": "edgeNGramAnalyzer"
},
"RepresentativeRoutedGenericDesc": {
       "type": "string",
       "analyzer": "edgeNGramAnalyzer"
},
"SearchContents": {
       "type": "string",
        "analyzer": "edgeNGramAnalyzer"
},

Here is what ES shows for my _settings for analyzers

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

Upvotes: 0

Views: 523

Answers (1)

keety
keety

Reputation: 17461

As per the above mapping edgeNGramAnalyzer is the search-analyzer for the fields as a result the search query would also get "edge ngrammed". You probably do not want this .

Change the mapping to set only the index_analyzer option as edgeNgramAnalyzer.

The search_analyzer would then default to standard.

Example:

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

Upvotes: 1

Related Questions