简体   繁体   English

如何使用 cts:element-values 在 XQuery 下实现

[英]How to impliment below XQuery using cts:element-values

I am having below query which is using cts:search which is taking lot of time while running in production我有以下使用cts:search查询,它在生产中运行时需要大量时间

xquery version "1.0-ml";
(: let $limit := ()  use this instead to display all values :)
for $uri in cts:uri-match("/tag-cloud/*")
let $values :=""
let $region := fn:substring-before(fn:substring-after($uri, "/tag-cloud/"), ".xml")
let $doc := concat("/tag-cloud/",$region,".xml")
let $distinct-search-text := cts:search(doc($doc)/All-searchtext/searchtext,
               cts:element-attribute-range-query(xs:QName("searchtext"), xs:QName("date"),
               ">=",
              (fn:current-date() - xs:dayTimeDuration("P2D"))
              )


             )

let $result-xml :=  for $each-search-text in fn:distinct-values($distinct-search-text/text())
                let $count := count($distinct-search-text[text() eq $each-search-text]) 
                order by $count descending
                return 
                fn:concat($region,"	", $each-search-text, "	",$count) 


return 
for $eachtag in (($result-xml)[1 to 15])
  return ($eachtag)

Can anyone please help me in re-writing above query using cts:element-values function because I just need values with frequency.任何人都可以帮助我使用cts:element-values函数重写上述查询,因为我只需要频率值。

I tried below query but filtering is not working properly.我尝试了以下查询,但过滤无法正常工作。 Any help is much appriciated任何帮助都非常有用

xquery version "1.0-ml";
let $limit := 15
(: let $limit := ()  use this instead to display all values :)
for $uri in cts:uri-match("/tag-cloud/*")
let $region := fn:substring-before(fn:substring-after($uri, "/tag-cloud/"), ".xml")
let $values :=
cts:element-values(
  xs:QName("searchtext"),
  "",
  (
    "collation=http://marklogic.com/collation/codepoint",
    "item-frequency",
    "frequency-order",
    "descending",
    if (fn:exists($limit)) then fn:concat("limit=", $limit) else ()
  ),
 cts:and-query((
   cts:element-attribute-range-query(xs:QName("searchtext"), xs:QName("date"),
               ">=",
              (fn:current-date() - xs:dayTimeDuration("P2D"))
              ),
 cts:document-query($uri)))
 )
 for $value in $values
 return
   fn:concat($region, "	", $value, "	", cts:frequency($value))

Thanks谢谢

If you retrieve a document with doc() , there's no reason to search.如果您使用doc()检索文档,则没有理由进行搜索。 You already have the document.您已经拥有该文档。

Another general point: you should try to build a single search request that does what you want instead of iterating and executing multiple search requests.另一个普遍观点:您应该尝试构建一个可以执行您想要的操作的单个搜索请求,而不是迭代和执行多个搜索请求。 For example,instead of iterating over cts:uri-match() , consider whether you could perform a single cts:search() with a cts:directory-query() clause.例如,与其迭代cts:uri-match()cts:uri-match()考虑是否可以使用cts:directory-query()子句执行单个cts:search()

For the particularly problem, I'm not sure I understand the goal.对于特别的问题,我不确定我是否理解目标。 If you create a range index on searchtext, you can calculate frequencies as described in:如果您在搜索文本上创建范围索引,则可以按如下所述计算频率:

http://docs.marklogic.com/cts:frequency http://docs.marklogic.com/cts:频率

If you need those frequencies to reflect the region, one approach would be to build the region into the searchtext values.如果您需要这些频率来反映区域,一种方法是将区域构建到搜索文本值中。

But, maybe I misunderstand the goal.但是,也许我误解了目标。

Hoping that's of some help,希望能有所帮助

Erik Hennum埃里克·亨纳姆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM