繁体   English   中英

弹性:在搜索期间将符号和 html 编码符号视为相同

[英]Elastic: Treat symbol and html encoded symbol the same during search

我的目标是在按符号或 html 编码版本搜索时返回相同的结果。

示例查询:

# searching with symbol
GET my-test-index/_search
{
  "query": {
    "bool": {
      "must": {
        "simple_query_string": {
          "query": "Hello®",
          "analyzer": "english_syn",
          "fields": [
            "AllContent"
          ]
        }
      }
    }
  }
}

# html symbol
GET my-test-index/_search
{
  "query": {
    "bool": {
      "must": {
        "simple_query_string": {
          "query": "Hello®",
          "analyzer": "english_syn",
          "fields": [
            "AllContent"
          ]
        }
      }
    }
  }
}

我尝试了几种不同的方法。

添加同义词但它们仍然产生不同的结果。

#######################################
# Synonyms
# Symbols
#######################################
™, ™
®, ®

创建了一个 char_filter 来替换特殊字符,这样他们至少会搜索“Hello”。 但这带来了自己的一系列问题,这些问题超出了我想要实现的 scope。

char_filter": {
    "specialCharactersFilter": {
    "type": "pattern_replace",
    "pattern": "[^A-Za-z0-9]",
    "replacement": " "
}

我感谢任何对实现此目标的新替代方案的反馈。 理想情况下,解决方案不仅涵盖 ® 和 ™。

您正在寻找的是html strip char filter ,它不仅适用于两个符号,而且适用于广泛的 html 个字符。

工作示例

使用 html strip char 过滤器进行索引映射

{
    "settings": {
        "analysis": {
            "analyzer": {
                "my_analyzer": {
                    "tokenizer": "standard",
                    "char_filter": [
                        "html_strip"
                    ]
                }
            }
        }
    },
    "mappings": {
        "properties": {
            "title": {
                "type": "text",
                "analyzer": "my_analyzer"
            }
        }
    }
}

在该文档中仅使用 (™) 索引示例文档。

PUT 71622637/_doc/1

{
   "title" : "™"
}

搜索其 html 编码版本

{
    "query" :{
        "match" : {
            "title" : "&trade"
        }
    }
}

And search result

"hits": [
            {
                "_index": "71622637",
                "_id": "1",
                "_score": 0.89701396,
                "_source": {
                    "title": "™"
                }
            }
        ]

与此类似,搜索商标符号

{
    "query" :{
        "match" : {
            "title" : "™"
        }
    }
}

And search result

"hits": [
            {
                "_index": "71622637",
                "_id": "1",
                "_score": 0.89701396,
                "_source": {
                    "title": "™"
                }
            }
        ]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM