0

I have an index which is 2-4 characters with no spaces but user often searches for the "full term" which I dont have indexed but has 3 extra characters after a blank space. Ex: I index "A1" or "A1B" or "A1B2" and the "full term" is something like "A1 11A" or "A1B ABA" or "A1B2 2C8".

This is current mapping:

"code": {
    "type": "text"
},

If he searches "A1" it bring all of them which is also correct, if he types "A1B" I want to bring only the last two and if he searches "A1B2 2C8" I want to bring only the last one.

Is that possible? If so, what would be the best search/index strategy?

Murilo
  • 580
  • 5
  • 21

1 Answers1

1

Index Mapping:

{
  "settings": {
    "analysis": {
      "filter": {
        "autocomplete_filter": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 10
        }
      },
      "analyzer": {
        "autocomplete": { 
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "autocomplete_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "code": {
        "type": "text",
        "analyzer": "autocomplete", 
        "search_analyzer": "standard" 
      }
    }
  }
}

Index data:

{
  "code": "A1"
}
{
  "code": "A1B"
}
{
  "code": "A1B2"
}

Search Query:

{
    "query": {
        "match": {
            "code": {
                "query": "A1B2 2C8"
            }
        }
    }
}

Search Result:

 "hits": [
      {
        "_index": "65067196",
        "_type": "_doc",
        "_id": "3",
        "_score": 1.3486402,
        "_source": {
          "code": "A1B2"
        }
      }
    ]
ESCoder
  • 15,431
  • 2
  • 19
  • 42
  • How would you do to also make it possible to find results for the query terms if a space is not present e.g. "A1B22C8", "A111A", "A1BABA". Is there an easy tweak on the answer? Or would it be a lot different? If its just a tweak I can edit the question – Murilo Dec 04 '20 at 06:49