簡體   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