Using variables in azure search filter - azure-cognitive-search

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);

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)
}

Related

DotNet Core Azure Search SDK - filtering results

We are trying to implement Filter functionality into Azure (Cognitive) Search. I was hoping to find some nice SDK methods that hide all the ugly parts, but so far the only example I found looks like this (source):
SearchParameters parameters = new SearchParameters()
{
Filter = String.Format("groupIds/any(p:search.in(p, '{0}'))", string.Join(",", groups.Select(g => g.ToString()))),
Select = new[] { "application essays" }
};
I was wondering, whether I am missing some docs. Or maybe it is on the roadmap?
Check out our new Azure.Search.Documents SDK we released last month. It does have OData filter helps as you can find here:
int stars = 4;
SearchOptions options = new SearchOptions
{
// Filter to only Rating greater than or equal our preference
Filter = SearchFilter.Create($"Rating ge {stars}"),
Size = 5, // Take only 5 results
OrderBy = { "Rating desc" } // Sort by Rating from high to low
};
It'll escape string parameters correctly. The OData $filter syntax still requires raw input, but the type helpers in the formattable string should make your situation easier: you don't have to worry about escaping values yourself.

Azure Search and using ends with in Array

I am using the latest Microsoft.Azure.Search SDK with the following search parameters. I have filter ID's that are MFR-1, MFR-2, MFR-3, etc. I am trying to bring back ANY record that has Filter ID that starts with MFR.
It seems that this should be simple query, but I am not finding a way to make this work with the SDK.
var Params = new SearchParameters()
{
SearchMode = SearchMode.Any,
QueryType = QueryType.Full,
Top = 72,
Skip = 0,
IncludeTotalResultCount = true,
Filter = "FilterIDs/any(c: c eq 'MFR-57')",
OrderBy = new List<string> { "Sort", "Title"},
Facets = new List<string>() { "Filters,count:500,sort:value" }
};
Data looks like this:
{
"id": "691",
"RecordType": "product",
"FilterIDs": [
"MFR-106",
"36-250",
"36-265"
],
}
I've tried this, but it doesn't appear to work with arrays as the title would suggest.
Contains / in array in Azure Search (Preview)
Per my understanding , you are looking for a filter express that could filter all record whose FilterIDs string collection(array) contains item value started with "MFR" .
As official doc indicated :
Inside lambda expressions for string collections, the only comparison
operators that can be used are eq and ne.
So I am afraid there is no way to do fuzzy search here.
But if your filter ids are enumerable , maybe you can use the filter express as below :
FilterIDs/any(c: c eq 'MFR-1') or FilterIDs/any(c: c eq 'MFR-2') or FilterIDs/any(c: c eq 'MFR-3') or ....
I think it would be a work around here, it works for me on my side .
Hope it helps.

How to create complex query parameters in Restangular

I need to create a fairly complex query string in Restangular.
http://vdmta.rd.mycompany.net//people?anr=Smith&attrs=givenName,displayName,name,cn
How do I do this?
So far I am OK getting as far as ?anr=Smith using this:
return Restangular.all('/people').getList({anr:searchTerm});
The last part attrs=x,y,x lets me control which attributes I want back in the search and could change per request I make.
Any advice appreciated.
Regards
i
You should be able to simply add another query parameter where the value is your comma separated list of attributes.
var attributes = ['givenName' , 'displayName']; // attributes you require for the request
var attributesAsString = attributes.join();
return Restangular.all('/people').getList({
anr : searchTerm,
attrs: attributesAsString
});

Search For All Locations In Active Dirrectory Via LDAP

I'm working in PL/SQL and searching LDAP ( with A.D defining the schema) for all locations. Right now I can apply a simple search and find all users. Each user has the address information via the following properties:
'physicalDeliveryOfficeName';
'streetAddress';
'l';--city
'st';--state
'postalCode';--zip code
However, I would like to search for all the locations separate from the search done for people. Is it possible to search Active directory to just find the locations(with out looking up each person) ? If so what would the search filter look like ? I tried objectClass=Physical-Location,DC=example,DC=com and didn't find any locations (beyond the schema) . I'm not sure if that's because there's a security issue, or its not possible to look up locations in that way.
What you have listed are attributes in AD. You can return attributes in searches and search for specific values but you'll always return the objects the attributes are attached to (in this case users). You're a little light on the details of how you're searching so I'll take a stab.
You can load just the location attributes you're looking for, be it State, City, etc.
var domain = "mydomain.com";
var dn = "CN=Users,DC=mydomain,DC=com";
var ldapSearchFilter = "(objectClass=user)";
var connection = new LdapConnection(domain);
var attributeList = new string[] { "physicalDeliveryOfficeName", "l", "st"};
try
{
var searchRequest =
new SearchRequest(dn, ldapSearchFilter,
SearchScope.OneLevel,
attributeList);
var searchResponse =
(SearchResponse)connection.SendRequest(searchRequest);
var locationList = (from SearchResultEntry entry in searchResponse.Entries
select entry.Attributes["physicalDeliveryOfficeName"][0].ToString())
.Distinct().ToList();
catch (Exception ex)
{
//Handle errors
}
One thing to keep in mind with this example. If the attributes aren't populated in AD, the WriteLine will throw an error when trying to read the attribute. If you are using some other search type (DirectorySearcher maybe) you should still be able to load just the attributes you want to get back.

Solrnet equivalent for frange in a facet query for Distance

I have implemented Spatial Search and need to generate facets for ranges or 'buckets' of distance in Solr: the following link helped me here - http://wiki.apache.org/solr/SpatialSearch#How_to_facet_by_distance
I found the solution to the following question
Faceting on distance in solr- how to generate links that search withing a given range of distance helpful.
I am using Solrnet to generate Solr queries from my .NET based site. I cant find a link that would help me to generate a frange part (italicized) of the query as below. Please help in pointing me to the code to generate this using solrnet.
&q=:&sfield=store&pt=45.15,-93.85**&facet.query={!frange l=0 u=5}geodist()&facet.query={!frange l=5.001 u=3000}geodist()**.
I am assuming that you are already using ExtraParams to set the sField & pt parameters. You should be able to add the facet.query parameters to this as well.
var results = solr.Query(SolrQuery.All, new QueryOptions
{
ExtraParams = new Dictionary<string, string> {
{ "sfield", "store" } ,
{ "pt", "45.15,-93.85" }
{ "facet.query", "{!frange l=0 u=5}geodist()" } ,
{ "facet.query", "{!frange l=5.001 u=3000}geodist()" } ,
}
});
return results;
Additionally, you might be able to use SolrFacetQuery combined with LocalParams to build the facet queries. Check the following links for some examples:
https://code.google.com/p/solrnet/wiki/Facets
How to add Spatial Solr to a Solrnet query
Update:
As stated in the comments, the use of the ExtraParams does not work because it is a dictionary object and as a result does not allow for multiple facet.query parameters. However, I was able to use the Facet Queries as described in the SolrNet Facet Wiki page linked above to create the following:
var facet1 = new SolrFacetQuery(new SolrQuery("{!frange l=0 u=5}geodist()}"));
var facet2 = new SolrFacetQuery(new SolrQuery("{!frange l=5.001 u=3000}geodist()}"));
var results = solr.Query(SolrQuery.All, new QueryOptions
{
ExtraParams = new Dictionary<string, string>
{
{"sfield", "store"},
{"pt", "45.15,-93.85"}
},
Facet = new FacetParameters
{
Queries = new[]
{
facet1, facet2
}
}
});
return results;

Resources