Understanding search-as-you-type Fields

Hello @safakkbilici,

Indeed you will get same result. But this can match the query terms in any order, but it will score documents higher if they contain the terms in order in a shingle subfield.

For example I am indexing below doc

Create index

PUT products
{
  "mappings": {
    "properties": {
      "description": {
        "type": "search_as_you_type"
      }
    }
  }
}

Index Docs

POST products/_doc/
{
  "description": "best jogging shoes for men"
}
POST products/_doc/
{
  "description": "I purchased best sport shoes for upcoming match"
}

Query 1

GET products/_search
{
  "query": {
    "multi_match": {
      "query": "best sport jogging",
      "type": "bool_prefix", 
      "fields": [
        "description"
        ]
    }
  }
}

Response

{
  "took": 9,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 1.201328,
    "hits": [
      {
        "_index": "products",
        "_id": "i5HdtIsBDw1I7eA1_uoH",
        "_score": 1.201328,
        "_source": {
          "description": "best jogging shoes for men"
        }
      },
      {
        "_index": "products",
        "_id": "jJHktIsBDw1I7eA1supk",
        "_score": 0.79994905,
        "_source": {
          "description": "I purchased best sport shoes for upcoming match"
        }
      }
    ]
  }
}

If you have noticed, We still getting record but best match is coming on second rank. Because in this query only term jogging is getting matched but my expectation was doc which contains best sport should come first.

Lets try another query

Query 2

GET products/_search
{
  "query": {
    "multi_match": {
      "query": "best sport jogging",
      "type": "bool_prefix", 
      "fields": [
        "description",
        "description._2gram",
        "description._3gram"
        ]
    }
  }
}

Response

{
  "took": 12,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 1.4235239,
    "hits": [
      {
        "_index": "products",
        "_id": "jJHktIsBDw1I7eA1supk",
        "_score": 1.4235239,
        "_source": {
          "description": "I purchased best sport shoes for upcoming match"
        }
      },
      {
        "_index": "products",
        "_id": "i5HdtIsBDw1I7eA1_uoH",
        "_score": 1.201328,
        "_source": {
          "description": "best jogging shoes for men"
        }
      }
    ]
  }
}

This time I am getting proper order because best sport term is matched with description._2gram field. Hence it boost the score.

1 Like