1

I am working with a custom SXA search query token. I want to Insert with two different values.

foreach (var val in values)
  {
     args.Models.Insert(i, this.BuildModel(itemFieldName, val));
  }
args.Models.Remove(model);

SearchStringModel:

protected virtual SearchStringModel BuildModel(string fieldName, string value)
{
  var name = fieldName;
  return new SearchStringModel("custom", FormattableString.Invariant(FormattableStringFactory.Create("{0}|{1}", name, value)))
  {
    Operation = "must"
  };
}

But this logic is not showing search results. Is there any way to achieve this functionality?

4
  • Did you check the search logs to verify the query that was performed?
    – Gatogordo
    Sep 22, 2020 at 8:00
  • @Gatogordo yes query is being executed, shown in the logs. When one value is inserted, search results are working but not with the two.
    – PSS
    Sep 22, 2020 at 8:05
  • And is the query correct with 2 values? You can verify this query with your solr/azure search if needed to see what is wrong. And if not correct, share what is actually wrong with it.
    – Gatogordo
    Sep 22, 2020 at 8:09
  • @Gatogordo this is the search query: ?q=(itemid_sm:(ea15966f4e8e494384a9f210cd94d273) AND itemid_sm:(1646be1cb09d473fab86ebc9b7e5e668)), as you can see its putting the AND operator instead of OR between the values. How can I put OR instead of AND?
    – PSS
    Sep 22, 2020 at 8:29

1 Answer 1

0

The AND or OR operators in between the parts of your query are placed based on the Operation as set in the SearchStringModel.

In your case, the Operation is set to "must" - which is an AND. If you want an OR, you can use "should" here:

protected virtual SearchStringModel BuildModel(string fieldName, string value)
{
  var name = fieldName;
  return new SearchStringModel("custom", FormattableString.Invariant(FormattableStringFactory.Create("{0}|{1}", name, value)))
  {
    Operation = "should"
  };
}
4
  • If should is added as operation, then it changes the overall query. should operation query: (_template:(36206e5352f74164ad95e6c18f35dd53) OR itemid_sm:(ea15966f4e8e494384a9f210cd94d273) OR itemid_sm:(1646be1cb09d473fab86ebc9b7e5e660)). must operation query: (_template:(36206e5352f74164ad95e6c18f35dd53) AND (itemid_sm:(ea15966f4e8e494384a9f210cd94d273) AND itemid_sm:(1646be1cb09d473fab86ebc9b7e5e660))). Desired query: (_template:(36206e5352f74164ad95e6c18f35dd53) AND (itemid_sm:(ea15966f4e8e494384a9f210cd94d273) OR itemid_sm:(1646be1cb09d473fab86ebc9b7e5e660)))
    – PSS
    Sep 22, 2020 at 9:30
  • Can you share the query with you token that you used?
    – Gatogordo
    Sep 22, 2020 at 10:03
  • Query: template:{1646BE1C-B09D-473F-AB86-EBC9B7E5E111};sxa:ItemsWithSameFieldValue|UserFieldName|ItemFieldName. I have also tried +template:{1646BE1C-B09D-473F-AB86-EBC9B7E5E111};sxa:ItemsWithSameFieldValue|UserFieldName|ItemFieldName. OR operator is needed between the values inserted by this token.
    – PSS
    Sep 22, 2020 at 12:45
  • I think the "+" (also) needs to be in front of the sxa:ItemsWithSameFieldValue.. (not sure, can't test this myself now)
    – Gatogordo
    Sep 22, 2020 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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