忽略连字符的Elasticsearch分析器

本文关键字:Elasticsearch 分析器 连字符 | 更新日期: 2023-09-27 17:50:18

我们正在实现Elasticsearch并在。net解决方案中使用.Nest。我们已经创建并加载了一个包含多个字段的索引。我们希望定义一个分析器,当用户输入UNV-1234UNV1234时,它将为itemNumber字段产生相同的搜索结果。itemNumber字段仅限于项目编号,没有其他单词。但是,项目编号字段可以有一个用空格分隔的数字。

根据我的搜索,似乎关键字分析器将提供所需的结果。这对我们不起作用。

谁能提供信息,如何最好地实现这一点?

"itemNumber": {
    "type": "string",
    "index": "not_analyzed",
    "fields": {
        "_english": {
        "type": "string",
        "analyzer": "english"
        },
        "_keyword": {
        "type": "string",
        "analyzer": "keyword"
        },
        "_standard": {
        "type": "string",
        "analyzer": "standard"
        }
    }
}

忽略连字符的Elasticsearch分析器

您需要定义一个带有"keyword"标记器的自定义分析器和一个模式替换标记过滤器,以删除任何特殊字符并将其用于您的字段。分析器可以定义如下

    "analysis" : { 
        "filter" : { 
            "cleanspecial": { 
                "type": "pattern_replace", 
                "pattern": "[^a-zA-Z0-9]", 
                "replacement": "" 
            } 
        }, 
        "analyzer" : { 
            "cleanspecialanalyzer": { 
                "filter": ["cleanspecial"], 
                "type": "custom", 
                "tokenizer": "keyword" 
             }
         }
    }

注意:请确认图案,我没有测试过。

你可以像下面这样修改映射

"itemNumber": {
    "type": "string",
    "index": "not_analyzed",
    "fields": {
        "_english": {
        "type": "string",
        "analyzer": "english"
        },
        "_keyword": {
        "type": "string",
        "analyzer": "cleanspecialanalyzer"
        },
        "_standard": {
        "type": "string",
        "analyzer": "standard"
        }
    }
}

可以在字段itemNumber._keyword上进行搜索