如何在天蓝色搜索中基于字段进行搜索? [英] How to search based on field in azure search?
问题描述
我正在使用azure搜索,并且我有一个带有以下代码的控制台应用程序,效果很好.
i am using a azure search, and i have a console app with the code as below, which is working fine.
DocumentSearchResult<Hotel> results;
Console.WriteLine("Search started\n");
results = indexClient.Documents.Search<Hotel>("smart", new SearchParameters { Top=5 });
WriteDocuments(results);
目前,它正在搜索带有"smart"一词的文本.这很简单,我需要的是表中有几个字段,我想根据字段进行搜索.
currently its searching a text with word "smart". this is straight forword, what i need is i have several fields in the table, i want to search based on the feild .
例如,让我有两个字段 1)标题 2)SoldDate
for example let i have two fields 1)Title 2)SoldDate
我必须编写代码来查找标题为"john"且销售日期为<当前日期.
I have to write code for finding items which has title 'john' and which has a sold date < current date.
我应该怎么做才能做到这一点?
what should i do to achieve this?
推荐答案
您可以通过搜索和过滤器实现所需的目标:
You can achieve what you want with search and a filter:
// Approach #1
string currentDate = DateTime.UtcNow.ToString("O");
var parameters = new SearchParameters()
{
Filter = "soldDate lt " + currentDate,
Top = 5
}
results = indexClient.Documents.Search<Hotel>("john", parameters);
这将仅将文档过滤为在currentDate
之前带有soldDate
的那些文档,然后搜索过滤的文档,以便如果任何可搜索字段包含"john"的文档都匹配.您可以像这样将范围缩小到title
字段:
This will filter the documents to only those with a soldDate
before currentDate
, and then searches the filtered documents such that documents match if any of the searchable fields contain "john". You can narrow this down to just the title
field like this:
// Approach #2
string currentDate = DateTime.UtcNow.ToString("O");
var parameters = new SearchParameters()
{
Filter = "soldDate lt " + currentDate,
SearchFields = new[] { "title" },
Top = 5
}
results = indexClient.Documents.Search<Hotel>("john", parameters);
或者像这样:
// Approach #3
string currentDate = DateTime.UtcNow.ToString("O");
var parameters = new SearchParameters()
{
Filter = "soldDate lt " + currentDate,
QueryType = QueryType.Full,
Top = 5
}
results = indexClient.Documents.Search<Hotel>("title:john", parameters);
使用哪种方式取决于您是希望将所有搜索词限制为一组特定的字段(方法2),还是要使特定术语匹配特定的字段(方法3).
Which way you use depends on whether you want all search terms to be limited to a specific set of fields (Approach #2), or if you want specific terms to match specific fields (Approach #3).
SearchParameters
的参考位于 docs.microsoft .com .
这篇关于如何在天蓝色搜索中基于字段进行搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!