java - 如何禁用 es 突出显示同义词?

标签 java elasticsearch elk

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

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

关于java - 如何禁用 es 突出显示同义词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60556658/

相关文章:

java - Jenkins 构建失败

java - OCPJP 8 什么是使用 NIO2 的符号链接(symbolic link)

java - 如何将一张 Canvas 的内容复制到另一张 Canvas 上?

c# - ElasticSearch巢状标志C#

elasticsearch - 在移动设备(android/iphone/ios)上运行 Elasticsearch 服务器

Kibana 不显示大文本

elasticsearch - Elasticsearch + Logstash-接收事件时索引来自不同来源的数据

java - 如何从图像中获取 EXIF 或元数据?

performance - 有酒店空房情况数据的Elasticsearch中的聚合性能问题

elasticsearch - 使用 quarkuslogging-gelf 扩展和 ELK 堆栈时出现索引错误