1

I am creating a search for a website and using many filter options. I want to use filter on my many search results and for that i saw Filter property in SearchParameters for Azure Cognitive search. What i want is to pass a variable in Filter when i try to pass those parameters in filter search. Is there any possible way that i do not have to manually pass the Boulevard House from my data and use the variable houseName instead as i have provided options to choose and this is just plain-hardcode. Any refernce will help as well, as i tried to read the documents but in vain.

                {
                    Filter = String.Format("HouseName eq '{0}'", houseName)
                } ; 

                var names = new List<Search>();

                if (nameResult.Results.Count > 0)
                    {
                        foreach (SearchResult<Search> results in nameResult.Results)
                        {
                            names.Add(results.Document);
                        }
                    }

                NameSearchViewModel nameSearchViewModel = new NameSearchViewModel();
                nameSearchViewModel.Grants = names;
                return View(namesSearchViewModel);
Aryanzee
  • 11
  • 3
  • I don't totally understand what the problem is. If you pass the options with the filter to your search method, it should work. Also, last month we released Azure.Search.Documents - the new version we are focusing on going forward - which has OData filter helps that can escape values for you as needed. See https://www.nuget.org/packages/Azure.Search.Documents/ for an example. – Heath Aug 12 '20 at 01:46

1 Answers1

0

I'm assuming you are using the C# SDK. You could do something like this

parameters = new SearchParameters
{
    Filter = String.Format("HouseName eq '{0}'", houseName) 
}
ramero-MSFT
  • 920
  • 5
  • 10
  • Actually i am using a .NET SDK. – Aryanzee May 09 '20 at 07:41
  • Which language are you using? .NET is the name of the "framework", but the language you are using is likely c#. Did you have a chance to try the snippet of code I shared? – ramero-MSFT May 09 '20 at 19:37
  • I am using C# language with .NET SDK, Yes i tried using the snippet which you shared but it was not working as expected. Let me share a link for the documentation which i was checking for this issue but was unable to found any solution. https://learn.microsoft.com/en-us/azure/search/search-filters – Aryanzee May 10 '20 at 14:41
  • Hi Aryanzee, can you expand on what you mean by "not working as expected"? Are you getting a compilation or runtime error? Or is it compiling and running correctly, but not returning the expected results? If I understand your problem correctly, you want to know how to pass the content of your variable "houseName" as a filter parameter. The Filter property expect a value of type "string". You can either hard code a string, the way you did in your example, or you can use the String.Format function that I provided to "dynamically" create a string which contains the content of your variable. – ramero-MSFT May 11 '20 at 01:15
  • One important question you need to answer is : What type is your houseName variable? Is it a string? Can you maybe share a larger snippet of your code so we can understand the context of what you are trying to do a bit more? Thanks – ramero-MSFT May 11 '20 at 01:16
  • Thank you for the time. Yes the houseName variable is a string. I tried to use the String.Format but it was not giving the expected result. Let me explain when i hardcode the values like in the example above. It simply filter the results on the searchText but when i try to pass any variable like the way as String.Format or by concatenating it some other way, It does not throw runtime Error it just does not provide the results. I think the code snippet can definitely help you to understand that. – Aryanzee May 12 '20 at 04:32
  • I have made changes to the code snippet in the question so that you can have a look at it. I hope that can explain it in a better way. When i try to pass the variable as a parameter in the filter it is not returning any result and the Count = 0. thus my search result are empty. So what i think is it is not taking the filter correctly or doing something else. – Aryanzee May 12 '20 at 04:41
  • I would need more code snippets. In particular, I'm interested in what is happening before you emit the query. The snippet you shared only shows the code of how you read the results. Please share code that shows how you populate the "houseName" variable. Also, it would be useful for you to use the debugger and inspect the content of the "houseName" variable to make sure it contains the value you expect (before sending the search query) – ramero-MSFT May 13 '20 at 00:57