1

I am new to Elasticsearch. We have certain data in different data-types that we want to index and retrieve. We are using custom "_all" fields as explained in the below link

Custom "_all" fields

Following is our code

For Creating Index

PUT myindex
{
  "mappings": {
    "mytype": {
      "properties": {
        "first_name": {
          "type":    "text",
          "copy_to": "contact_details" 
        },
        "mobile_number": {
          "type":    "long",
          "copy_to": "contact_details" 
        },
        "contact_details": {
          "type":    "text"
        }
      }
    }
  }
}

For Adding to Index

PUT myindex/mytype/1
{
  "first_name": "John",
  "mobile_number": 9988776655
}

For Search

GET myindex/_search
{
  "query": {
    "multi_match": {
      "query": "9988776655",
      "fields": [
        "contact_details"
      ],
      "fuzziness": "auto"
    }
  },
  "highlight": {
    "require_field_match": false,
    "pre_tags": [
      "<tag1>"
    ],
    "post_tags": [
      "</tag1>"
    ],
    "fields": {
      "first_name": {},
      "mobile_number": {}
    }
  }
}

Using the above query we are able to fetch results but are not able to highlight the original field value as explained in the following link

Highlighting Original Fields

Need to know if we are doing anything wrong or is there a bug

Please note that we have to use the Custom "_all" field as it is important to our requirement. Also, the datatype of the fields cannot be changed.

Thanks a lot

Chintan Shah
  • 85
  • 1
  • 1
  • 8

1 Answers1

0

I've tested your query and it appears to me to highlight the original field, in this case mobile_number with tag fields surrounding it.

Here are the results I get:

    {
  "took" : 93,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "my_so_index",
        "_type" : "mytype",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "first_name" : "John",
          "mobile_number" : 9988776655
        },
        "highlight" : {
          "mobile_number" : [
            "<tag1>9988776655</tag1>"
          ]
        }
      }
    ]
  }
}

I'm running the above query on version 6.6 of ElasticSearch. For version 5 the documentation shows a slightly different structure, can you try this:

GET myindex/_search
{
  "query": {
    "multi_match": {
      "query": "9988776655",
      "fields": [
        "contact_details"
      ],
      "fuzziness": "auto"
    }
  },
  "highlight": {
    "pre_tags": [
      "<tag1>"
    ],
    "post_tags": [
      "</tag1>"
    ],
    "fields": {
      "first_name": {"require_field_match": false},
      "mobile_number": {"require_field_match": false}
    }
  }
}
whatapalaver
  • 865
  • 13
  • 25
  • Hi @whatapalaver, May I know which version of elasticsearch you are using. I am getting following result '{ "took": 14, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 1, "hits": [ { "_index": "myindex", "_type": "mytype", "_id": "1", "_score": 1, "_source": { "first_name": "John", "mobile_number": 9988776655 } } ] } }' – Chintan Shah Mar 05 '19 at 07:24
  • I'm using elasticsearch 6.6 so that presumably accounts for the difference. The version 5 documentation shows the require_field_match: false against each highlight field. I'll edit my answer with a proposal to try. https://www.elastic.co/guide/en/elasticsearch/reference/5.0/mapping-all-field.html#all-highlight-fields – whatapalaver Mar 05 '19 at 09:58