我应该在哪里限制我的结果?

时间:2012-09-23 08:01:46

标签: sql-order-by xquery

我所做的是创建一个XML文件,其中包含我需要在文档上执行的数千个搜索词的列表。然后,我从一组搜索术语样本中创建了这个查询,作为测试,针对测试文档执行,其中包含来自实际文档的一些样本:

let $keywords := ("best clients", "Very", "20")
for $keyword in $keywords
let $matches := doc('test')/set/entry[matches(comment, $keyword, 'i')]
return (<re>
{subsequence($matches/comment, 1, 1),
subsequence($matches/buyer, 1, 1)}</re>,
<re>
{subsequence($matches/comment, 2, 1),
subsequence($matches/buyer, 2, 1)}
</re>
)

试图回到<re><comment /><buyer /></re><re><comment /><buyer /></re>... continuous,但我正在粗略地回复它们。

这是正在解析的文档中的一个块(我删除了买家名称和一些嵌套,以便于阅读):

<set>
<entry>
<comment>The client is only 20 years old.  Do not be surprised by his youth.</comment>
<buyer></buyer>
<id>1282</id>
<industry>International Trade; Fish and Game</industry>
</entry>
<entry>
<comment>!On leave in October.</comment>
<buyer></buyer>
<id>709</id>
<industry>Real Estate</industry>
</entry>
<entry>
<comment>Is often !out between 1 and 3 p.m.</comment>
<buyer></buyer>
<id>127</id>
<industry>Virus Software Marketting</industry>
</entry>
<entry>
<comment>Very personable.  One of our best clients.</comment>
<buyer></buyer>
<id>14851</id>
<industry>Administrative support.</industry>
</entry>
<entry>
<comment>!Very difficult to reach, but one of our top buyers.</comment>
<buyer></buyer>
<id>1458</id>
<industry>Construction</industry>
</entry>
<entry>
<comment></comment>
<buyer></buyer>
<id>276470</id>
<industry>Bulk Furniture Sales</industry>
</entry>
<entry>
<comment>A bit of an eccentric.  One of our best clients.</comment>
<buyer></buyer>
<id>1506</id>
<industry>Sports Analysis</industry>
</entry>
<entry>
<comment>Very gullible, so please !be sure she needs what you sell her.  She's one of our best clients.</comment>
<buyer></buyer>
<id>1523</id>
<industry>International Trade</industry>
</entry>
<entry>
<comment>He wants to buy everything, but !he has a tight budget.</comment>
<buyer></buyer>
<id>1524</id>
<industry>Public Relations</industry>
</entry>
</set>

我正在使用的一些关键词:“最佳客户*”,“交易”,“20”,....

我一直

输出是一个很长的条目列表,其中comment和buyer子项作为entry元素下的兄弟。我想限制返回2 per keyword的条目数。我也试图以感叹号(!)开头的评论为优先考虑。

当前输出(越来越接近):

<re><comment>Very personable.  One of our best clients.</comment>
  <buyer/>
</re><re><comment>A bit of an eccentric.  One of our best clients.</comment>
  <buyer/>
</re><re><comment>Very personable.  One of our best clients.</comment>
  <buyer/>
</re><re><comment>!Very difficult to reach, but one of our top buyers.</comment>
  <buyer/>
</re><re><comment>The client is only 20 years old.  Do not be surprised by his youth.</comment>
  <buyer/>
</re><re/>

当前输出格式:

<entry>
<comment>keyworda</comment>
<buyer></buyer>
</entry>
<entry>
<comment>keyworda</comment>
<buyer></buyer>
</entry>
<entry>
<comment>keywordb</comment>
<buyer></buyer>
</entry>
<entry>
<comment>!keywordb</comment> //Not prioritized.
<buyer></buyer>
</entry>
<entry>
<comment>keywordc</comment>
<buyer></buyer>
</entry>

期望的输出:

<entry>
<comment>!keyworda</comment>
<buyer></buyer>
</entry>
<entry>
<comment>keyworda</comment>
<buyer></buyer>
</entry>
<entry>
<comment>!keywordb</comment>
<buyer></buyer>
</entry>
<entry>
<comment>!keywordb</comment>
<buyer></buyer>
</entry>

(基本上,优先考虑包含感叹号的条目,并将结果限制为每个关键字2个。)。

1 个答案:

答案 0 :(得分:0)

let $reults :=
(
  let $pKeywords := ('best clients', 'Very', '20')
  return
    for $kw in $pKeywords
    return
    (
      /*/entry[contains(comment, concat('!', $kw))],
      /*/entry[contains(comment, $kw)]
    )
  [not(position() gt 2)]
)
for $i in (1 to count($results))
return
(
  subsequence($results/comment, $i, 1),
  subsequence($results/buyer, $i, 1)
)

返回正确的解决方案:

<comment>The client is only 20 years old.  Do not be surprised by his youth.</comment>
<buyer/>
<comment>Very personable.  One of our best clients.</comment>
<buyer/>
<comment>!Very difficult to reach, but one of our top buyers.</comment>
<buyer/>
<comment>A bit of an eccentric.  One of our best clients.</comment>
<buyer/>