Search_as_you_type and highlight

I am looking for working example of a search_as_you_type and highlight. Could Someone provide me with example? Thanks

1 Like
PUT test
{
  "mappings": {
    "properties": {
      "my_field": {
        "type": "search_as_you_type"
      }
    }
  }
}

PUT test/_doc/1
{
  "my_field": "quick brown fox jump lazy dog"
}

GET test/_search
{
  "query": {
    "multi_match": {
      "query": "brown fox ",
      "type": "bool_prefix",
      "fields": [
        "my_field",
        "my_field._2gram",
        "my_field._3gram"
      ]
    }
  },
  "highlight": {
    "fields": {
      "my_field": {
        "type" : "unified"
      }
    }
  }
}

That leads to the result:

    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.8630463,
        "_source" : {
          "my_field" : "quick brown fox jump lazy dog"
        },
        "highlight" : {
          "my_field" : [
            "quick <em>brown</em> <em>fox</em> jump lazy dog"
          ]
        }
      }
    ]

It will only highlight matching tokens — if you leave out the space after fox, it won't mark that. Are you expecting partial highlighting instead? Not sure this would make sense for an autocomplete / search as you type field.

Thanks. My problem is that I use a copy field. Mapping:
{
"properties": {
"abstract": {
"type": "text",
"copy_to": "freetext"
},
"name": {
"type": "text",
"copy_to": "freetext"
},
"freetext": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
},
"completion": {
"type": "completion"
},
"shingle": {
"type": "search_as_you_type"
}
}
}
}
}

and search:
{
"_source": false,
"docvalue_fields": [
{
"field": "freetext.keyword"
}
],
"size": 10,
"query": {
"multi_match": {
"query": "summ",
"type": "bool_prefix",
"fields": [
"freetext.shingle",
"freetext.shingle._2gram",
"freetext.shingle._3gram"
]
}
},
"highlight": {
"fields": {
"freetext.shingle": {
"type": "unified"
}
}
}
}

It looks that the problem is that the freetext is not part of the original document. It is a copy document. I can display it with "docvalue_fields", but is it possible to get the highligting working for the copy field?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.