Search as you type field, best practice

Hi,
I made some tests with search as you type field: https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-as-you-type.html

I've an index containing products. Each product(document) have many fields like: lineDescription, model, colorDescription and so on.
On those fields in some part of my application I have some autocomplete that help the user during typing: for example on model field, lineDescription, and so on.

I saw how to create a field of "search_as_you_type" type, but I don't know if I should convert my original fields (they were text) into this type or if it's better to add a new field "search_as_you_type" in addition to the original field with the same data inside.

In short, should I have this:

 "lineDescription": {
                "type": "text",
                "fields": {
                  "ngram": {
                    "type": "text",
                    "analyzer": "ngram",
                    "search_analyzer": "standard"
                  },
                  "keyword": {
                    "type": "keyword"
                  }
                }
              },
			  "lineDescription_as_you_type": {
                "type": "search_as_you_type"              
              },

just this:

  "lineDescription_as_you_type": {
                    "type": "search_as_you_type"              
                  },

or

 "lineDescription": {
                    "type": "text",
                    "fields": {
                      "ngram": {
                        "type": "text",
                        "analyzer": "ngram",
                        "search_analyzer": "standard"
                      },
                      "keyword": {
                        "type": "keyword"
                      },
                      "suggest": {
                          "type": "search_as_you_type"              
                      },
                    }
                  },

I was asking myse which is the most long term solution to avoid to wake up a day and say "oh, I can't use this query on lineDescription field because it has search_as_you_type type". For example I also want to do exact matching on lineDescription field as well, and for that reason I created also .keyword variant of the field.

I'm sure you guys can suggest me a more wide vision about modeling this specific use case.

Thanks

Hmm, what I did for a similar use-case was creating a new field called: product_search_as_you_type of the type: search_as_you_type.

And then for lineDescription, i would use copy_to: with the value: product_search_as_you_type.
In this case all the lines of the product will be copied to that field and you can search all the contents of the object without worrying about not using that field later in a different kind of way.

1 Like