如何禁用es高亮同义词?

how to disable es highlight the synonym?

我只想高亮我在查询中搜索的词,不包括同义词,但我也希望es可以return搜索结果可以包含同义词搜索结果,这里是一个例子。

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": {}
        }
    }
}

但是,es 突出显示 fruitapple 而我只希望 apple 突出显示。 任何人都知道如何解决这个问题?提前致谢:)

"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>"
                    ]
                }
            }
        ]

您可以添加与实际搜索查询不同的突出显示查询。那么你所需要的只是一个没有同义词索引的字段,你应该能够得到你想要的:

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"
          }
        }
      }
    }
  }
}