Search code examples
xquerymarklogicmarklogic-7

xquery function to return a matching text come up on top ahead of others


I am trying to search for a text "car race" in an element in my database. I have the following results returned in my result, I need to order the result such that "car race" comes first since its an exact match, followed by other results in order by ascending. Any way to add a high score to "car race" result entry?

first car race
second car race
third car race
car race
first car race again
second car race again
third car race again


Solution

  • If you have elements whose values are as listed, you can use a boost query to increase scores for matches that span the complete element without changing the matching:

    cts:boost-query(
      cts:element-word-query(xs:QName("myelement"),"car race"),
      cts:element-value-query(xs:QName("myelement"),"car race"))
    

    The value query says "the match must fill the whole element"; the boost says "match the first query, add in scores from the second, if there is a match to that too".