How to handle wildcards in elastic search structured queries

ε祈祈猫儿з 提交于 2019-12-02 15:28:41

问题


My use case requires to query for our elastic search domain with trailing wildcards. I wanted to get your opinion on the best practices of handling such wildcards in the queries.

Do you think adding the following clauses is a good practice for the queries:

"query" : { 
    "query_string" : { 
        "query" :   "attribute:postfix*",
        "analyze_wildcard" : true,
        "allow_leading_wildcard" : false,
        "use_dis_max" : false
    } 
}

I've disallowed leading wildcards since it is a heavy operation. However I wanted to how good is analyzing wildcard for every query request in the long run. My understanding is, analyze wildcard would have no impact if the query doesn't actually have any wildcards. Is that correct?


回答1:


If you have the possibility of changing your mapping type and index settings, the right way to go is to create a custom analyzer with an edge-n-gram token filter that would index all prefixes of the attribute field.

curl -XPUT http://localhost:9200/your_index -d '{
    "settings": {
        "analysis": {
            "filter": {
                "edge_filter": {
                    "type": "edgeNGram",
                    "min_gram": 1,
                    "max_gram": 15
                }
            },
            "analyzer": {
                "attr_analyzer": {
                    "type": "custom",
                    "tokenizer": "standard",
                    "filter": ["lowercase", "edge_filter"]
                }
            }
        }
    },
    "mappings": {
        "your_type": {
            "properties": {
                "attribute": {
                    "type": "string",
                    "analyzer": "attr_analyzer",
                    "search_analyzer": "standard"
                }
            }
        }
    }
}'

Then, when you index a document, the attribute field value (e.g.) postfixing will be indexed as the following tokens: p, po, pos, post, postf, postfi, postfix, postfixi, postfixin, postfixing.

Finally, you can then easily query the attribute field for the postfix value using a simple match query like this. No need to use an under-performing wildcard in a query string query.

{
  "query": {
     "match" : {
        "attribute" : "postfix"
     }
  }
}


来源:https://stackoverflow.com/questions/36706903/how-to-handle-wildcards-in-elastic-search-structured-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!