从天蓝色搜索返回仅与确切搜索词匹配的记录 [英] Return records from azure search that match the exact search term only

查看:110
本文介绍了从天蓝色搜索返回仅与确切搜索词匹配的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我是Azure搜索的新手,我才刚刚开始弄清楚它.我有一个搜索索引,用于查询一个非常简单的sql视图以查找匹配的记录.我很难找到与我搜索的单词完全匹配的单词.

每当我搜索时,我都会得到仅包含确切单词的记录.但是,我也会得到包含搜索到的单词的记录,例如,我搜索类型",然后得到新类型"和我的类型".我在搜索时将类型"括在双引号中.

我正在使用C#SDK,当我通过邮递员执行搜索时也会发生这种情况:

https://myapp.search.windows.net/indexes/myindex/docs?$select=AlertDate,DocumentName,City,DocumentType&search=(DocumentType:"Type")&$count=true&queryType=full&searchMode=any&api-version=2016-09-01

有什么想法会导致这种情况吗?我怎么才能只获得完全匹配?我已经阅读了相当多的文章,并且有几篇文章和网页说,将搜索词用双引号引起来只会得到确切的词,但这似乎并不正确,至少对我来说不是.

为完整起见,我应该添加在C#中使用的调用以检索结果:

var result = await indexClient.Documents.SearchAsync(query, new SearchParameters()   
{ Facets = parameters.Facets, QueryType = QueryType.Full, SearchMode = SearchMode.Any,   
Top = request.Top, Skip = request.Skip, IncludeTotalResultCount = true });

有时,搜索将只是一个单词或多个单词,而有时可能是用户从下拉菜单中选择的过滤器,例如国家/地区"和城市",或两者结合.然后,我形成如下查询字符串:

(Country:"France" OR "Germany") AND (City:"Paris")

如果它还包含搜索词,则查询将如下所示:

"Type" AND (Country:"Italy" OR "France") AND (City:"Paris" OR "Rome")

解决方案

用双引号引起来的查询将其转换为短语查询.仅当引号之间有多个术语,并用空格,标点符号等分隔时,这才有意义.例如,如果您的搜索字符串为Hello world,它将匹配包含"hello"或"world"的文档(如果您正在使用默认的searchMode any并假设您未显式使用任何其他运算符),但是如果您的搜索字符串为"Hello world",则只有两个术语彼此相邻时,文档才匹配./p>

看着您的查询,我猜您想按分类数据进行过滤.如果您确实希望完全匹配(区分大小写),则应使用$ filter而不是search:

https://myapp.search.windows.net/indexes/myindex/docs?$select=AlertDate,DocumentName,City,DocumentType&$filter=DocumentType eq 'Type'&$count=true&queryType=full&searchMode=any&api-version=2016-09-01

以下是一些有用的链接,用于在Azure搜索中构造过滤器和搜索查询:

https://docs.microsoft .com/rest/api/searchservice/lucene-query-syntax-in-azure-search https://docs.microsoft.com/rest/api/searchservice/odata-expression-syntax-for-azure-search

So I'm new to Azure search and I'm only starting to figure it out. I have a search index that queries a pretty simple sql view for matching records. I'm having major trouble getting an exact match for the word I search for.

Whenever I search I get records with the exact word only.. however I also get records that contain the searched for word e.g I search for "Type" and I get "New Type" and "My Type". I'm enclosing "Type" in double quotes when I search.

I'm using the C# SDK, and it also happens when I execute a search via Postman:

https://myapp.search.windows.net/indexes/myindex/docs?$select=AlertDate,DocumentName,City,DocumentType&search=(DocumentType:"Type")&$count=true&queryType=full&searchMode=any&api-version=2016-09-01

Any idea what might cause this and how I can get exact matches only? I've read a fair bit on it and several posts and web pages say that wrapping the search term in double quotes will get the exact term only, but this doesn't seem to be correct at least not for me.

Edit:

For completeness I should add the call that I'm using in C# to retrieve the results:

var result = await indexClient.Documents.SearchAsync(query, new SearchParameters()   
{ Facets = parameters.Facets, QueryType = QueryType.Full, SearchMode = SearchMode.Any,   
Top = request.Top, Skip = request.Skip, IncludeTotalResultCount = true });

Sometimes the search would be just a single word or multiple words, other times it could be filters that a user would select from drop downs such as Country and City, or a combination of both. I then form the query string like this:

(Country:"France" OR "Germany") AND (City:"Paris")

If it also includes a search term the query would look like this:

"Type" AND (Country:"Italy" OR "France") AND (City:"Paris" OR "Rome")

解决方案

Enclosing a query in double quotes turns it into a phrase query. This is only meaningful if there is more than one term between the quotes, separated by whitespace, punctuation etc. For example, if your search string is Hello world, it will match documents that contain "hello" or "world" (if you're using the default searchMode of any and assuming you don't use any other operators explicitly), but if your search string is "Hello world", then documents only match if the two terms are adjacent to each other.

Looking at your query, I'm guessing you want to filter by categorical data. If you truly want an exact match (case-sensitive), then you should use $filter instead of search:

https://myapp.search.windows.net/indexes/myindex/docs?$select=AlertDate,DocumentName,City,DocumentType&$filter=DocumentType eq 'Type'&$count=true&queryType=full&searchMode=any&api-version=2016-09-01

Here are a few useful links for constructing filter and search queries in Azure Search:

https://docs.microsoft.com/rest/api/searchservice/lucene-query-syntax-in-azure-search https://docs.microsoft.com/rest/api/searchservice/odata-expression-syntax-for-azure-search

这篇关于从天蓝色搜索返回仅与确切搜索词匹配的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆