0

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"])
0

You should make sure that your GetListOfStringFromFields method return not null value (to enable quering by empty list):

public List<string> GetListOfStringFromFields(string fieldName)
{
  var results = ...; // your existing logic to extract tag values
  return results ?? new List<string>{string.Empty}; // to store in solr empty list - semanticscomputed_sm:[""]
}

Than you can update your code following:

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).Boost(10)); // boost matched tags
}

predicate = predicate.Or(p => p.Semantics.Contains("")) // query items with empty tags list without boosting

expr = expr.And(predicate);

In this case you will get Solr query:

(semanticscomputed_sm:["tag1"]^10 OR semanticscomputed_sm:["tag2"]^10 OR semanticscomputed_sm:[""])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.