如何在azure搜索中使用sql like子句?

时间:2016-06-07 02:19:01

标签: azure azure-search

我正在使用azure搜索,我想使用sql的子句

我在azure中使用的查询是

https://Serivename.search.windows.net/indexes/catalogsearch/docs?
api-version=2015-02-28&search =Mc*&queryType=simple&searchMode=all

但它不起作用

我尝试过:

How Can i use Azure Search Like Syntax

https://azure.microsoft.com/en-us/blog/lucene-query-language-in-azure-search

1 个答案:

答案 0 :(得分:0)

关于使用通配符*来执行类似%样式查询的评论中有一些很好的答案,但有时您可能也希望执行%Like查询。为此,您需要创建custom analyzer

你需要这样做的原因是因为默认索引的工作方式是索引单词的开头。例如,对于单词Microsoft,索引将通过M,然后Mi,然后Mic等完成...

这样可以轻松搜索Mic%,但它不适用于%soft。所以为了让它工作,我们需要使用一些技巧,我们告诉分析器反转单词然后索引索引(你可以在下面的分析器“suffixAnalyzer”中看到这个例子)。对于微软的例子,它会对t,tf,tfo,tfos等进行索引...当然这不会起作用,因为我们希望能够搜索%soft,但是索引反转了这些词(即tofs而不是软。所以我们所做的就是让分析仪尊重它(你可以在下面的分析器“revereseText”中看到这个例子)。

现在我们需要的是一个新字段,它使用“suffixAnalyzer”进行索引,使用“revereseText”进行searchAnalyzer(你可以在下面的字段“suffixName”中看到这个例子)。

"fields": [
    { "name":"suffixName", "type":"Edm.String", "searchable":true, "searchAnalyzer":"reverseText", "indexAnalyzer":"suffixAnalyzer" }
]

"analyzers": [
    {"name":"suffixAnalyzer","@odata.type":"#Microsoft.Azure.Search.CustomAnalyzer","tokenizer":"standard","tokenFilters":[ "lowercase", "asciifolding", "reverse", "my_edgeNGram" ]},
    {"name":"reverseText","@odata.type":"#Microsoft.Azure.Search.CustomAnalyzer","tokenizer":"standard","tokenFilters":[ "lowercase", "reverse" ]}
]

完成所有这些后,您可以在suffixName字段上搜索“soft”,它会以非常快的查询速率找到它。

顺便说一句,您实际上可以使用以下分析器创建一个前缀分析器,这样您实际上不必使用通配符。

"analyzers": [
    {"name":"prefixAnalyzer","@odata.type":"#Microsoft.Azure.Search.CustomAnalyzer","tokenizer":"standard","tokenFilters":[ "lowercase", "my_edgeNGram" ]}
]

"fields": [
    { "name":"partialName", "type":"Edm.String", "searchable":true, "searchAnalyzer":"standard", "indexAnalyzer":"prefixAnalyzer" },
]