how to disable es highlight the synonym?

kongkongyzt :

I only want to highlight the words I search in the query, not including the synonym, but I also hope es can return the search result can contain the synonym search result, here is an example.

PUT /my_test_index/
{
    "settings": {
        "analysis": {
            "filter": {
                "native_synonym": {
                    "type": "synonym",
                    "ignore_case": true,
                    "expand": true,
                    "synonyms": [
                        "apple,fruit"
                    ]
                }
            },
            "analyzer": {
                "test_analyzer": {
                    "tokenizer": "whitespace",
                    "filter": [
                        "native_synonym"
                    ]
                }
            }
        }
    },
    "mappings": {
        "properties": {
            "desc": {
                "type": "text",
                "analyzer": "test_analyzer"
            }
        }
    }
}
POST /my_test_index/_doc
{
    "desc": "apple"
}

POST /my_test_index/_doc
{
    "desc": "fruit"
}
GET /my_test_index/_search
{
    "query": {
        "match": {
            "desc": "apple"
        }
    },
    "highlight": {
        "fields": {
            "desc": {}
        }
    }
}

However, es highlight both fruit and apple while I only want the apple get highlight. Anyone knows how to solve this? Thanks in advance :)

"hits": [
            {
                "_index": "my_test_index",
                "_type": "_doc",
                "_id": "RMyZrXAB7JsJEwsbVF33",
                "_score": 0.29171452,
                "_source": {
                    "desc": "apple"
                },
                "highlight": {
                    "desc": [
                        "<em>apple</em>"
                    ]
                }
            },
            {
                "_index": "my_test_index",
                "_type": "_doc",
                "_id": "RcyarXAB7JsJEwsboF2V",
                "_score": 0.29171452,
                "_source": {
                    "desc": "fruit"
                },
                "highlight": {
                    "desc": [
                        "<em>fruit</em>"
                    ]
                }
            }
        ]
Stefano Branco :

You can add a highlight query that behaves different to your actual search query. All you need then is a field indexed without the synonyms, and you should be able to get what you want:

PUT /my_test_index/
{
  "settings": {
    "analysis": {
      "filter": {
        "native_synonym": {
          "type": "synonym",
          "ignore_case": true,
          "expand": true,
          "synonyms": [
            "apple,fruit"
          ]
        }
      },
      "analyzer": {
        "test_analyzer": {
          "tokenizer": "whitespace",
          "filter": [
            "native_synonym"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "desc": {
        "type": "text",
        "analyzer": "test_analyzer",
        "fields": {
          "raw": {
            "type": "text",
            "analyzer": "whitespace"
          }
        }
      }
    }
  }
}

GET /my_test_index/_search
{
  "query": {
    "match": {
      "desc": "apple"
    }
  },
  "highlight": {
    "fields": {
      "desc.raw": {
        "highlight_query": {
          "match": {
            "desc.raw": "apple"
          }
        }
      }
    }
  }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=26956&siteId=1