问题
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")
回答1:
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
回答2:
why dont you try the OData filter if you want to perform exact match? In this case your query will become $filter=DocumentType eq 'Type'
Note that if you you the preview version of azure search, api-version=2015-02-28-Preview, they have added new filters for partial matching.
search.ismatch("filedvalue", "fieddname") and saerch.ismatchscoring()
Also, did you checked the Custom Analyzer? If you know that you will be always performing exact match on few fields then use "keyword" analyzer with lowercase tokeniser
https://docs.microsoft.com/en-us/rest/api/searchservice/custom-analyzers-in-azure-search
来源:https://stackoverflow.com/questions/43332467/return-records-from-azure-search-that-match-the-exact-search-term-only