0
votes

I'm using the azure search api trying to filter by a certain field value: businesstype = store. It always returns 3 stores, even though I should have thousands. I can't tell for sure what's inside the index. In the Azure search web portal I type businessType eq 'store' and it gives me two stores, then starts returning businesstype = restaurant. Not sure what is going on. We have other implementations in other projects where filters are working. Here's code that I'm executing as it is invoked by using ASP.NET Web API

  var indexClient = new SearchIndexClient(GlobalSettings.SearchServiceName, $"businesses{GlobalSettings.Environment}", new SearchCredentials(GlobalSettings.SearchServiceAdminApiKey));

        if (latitude == null && longitude == null)
        {
            //chicago
            latitude = 41.8333925;
            longitude = -88.0121478;
        }

        // get all attributes and camel case them
        var attributes = typeof(BusinessSearchItem).GetProperties().Select(x => char.ToLowerInvariant(x.Name[0]) + x.Name.Substring(1)).ToList();
        var parameters = new SearchParameters
        {
            Select = attributes,
            QueryType = QueryType.Full,
            Top = take,
            Skip = skip,
            IncludeTotalResultCount = true,
            OrderBy = new List<string>() { $"geo.distance(location, geography'POINT({longitude} {latitude})')" }
        };

        // filters
        string filter = "";
        if (!string.IsNullOrEmpty(businessType))
        {
            switch (businessType.ToLower())
            {
                case "restaurant":
                    filter += "businessType eq 'Restaurant'";
                    break;

                case "store":
                    filter += "businessType eq 'Store'";
                    break;
            }// end switch on business type
        }

        parameters.Filter = filter;

        try
        {
            // run the search
            var results = indexClient.Documents.Search<BusinessSearchItem>(q, parameters);
            Logger.Log.Info($"Search conducted. Query: {q} Business Type: {businessType} Lat: {latitude} Long: {longitude} User: {username}");
            var businessDTOs = results.Results.Select(x => new BusinessDTO
            {
             .........
            ).ToList()
            }).ToList();

the model BusinessSearchItem has a field BusinessType of string that has the attribute searchable. The skip is 0 and take 40.

1
You mention that BusinessType is marked as searchable. Is it also marked as filterable? If not, I’d expect Search to throw CloudException.Bruce Johnston
Also, the casing of Store and Restaurant is inconsistent throughout your question. Filtering is case-sensitive, so it matters how you capitalize the literals. Can you confirm whether the casing of these values is consistent in the index? One way to check would be to facet on businessType without a filter, assuming it’s facetable. As for the Portal, it would help if you share the query string that you used.Bruce Johnston
Bruce, thanks for responding. BusinessType is also marked as IsFilterable. When I populate the index, I'm calling ToString on a BusinessType enum so I'm sure they've all got 'Store' with a capital S. In the portal, the query is like so: https://.....search.windows.net/indexes/businessesstaging/docs?api-version=2019-05-06&search=businessType%20eq%20'Store'Patrick Goode
It occurred to me I don't know whether you're talking about the property NAME or VALUE when referring to the casing. The serach model has a public string BusinessType with filterable and serachable attributes. I see the code is calling businesstype = 'Store' with a small b, is that what you mean?Patrick Goode
@PatrickGoode If your issue has been resolved, please add the answer to the answer part. Then this issue can be closed. Thank you.Tony Ju

1 Answers

0
votes

The problem wasn't the search at all, it was that the data was not in the index