首页 > 解决方案 > 如何在弹性搜索中排除同义词过滤器中的搜索词

问题描述

当我在弹性搜索中添加表和表作为同义词过滤器时,我需要过滤掉表扇的结果。如何在弹性搜索中实现这一点

我们是否可以在设置中而不是在弹性搜索中的运行时查询中构建包含和排除列表过滤器的分类法

标签: 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 文档以了解映射用于定义什么。

请参阅有关动态模板的文档,该模板允许您定义可应用于动态添加的字段的自定义映射


推荐阅读