如何在 Azure 搜索中编写"Ends With"正则表达式



在Azure Search中,在值为"12-10-3"或"30-843-44"的字段上,我设置了一个自定义标记生成器,用空字符串替换短划线。

我现在想做一个"以结尾"的regex搜索,但无法让它完全按照我的意愿进行。

例如,为了找到以3结尾的代码,我尝试过:

searchMode=any&queryType=full&search=code:/(.*)3/

比如说,这会返回"12-10-3",但也会返回"30-843-44"。

然后我尝试了:

searchMode=any&queryType=full&search=code:/(.*)3[^<0-9>]*/

但这似乎给出了同样的结果。我一直在尝试浏览Azure搜索文档中引用的regex语法。

当我在"123-456-78"上测试我的标记器时,它似乎可以工作,所以我不明白为什么正则表达式搜索不能正常工作。

"tokens": [
{
"token": "12345678",
"startOffset": 0,
"endOffset": 10,
"position": 0
}
]

有什么想法吗?

更新:

令牌化器在C#中的应用如下:

var myIndexDefinition = new Index()
{
Name = "MyIndex",
Analyzers = new[] 
{
new CustomAnalyzer
{
Name = "code_with_dash_analyzer",
Tokenizer = TokenizerName.Keyword,
CharFilters = new CharFilterName [] { "dash_to_empty_mapper" }
}
},
CharFilters = new List<CharFilter>
{
new MappingCharFilter("dash_to_empty_mapper", new[] { "- => " })
},
Fields = new[]
{
// Field with the dash in the values
new Field("codes", DataType.String) { IsRetrievable = true, IsSearchable = true, IsSortable = true, IsFilterable = true, IsFacetable = true },
//.... other field definitions....
}
}

根据您的描述,根据我的经验,我想您的问题可能是由您的自定义标记器引起的,我不知道如何实现。

然而,在不使用自定义标记化器的情况下,您可以尝试使用的lucene regexp是:

/([0-9]+-?)+[0-9]*3/

希望能有所帮助。

最新更新