-->

如何与指数字母数字字符的字段和通配符搜索破折号(How to index a field with

2019-10-30 01:21发布

给定一个模型,如下所示:

{
    [Key]
    public string Id { get; set; }

    [IsSearchable]
    [Analyzer(AnalyzerName.AsString.Keyword)]
    public string AccountId { get; set; }
}

对于ACCOUNTID会像这样和样本数据:

1-ABC123
1-333444555
1-A4KK498

本场可有字母/数字的组合,并在中间有一个破折号。

我需要能够在使用查询像1-ABC *此搜索。 然而,没有基本的分析似乎支持除关键字,这是不拾取任何通配符查询,只有完全匹配的冲刺。 我已经看到了有关自定义分析其他一些文章,但我不能得到关于如何构建它来解决这个问题的足够信息。

我需要知道,如果我必须建立一个客户分析这个领域,我需要一个不同的搜索分析和索引分析?

我使用StandardLucene其他字母数字字段没有破折号和我有破折号另一个领域,但它的所有的数字和关键字工作得很好那里。 看来这个问题是字母和数字的组合。

Answer 1:

自仪确实是去这里的路。 基本上你可以定义使用“关键词”标记者以“小写”标记过滤器自定义分析。

自定义分析仪添加到您的Index类,并在模型相匹配的自定义分析名称更改分析仪名称:

new Index()
{
    ...
    Analyzers = new[]
    {
        new CustomAnalyzer()
        {
            Name = "keyword_lowercase",
            Tokenizer = TokenizerName.Keyword,
            TokenFilters = new[] { TokenFilterName.Lowercase }
        }
    }
}

模型:

{
    [Key]
    public string Id { get; set; }

    [IsSearchable]
    [Analyzer("keyword_lowercase")]
    public string AccountId { get; set; }
}

在REST API,这将是这个样子:

{
    "fields": [{
        "name": "Id",
        "type": "Edm.String",
        "key": true
    },
    {
        "name": "AccountId",
        "type": "Edm.String",
        "searchable": true,
        "retrievable": true,
        "analyzer": "keyword_lowercase"
     }],
    "analyzers":[
        {
           "name":"keyword_lowercase",
           "@odata.type":"#Microsoft.Azure.Search.CustomAnalyzer",
           "tokenizer":"keyword_v2",
           "tokenFilters":["lowercase"]
        }
     ]
}


文章来源: How to index a field with alphanumeric characters AND a dash for wildcard search