Project computes semantic field with list of tags (stringArray
).
Requirement is to search for items that tag list intersect with given set of tags. But also if the item tags list is empty we would also like to return it, but with lower score.
Following query in SOLR does what I want:
(semanticscomputed_sm:["tag1"] OR semanticscomputed_sm:["tag2"] OR *:*)
But I don't know how to write a query in Sitecore to generate above query.
Code to generate query right now:
...
var tags = new string[] { "tag1", "tag2" };
var predicate = PredicateBuilder.True<CustomSearchResult>();
foreach (var t in tags)
{
var tempTerm = t;
predicate = predicate.Or(p => p.Semantics.Contains(tempTerm));
}
expr = expr.And(predicate);
Custom SearchResult class:
public class CustomSearchResult : SearchResultItem
{
[IndexField("semanticscomputed")]
public List<string> Semantics
{
get { return GetListOfStringFromFields("semanticscomputed"); }
set { _merckSemantics = value; }
}
}
Query that is generated by below code:
(semanticscomputed_sm:["tag1"] OR semanticscomputed_sm:["tag2"])