Elastic search : Match query with analyzer is not

2019-09-17 10:01发布

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?

2条回答
叛逆
2楼-- · 2019-09-17 10:52

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.

查看更多
小情绪 Triste *
3楼-- · 2019-09-17 10:52
GET /author/_search 
{
    "query": {
        "term": "le"
    }
}
查看更多
登录 后发表回答