Desire feature of searching for part of word in Elasticsearch returning nothing. Only works with complete word

前端 未结 1 812
名媛妹妹
名媛妹妹 2021-01-26 23:45

I tried two different approaches for creating index and both are returning anything if I search for part o the word. Basically, if I search for first letters or letters in the m

相关标签:
1条回答
  • 2021-01-27 00:28

    In my this SO answer, the requirement was kinda prefixed search, ie for text Demetrio1 only searching for de demet required, which worked as I created edge-ngram tokenizer to address this, but in this question, requirement is to provide the infix search for which we will use the ngram tokenizer in our custom analyzer.

    Below is the step by step example

    Index def

    {
      "settings": {
        "index.max_ngram_diff" :10,
        "analysis": {
          "filter": {
            "autocomplete_filter": {
              "type": "ngram",  --> note this
              "min_gram": 2,
              "max_gram": 8
            }
          },
          "analyzer": {
            "autocomplete": { 
              "type": "custom",
              "tokenizer": "standard",
              "filter": [
                "lowercase",
                "autocomplete_filter"
              ]
            }
          }
        }
      },
      "mappings": {
        "properties": {
          "title": {
            "type": "text",
            "analyzer": "autocomplete", 
            "search_analyzer": "standard" 
          }
        }
      }
    }
    

    Index sample doc

    {
        "title" : "Demetrio1"
    }
    

    Search query

    {
        "query" :{
            "match" :{
                "title" :"met"
            }
        }
    }
    

    search result bring the sample doc:)

     "hits": [
                {
                    "_index": "ngram",
                    "_type": "_doc",
                    "_id": "1",
                    "_score": 0.47766083,
                    "_source": {
                        "title": "Demetrio1"
                    }
                }
            ]
    
    0 讨论(0)
提交回复
热议问题