ngrams ins elasticsearch are not working

2019-09-04 17:59发布

问题:

I use elasticsearch ngram

"analysis": {
    "filter": {
        "desc_ngram": {
            "type": "ngram",
            "min_gram": 3,
            "max_gram": 8
        }
    },
    "analyzer": {
        "index_ngram": {
            "type": "custom",
            "tokenizer": "keyword",
            "filter": [ "desc_ngram", "lowercase" ]
        },
        "search_ngram": {
            "type": "custom",
            "tokenizer": "keyword",
            "filter": "lowercase"
        }
    }
}

And I have 2 objects here

{
    "name": "Shana Calandra",
    "username": "shacalandra",
},
{
    "name": "Shana Launer",
    "username": "shalauner",
},

And using this query

{
    query: {
        match: {
            _all: "Shana"
        }
    }
}

When I search with this query, it returns me both documents, but I cant search by part of word here, for example I cant use "Shan" instead of "Shana" in query because it doesnt return anything.

Maybe my mapping is wrong, I cant understand problem is on mapping or on query

回答1:

If you specify

  "mappings": {
    "test": {
      "_all": {
        "index_analyzer": "index_ngram",
        "search_analyzer": "search_ngram"
      },

for your mapping of _all field then it will work. _all has its own analyzers and I suspect you used the analyzers just for name and username and not for _all.