Elasticsearch: Find substring match

后端 未结 2 1221
一生所求
一生所求 2020-12-07 16:01

I want to perform both exact word match and partial word/substring match. For example if I search for \"men\'s shaver\" then I should be able to find \"men\'s shaver\" in th

相关标签:
2条回答
  • 2020-12-07 16:17

    To search for partial field matches and exact matches, it will work better if you define the fields as "not analyzed" or as keywords (rather than text), then use a wildcard query.

    See also this.

    To use a wildcard query, append * on both ends of the string you are searching for:

    POST /my_index/my_type/_search
    {
    "query": {
        "wildcard": {
           "name": {
              "value": "*en's*"
           }
        }
    }
    }
    

    To use with case insensitivity, use a custom analyzer with a lowercase filter and keyword tokenizer.

    Custom Analyzer:

    "custom_analyzer": {
                "tokenizer": "keyword",
                "filter": ["lowercase"]
            }
    

    Make the search string lowercase

    If you get search string as AsD: change it to *asd*

    0 讨论(0)
  • 2020-12-07 16:22

    By searching with any string or substring Use:

    query: {
        or: [{
          match_phrase_prefix: {
                name: str
         }
        }, {
            match_phrase_prefix: {
                surname: str
            }
        }]
    }
    

    Happy coding with Elastic Search....

    0 讨论(0)
提交回复
热议问题