1

I have data in elasticsearch fields which contains "face mask", I am able to fetch this data with the following query with search_term set to "face", "face mask", "mask" and "face masks".

{
    "query": {
        "bool": {
            "must": [{
                "multi_match": {
                    "query": ${search_term},
                    "fields": [
                        "title^4",
                        "titleNgram^3",
                    ],
                    "fuzziness": "auto"
                }
            }]
        }
    }
}

What I am not able to achieve is when I query for "facemask" (no spaces in search_term) it does not return any document containing "face" or "mask", but, it returns documents with fields containing "facewash". Is there a way to achieve it?

Mapping is shared below

{
    "mappings": {
        "_doc": {
            "properties": {
                "title": {
                    "type": "text"
                },
                "titleNgram": {
                    "search_analyzer": "whitespace_analyzer",
                    "analyzer": "nGram_analyzer",
                    "type": "text"
                },
                "id": {
                    "index": false,
                    "type": "text"
                }
            }
        }
    }
}
yousuf iqbal
  • 408
  • 2
  • 7
  • 16

1 Answers1

1

You can try removing the "search_analyzer": "whitespace_analyzer", and reindex the data and use the same query and it should return you the expected results.

{
    "mappings": {
        "_doc": {
            "properties": {
                "title": {
                    "type": "text"
                },
                "titleNgram": {
                    "search_analyzer": "whitespace_analyzer", --> remove this line.
                    "analyzer": "nGram_analyzer",
                    "type": "text"
                },
                "id": {
                    "index": false,
                    "type": "text"
                }
            }
        }
    }
}

Edit: I tested it locally and it worked for me, below is the sample I used

Search query

{
  "query": {
    "multi_match": {
      "query": "face mask",
      "fields": [
        "titlengram",
        "title"
      ]
    }
  }
}

 

And it gives your expected doc

 "hits": [
      {
        "_index": "ngram",
        "_type": "_doc",
        "_id": "1",
        "_score": 2.3014567,
        "_source": {
          "title": "face mask",
          "titlengram": "face mask"
        }
      }
   
Amit
  • 30,756
  • 6
  • 57
  • 88