带有构面的 SearchIndexClient

SearchIndexClient with facets

我在查询索引时发现了多个样本,例如:

results = indexClient.Documents.Search<Hotel>("budget", parameters);

但是我在使用构面时找不到任何东西。您如何使用 SearchServiceClient 和构面?

在搜索服务资源管理器中,它将类似于:

&facet=Group

结果:

{
    "@odata.context": "https://xxx-dev.search.windows.net/indexes('influencers')/$metadata#docs",
    "@search.facets": {
        "Group@odata.type": "#Collection(Microsoft.Azure.Search.V2017_11_11.QueryResultFacet)",
        "Group": [
            {
                "count": 426,
                "value": "Gaming"
            },
            {
                "count": 388,
                "value": "Action Sports"
            },
            {
                "count": 379,
                "value": "Music"
            },
            {
                "count": 378,
                "value": "Sport"
            }
        ]
    },
    "value": [
        {
            "@search.score": 1,
            "id": "fc4b1200-fb91-4fe0-a251-beb351ee2988",
            "FirstName": "Chase",
            "LastName": "Powell",
            "Mobile": "500-0545772",
            "Country": "Sweden",
            "Group": "Music",
            "SubGroups": [
                "Jazz",
                "Electronic Dance",
                "Rock Music",
                "Pop",
                "Techno",
                "Indie Rock",
                "Dubstep"
            ]
        },
        {
            "@search.score": 1,
            "id": "131f3d54-9b36-4b60-bb38-4d412bcc1682",
            "FirstName": "Ian",
            "LastName": "Bryant",
            "Mobile": "236-3224487",
            "Country": "Denmark",
            "Group": "Gaming",
            "SubGroups": [
                "World of Warcraft ",
                "Counter-Strike",
                "League of Legends"
            ]
        }

However I cant find anything when working with facets. How do you work with the SearchServiceClient and facets?

我们可以查看 Azure 搜索 demo source code 以获得答案。 以下是 Azure 搜索演示中的代码片段。

SearchParameters sp = new SearchParameters()    
{    
    ...
    Select = new List<String>() {"id", "agency", "posting_type",...},
    ....
    // Add facets    
    Facets = new List<String>() { "business_title", "posting_type", "level", "salary_range_from,interval:50000" },

};
// Add filtering

string filter = null;
if (businessTitleFacet != "")
filter = "business_title eq '" + businessTitleFacet + "'";
if (postingTypeFacet != "")

  {
    if (filter != null)
    filter += " and ";
    filter += "posting_type eq '" + postingTypeFacet + "'";

  }
 ....
 sp.Filter = filter;
 _indexClient.Documents.Search(searchText, sp);