简体   繁体   中英

Elastic search : Match query with analyzer is not working

My goal is to search a word irrespective of the analyzer added to that.

I used match query with keyword analyzer but i think it works with the default analyzer added to that property.

In elastic search, my author document structure is like

"_source": {
               "Id": 3,
               "Organization": "let123"
            }

Index mapping :

 createIndexDescriptor.NumberOfReplicas(1)
                .NumberOfShards(1)
                .Settings(
                    settings =>
                    settings
                        .Add("analysis.filter.autocomplete_filter_ngram.type", "edge_ngram")
                        .Add("analysis.filter.autocomplete_filter_ngram.min_gram", "2")
                        .Add("analysis.filter.autocomplete_filter_ngram.max_gram", "7")
                        .Add("analysis.analyzer.title_analyzer.type", "custom")
                        .Add("analysis.analyzer.title_analyzer.char_filter.0", "html_strip")
                        .Add("analysis.analyzer.title_analyzer.tokenizer", "standard")
                        .Add("analysis.analyzer.title_analyzer.filter.0", "lowercase")
                        .Add("analysis.analyzer.title_analyzer.filter.1", "asciifolding")
                        .Add("analysis.analyzer.title_analyzer.filter.2", "autocomplete_filter_ngram"))
                .AddMapping<Author>(
                    m =>
                    m.MapFromAttributes()
                        .AllField(f => f.Enabled(true))
                        .Properties(
                            props =>
                            props.MultiField(
                                mf =>
                                mf.Name(t => t.Organization)
                                    .Fields(fs => fs.String(s => s.Name(t => t.Organization).Analyzer("title_analyzer"))
                                          ))));

here i noted one of my title analyzer filter is ngram

But I used keyword analyzer in my match query to avoid autocomplete in my searching.

GET /author/_search {
    "query": {
        "match": {
           "Organization": {
               "query": "le",
               "analyzer": "keyword"
           }
        }
    } }

But when i searched, the above document is matched. what i am expecting is Organization having exact value as ' le '

Why this is matched? Any idea to achieve my goal?

By specifiying the analyser in the query you are instructing Elasticsearch how to analyse the query you've sent.

For example:

GET /author/_search
  {
    "query": {
        "match": {
           "Organization": {
               "query": "le",
               "analyzer": "keyword"
           }
        }
     }
  }

Tells Elasticsearch to use the keyword analyser on the le string. It doesn't affect the indexed terms that have already been created on your stored data ( let123 )

The only way to change the way that stored data is analysed, is to update your mapping and re-index your data.

Multifields

It's not possible to have multiple analyzers against the same field but data can instead be easily stored in multiple fields (each having a single analyser).

for example:

{
  "tweet" : {
    "properties" : {
      "name" : {
        "type" : "string",
        "index" : "analyzed",
        "fields" : {
          "raw" : {"type" : "string", "index" : "not_analyzed"}
        }
      }
    }
  }
}

the name data is automatically stored in two places - in fields name (where it is analysed) and name.raw (where no analysis takes place). See Multi Fields .

GET /author/_search 
{
    "query": {
        "term": "le"
    }
}

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