简体   繁体   中英

Elasticsearch - match not_analyzed field with partial search term

I have a "name" field - not_analyzed in my elasticsearch index.

Lets say value of "name" field is "some name" . My question is, if I want a match for the search term - some name some_more_name someother name because it contains some name in it, then will not_analyzed allow that match to happen, if not, then how can I get a match for the proposed search term?

During the indexing the text of name field is stored in inverted index. If this field was analyzed, 2 terms would go to the inverted index: some and name . But as it is not analyzed, only 1 term is stored: some name During the search (using match query), by default your search query is analyzed and tokenized. So there will be several terms: some , name , some_more_name and someother . Then Elasticsearch will look at inverted index to see if there is at least one term from the search query. But there is only some name term, so you won't see this document in the result set.
You can play with analyzers using _analyze endpoint

Returning to your question, if you want to get a match for the proposed search query, your field must be analyzed.

If you need to keep non-analyzed version as well you should use multi fields :

PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "name": {
          "type": "keyword",
          "fields": {
            "analyzed": { 
              "type": "text"
            }
          }
        }
      }
    }
  }
}

Taras has explained clearly,and i issue might have resolved,but still if you cant change mapping of your index ,you can use query(I have tested in 5.4 ES)

GET test/_search
{
  "query": {
    "query_string": {
      "default_field": "namekey",
      "query": "*some* *name*",
      "default_operator": "OR"
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM