How to reference search_as_you_type fields when creating a query for Opensearch

31 Views Asked by At

I have a requirement from a website to start typing into a search box letter by letter and filter results coming back quickly. For example, customer name. We might have John and James. The client presses J and records for both come back, then they press a (so Ja) and now it filters only to the ‘James’ records.

My understanding is with a lot of data, this needs to be quite performant, so we have indexed the fields that need this kind of search as you type ability as search_as_you_type fields in addition to text (as they can be matched exactly, as well).

"customerName": {
  "type": "text",
  "fields": {
    "keyword": {
      "type": "keyword"
    },
    "suggestion": {
      "type": "search_as_you_type",
      "doc_values": false,
      "max_shingle_size": 3
    }
  }
},

When querying these fields, we are just specifying the field as customerNumber.suggestion (for example):

      },
      {
        "multi_match": {
          "fields": [
            "customerNumber.suggestion",
            "orderReference.suggestion"
          ],
          "query": "string",
          "type": "phrase_prefix"
        }
      }
    ]

But in OpenSearch documentation like Search as you type - OpenSearch Documentation the examples show queries with these fields like _2gram, or _3gram? Do we need to actually specifiy these when searching?

GET books/_search
{
“query”: {
“multi_match”: {
“query”: “tw one”,
“type”: “bool_prefix”,
“fields”: [
“suggestions”,
“suggestions._2gram”,
“suggestions._3gram”
]
}
}
}

We have tried both ways and it doesn’t seem to make any difference. So do we need to specify these _3gram field ‘names’? Or no?

0

There are 0 best solutions below