首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在elasticsearch中排除同义词过滤器中的搜索词

如何在elasticsearch中排除同义词过滤器中的搜索词
EN

Stack Overflow用户
提问于 2020-09-23 14:12:26
回答 2查看 241关注 0票数 0

当我在elastic search中添加table和tables作为同义词筛选器时,我需要筛选出表扇的结果。如何在弹性搜索中实现这一点

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

EN

回答 2

Stack Overflow用户

发布于 2020-09-23 15:03:18

添加一个包含索引数据、映射、搜索查询和搜索结果的工作示例

索引映射:

代码语言:javascript
复制
{
  "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"
      }
    }
  }
}

分析应用编程接口

代码语言:javascript
复制
POST/_analyze

{
  "analyzer" : "synonym_analyzer",
  "text" : "table fan"
}

将生成以下令牌:

代码语言:javascript
复制
{
  "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
    }
  ]
}

索引数据:

代码语言:javascript
复制
{ "title": "table and fan" }
{ "title": "tables and fan" }
{ "title": "table fan" }
{ "title": "tables fan" }
{ "title": "table chair" }

搜索查询:

代码语言:javascript
复制
{
    "query": {
        "bool": {
            "must": {
                "match": {
                    "title": "table"
                }
            },
            "filter": {
                "bool": {
                    "must_not": [
                        {
                            "match_phrase": {
                                "title": "table fan"
                            }
                        },
                        {
                            "match_phrase": {
                                "title": "table and fan"
                            }
                        }
                    ]
                }
            }
        }
    }
}

也可以使用match query代替match_phrase查询

代码语言:javascript
复制
{
    "query": {
        "bool": {
            "must": {
                "match": {
                    "title": "table"
                }
            },
            "filter": {
                "bool": {
                    "must_not": [
                        {
                            "match": {
                                "title": {
                                    "query": "table fan", 
                                    "operator": "AND"
                                }
                            }
                        }
                    ]
                }
            }
        }
    }
}

搜索结果:

代码语言:javascript
复制
"hits": [
      {
        "_index": "synonym",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.06783115,
        "_source": {
          "title": "table chair"
        }
      }
    ]

更新1:

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

映射是定义如何存储文档及其包含的字段的过程,并对此ES documentation on mapping执行indexed.Refer操作,以了解使用什么映射来定义。

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

票数 0
EN

Stack Overflow用户

发布于 2020-09-23 15:07:13

代码语言:javascript
复制
GET <indexName>/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "<fieldName>": {
              "query": "table fan", // <======= Below operator will applied b/w table(&synonyms) And fan(&synonyms)
              "operator": "AND"
            }
          }
        }
      ]
    }
  }
}

您可以使用上面的查询来排除所有同时具有'table‘、'fan’及其对应同义词的文档。

OR:

如果你想玩多个逻辑运算符。例如,给我所有不包含"table fan“或"ac”的文档,您可以使用simple_query_string

代码语言:javascript
复制
GET <indexName>/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "simple_query_string": {
            "query": "(table + fan) | ac", // <=== '+'='and', '|'='or', '-'='not' 
            "fields": [
              "<fieldName>" // <==== use multiple field names, wildcard also supported
            ]
          }
        }
      ]
    }
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64022009

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档