如何在天蓝色搜索中基于字段进行搜索?

How to search based on field in azure search?

我正在使用 azure 搜索,我有一个控制台应用程序,代码如下,运行良好。

        DocumentSearchResult<Hotel> results;
        Console.WriteLine("Search started\n"); 
        results = indexClient.Documents.Search<Hotel>("smart", new SearchParameters { Top=5 });
        WriteDocuments(results);

目前正在搜索包含单词 "smart" 的文本。这是直截了当的,我需要的是 table 中有几个字段,我想根据字段进行搜索。

例如让我有两个字段 1)标题 2) 售出日期

我必须编写代码来查找标题为 'john' 且销售日期小于当前日期的商品。

我应该怎么做才能做到这一点?

您可以通过搜索和过滤器实现您想要的结果:

// 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 字段:

// 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)。

SearchParameters 的引用在 docs.microsoft.com