Elastic search resuts for analyzer field does not match

40 views Asked by At

I have a fuzzy analyzer on addresss field with following mapping and data

PUT test_fuzzy
{
  "settings": {
    "index": {
      "max_ngram_diff": "40",
      "mapping": {
        "total_fields": {
          "limit": "2000"
        }
      },
      "number_of_shards": "3",
      "max_result_window": "15000",
      "analysis": {
        "filter": {
          "autocomplete": {
            "type": "ngram",
            "min_gram": "1",
            "max_gram": "40"
          }
        },
        "normalizer": {
          "lowercase_normalizer": {
            "filter": ["lowercase"]
          }
        },
        "analyzer": {
          "autocomplete": {
            "filter": ["lowercase", "autocomplete"],
            "tokenizer": "whitespace"
          },
          "autocomplete_search": {
            "filter": ["lowercase"],
            "tokenizer": "whitespace"
          }
        }
      },
      "number_of_replicas": "0"
    }
  },
  "mappings": {
    "dynamic": "true",
    "dynamic_templates": [
      {
        "named_analyzers": {
          "match": "address",
          "match_mapping_type": "string",
          "mapping": {
            "analyzer": "autocomplete",
            "fields": {
              "keyword": {
                "ignore_above": 256,
                "type": "keyword"
              }
            },
            "search_analyzer": "autocomplete_search",
            "type": "text"
          }
        }
      }
    ],
    "properties": {
      "address": {
        "type": "text",
        "analyzer": "autocomplete",
        "search_analyzer": "autocomplete_search",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "identifier": {
        "type": "text"
      }
    }
  }
}

Sample data to be ingested

 POST test_fuzzy/_bulk
    { "index": { "_id": "1" } }
    { "address": "1136 N Madison Ave", "identifier": 1 }
    { "index": { "_id": "2" } }
    { "address": "7135 Madison Ave W", "identifier": 2 }
    { "index": { "_id": "3" } }
    { "address": "1333 Madison Ave", "identifier": 3 }
    { "index": { "_id": "4" } }
    { "address": "1303 Madison Ave", "identifier": 4 }
    { "index": { "_id": "5" } }
    { "address": "6138 Madison Ave", "identifier": 5 }
    { "index": { "_id": "6" } }
    { "address": "1373 E Madison Ave", "identifier": 6 }
    { "index": { "_id": "7" } }
    { "address": "1333 E Madison Ave Ste 200", "identifier": 7 }
    { "index": { "_id": "8" } }
    { "address": "1311 Madison Ave", "identifier": 8 }
    { "index": { "_id": "9" } }
    { "address": "132 Madison Ave", "identifier": null }
    { "index": { "_id": "10" } }
    { "address": "1213 Madison Ave", "identifier": null }
    { "index": { "_id": "11" } }
    { "address": "413 Madison Ave", "identifier": null }
    { "index": { "_id": "12" } }
    { "address": "134 W Madison Ave", "identifier": null }
    { "index": { "_id": "13" } }
    { "address": "5138 Madison Ave", "identifier": null }
    { "index": { "_id": "14" } }
    { "address": "513 Madison Ave", "identifier": null }
    { "index": { "_id": "15" } }
    { "address": "1330 Madison Ave", "identifier": null }
    { "index": { "_id": "16" } }
    { "address": "13 Madison Ave", "identifier": null }
    { "index": { "_id": "17" } }
    { "address": "130 Madison Ave", "identifier": null }
    { "index": { "_id": "18" } }
    { "address": "131 W Madison Ave", "identifier": null }

When we search on 13 Madison Ave we wanted the document matching to it to appear first vs document which are least matching showing below that

Currently 6138 Madison Ave shows up as the first document in the ouput instead of 13 Madison Ave

Query used for Search

GET test_fuzzy/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                "bool": {
                  "should": [
                    {
                      "match": {
                        "address": {
                          "query": "13 Madison Ave",
                          "operator": "OR",
                          "prefix_length": 0,
                          "max_expansions": 50,
                          "fuzzy_transpositions": true,
                          "lenient": false,
                          "zero_terms_query": "NONE",
                          "auto_generate_synonyms_phrase_query": true,
                          "boost": 1
                        }
                      }
                    }
                  ],
                  "adjust_pure_negative": true,
                  "boost": 1
                }
              }
            ],
            "adjust_pure_negative": true,
            "boost": 1
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}
0

There are 0 answers