Where should I be limiting my results?

2020-05-01 10:57发布

问题:

What I have done is created an XML file with a list of several thousand search terms that I need to perform on a document. I then created this query, from a sample set of search terms, as a test, to perform against a test document, with some samples from the actual document:

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>
)

Trying to get back <re><comment /><buyer /></re><re><comment /><buyer /></re>... continuous, but I am getting them back in a rough order.

This is a chunk from the document being parsed (I've removed the buyer names and some nests, to make it easier to read):

<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>

Some of the keywords I'm using: "Best client*," "Trade", "20", ....

I've been

The output is a long list of entries with comment and buyer children as siblings under the entry element. I'd like to limit the number of entries returned to 2 per keyword. I'm also trying to get comments that begin with an exclamation point (!) to be the priority.

Current output (getting close):

<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/>

Current output format:

<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>

Desired output:

<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>

(Basically, prioritizing exclamation point-containing entries and limiting the results to 2 per keyword.).

回答1:

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)
)

Returns the correct solution:

<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/>