elasticsearch - 如何在弹性搜索中排除同义词过滤器中的搜索词
问题描述
当我在弹性搜索中添加表和表作为同义词过滤器时,我需要过滤掉表扇的结果。如何在弹性搜索中实现这一点
我们是否可以在设置中而不是在弹性搜索中的运行时查询中构建包含和排除列表过滤器的分类法
解决方案
添加带有索引数据、映射、搜索查询和搜索结果的工作示例
索引映射:
{
"settings": {
"index": {
"analysis": {
"filter": {
"synonym_filter": {
"type": "synonym",
"synonyms": [
"table, tables"
]
}
},
"analyzer": {
"synonym_analyzer": {
"tokenizer": "standard",
"filter": [
"lowercase",
"synonym_filter"
]
}
}
}
}
},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "synonym_analyzer",
"search_analyzer": "standard"
}
}
}
}
分析 API
POST/_analyze
{
"analyzer" : "synonym_analyzer",
"text" : "table fan"
}
生成以下令牌:
{
"tokens": [
{
"token": "table",
"start_offset": 0,
"end_offset": 5,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "tables",
"start_offset": 0,
"end_offset": 5,
"type": "SYNONYM",
"position": 0
},
{
"token": "fan",
"start_offset": 6,
"end_offset": 9,
"type": "<ALPHANUM>",
"position": 1
}
]
}
指数数据:
{ "title": "table and fan" }
{ "title": "tables and fan" }
{ "title": "table fan" }
{ "title": "tables fan" }
{ "title": "table chair" }
搜索查询:
{
"query": {
"bool": {
"must": {
"match": {
"title": "table"
}
},
"filter": {
"bool": {
"must_not": [
{
"match_phrase": {
"title": "table fan"
}
},
{
"match_phrase": {
"title": "table and fan"
}
}
]
}
}
}
}
}
您还可以使用匹配查询代替match_phrase查询
{
"query": {
"bool": {
"must": {
"match": {
"title": "table"
}
},
"filter": {
"bool": {
"must_not": [
{
"match": {
"title": {
"query": "table fan",
"operator": "AND"
}
}
}
]
}
}
}
}
}
搜索结果:
"hits": [
{
"_index": "synonym",
"_type": "_doc",
"_id": "2",
"_score": 0.06783115,
"_source": {
"title": "table chair"
}
}
]
更新1:
我们是否可以在设置中而不是在弹性搜索中的运行时查询中构建包含和排除列表过滤器的分类法
映射是定义文档及其包含的字段如何存储和索引的过程。请参阅有关映射的 ES 文档以了解映射用于定义什么。
请参阅有关动态模板的文档,该模板允许您定义可应用于动态添加的字段的自定义映射
推荐阅读
- python - Python 2 和 3 之间 threading.Event().wait 的大时间差
- asp.net-mvc - 使用 web.config 排除 MVC 角色提供程序的目录
- python - 在 python 3.x 中更改图像文件的 md5 签名
- javascript - 改变视口/屏幕宽度js
- javascript - 如何制作一个将背景变成渐变的按钮?
- javascript - 用 promises 和 undefined var 理解奇怪的错误
- python - 在 matplotlib 图的中间水平居中 y 标签
- javascript - 如何跨突变将新附加值添加到状态?
- python-3.x - 从python中的列表中删除子列表
- python - 用于在 Selenium 中读取与切换开关相关的文本标签的 Python 代码