开发者

Xquery search on multiple keywords

I have written a basic XQuery that takes a parameter, performs a "contains" parameter on the data and returns the relevant data set.

let $search_term:= request:get-parameter("param1",0)
return 
  <root> { 
    for $node in doc('http://localhost:8080/data/doc.xml')/root/node
      for $value in $node/element/value
        where contains((upper-case($value)), (upper-case($search_term)))
      order by $node/title
      return $node
  } <root>

How would I go about extending this to search for multiple parameters. In my Java application, the user enters the search string in an edit box. Therefore the search string may c开发者_如何转开发ontain many words. Currently, this code only allows for searching on the search string as one entire phrase.

Is it possible to parse all of the words in a "param1" into a string sequence in XQuery and then perform a contains-any-of on this sequence. Finally, in extension, can I then order the results by the number of matches.

Thanks.


You have to write a search parser either in java or Xquery to do that. if it is a Xquery database like MarkLogic or exist-db u could leverge their Search APIs and Index.

if you want a simple search on single node text then you could transform users input into a REGEX and use fn:matches. Beware of XQuery injections in that case though.

looking at your code snippet i would like to suggest one more thing. please consider using Xpath as much as possible rather than for loop which is performance consuming.


You can certainly do something like replacing

where contains((upper-case($value)), (upper-case($search_term)))

by

where some $v in tokenize($value, '\s'), $k in tokenize($search_term, '\s')
satisfies upper-case($v) = upper-case($k)

However, I fear the results may be disappointing to users accustomed to intelligent free text searching available from engines like Google. Rather than building your own crude free text searching, it would be better to use something that does the job properly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜