Hello,
I have a follow up question, regarding searching a specific element.
The suggestions provided were very helpful, but they don’t search for a
specific element within a path (as far as I can tell).
I’m getting results, but the problem is we have multiple elements with the name
of “subject”. So the search is returning results for all elements named
“subject”, rather than the specific “subject” element I’m looking for.
For example:
<subj-group subj-group-type="other">
<subject>Cell biology & molecular genetics</subject>
<subject>Crop genetics</subject>
<subject>Plant genetic resources</subject>
<subject>Production agriculture</subject>
<subject>Crop quality</subject>
</subj-group>
<subj-group subj-group-type="heading">
<subject>Beyond the Yield Curves: Exerting the Power of Genetics, Genomics, and
Synthetic Biology</subject>
</subj-group>
I’d really only like to search and return results for the green element above.
I’m using the following query now and it’s searching all <subject> elements and
returning them:
let $qtext := "Crop"
let $q := cts:and-query((cts:collection-query("content"),
cts:element-attribute-value-query(xs:QName("subj-group"),
xs:QName("subj-group-type"), "heading"),
cts:element-word-query(xs:QName("subject"), $qtext)))
return cts:element-values(xs:QName("subject"), (), (), $q)
How can I restrict it to a specific path/element?
Thanks much!
-Chad
From: [email protected]
[mailto:[email protected]] On Behalf Of Chad Bishop
Sent: Wednesday, May 06, 2015 9:09 AM
To: MarkLogic Developer Discussion
Subject: Re: [MarkLogic Dev General] Searching elements
Thank you all for your responses and help.
I’ll give those a try.
Thanks again,
-Chad
From:
[email protected]<mailto:[email protected]>
[mailto:[email protected]] On Behalf Of Indrajeet Verma
Sent: Wednesday, May 06, 2015 12:49 AM
To: MarkLogic Developer Discussion
Subject: Re: [MarkLogic Dev General] Searching elements
Hi Chad Bishop,
You can try using cts:element-values() as well to get distinct subject values
based on your input,
let $qtext := "whatever search"
let $terms := fn:tokenize($qtext," ")
let $q := cts:and-query((
cts:collection-query("content"),
cts:element-attribute-value-query(xs:QName("subj-group"),
xs:QName("subj-group-type"),"heading"),
cts:element-word-query(xs:QName("subject"), $terms)
))
return cts:element-values(xs:QName("subject"), (), (), $q)
If you want exact subject values, use cts:element-value-query and
case-insensitive option OR cts:element-range-query
Note - You would need to create range index for the subject to run above code.
[Inline image 1]
Regards,
Indy
On Wed, May 6, 2015 at 9:17 AM, Asitmohan Nautiyal
<[email protected]<mailto:[email protected]>> wrote:
Hi Chad,
For searching a specific element and returning the matching values of that
element use cts:element-value-query() function. Please refer
https://docs.marklogic.com/cts:element-value-query
Avoid use fn:distinct-values() function as its again a expensive in terms of
performance. Use cts:values() function to get distinct terms from a specified
element. Refer https://docs.marklogic.com/cts:values
sample code is here :
cts:values(cts:path-reference("*:subject,"http://marklogic.com/collation//S2<http://marklogic.com/collation/S2>"),(),(),
cts:and-query((
cts:collection-query("content"),
cts:element-attribute-value-query(xs:QName("subj-group"),
xs:QName("subj-group-type"),xs:string(heading)))))
Note : Create path range index for subject element in your database.
Regards,
Asit Nautiyal
________________________________
From:
[email protected]<mailto:[email protected]>
[[email protected]<mailto:[email protected]>]
on behalf of Chad Bishop
[[email protected]<mailto:[email protected]>]
Sent: Wednesday, May 06, 2015 3:04 AM
To: [email protected]<mailto:[email protected]>
Subject: [MarkLogic Dev General] Searching elements
Greetings,
I’m wondering if someone can help with the best way to approach searching a
specific element and returning the matching values of that element.
I figured xpath would be best so set this up:
let $term1 := "what"
let $term2 := "ever"
let $subjects :=
fn:collection("content")/article/front/article-meta/article-categories/subj-group[@subj-group-type
= "heading"]/subject[fn:contains(., $term1) and fn:contains(., $term2)]
for $subject in fn:distinct-values($subjects)
return $subject
However, the number of terms will be variable, so I created this:
let $qtext := "whatever search"
let $terms := fn:tokenize($qtext," ")
let $contains := for $term in $terms
return
if ($term != $terms[fn:last()])
then fn:concat("fn:contains(., ", """", $term, """", ") and ")
else fn:concat("fn:contains(., ", """", $term, """", ")")
let $containsText := fn:string-join($contains)
let $subjects :=
fn:collection("content")/article/front/article-meta/article-categories/subj-group[@subj-group-type
= "heading"]/subject[$containsText]
for $subject in fn:distinct-values($subjects)
return $subject
The problem is the $containsText isn’t evaluated as a function(s).
Is there a way to do that or should I be using something different?
Any help or advice is much appreciated,
-Chad
~~~~~~~~~~~
Chad Bishop
Lead Developer
Science Societies
Synergy in Science: Partnering for Solutions
2015 ASA, CSSA, and SSSA International Annual Meeting
with the Entomological Society of America
November 15-18 | Minneapolis, Minnesota
www.acsmeetings.org<http://www.acsmeetings.org/>
::DISCLAIMER::
----------------------------------------------------------------------------------------------------------------------------------------------------
The contents of this e-mail and any attachment(s) are confidential and intended
for the named recipient(s) only.
E-mail transmission is not guaranteed to be secure or error-free as information
could be intercepted, corrupted,
lost, destroyed, arrive late or incomplete, or may contain viruses in
transmission. The e mail and its contents
(with or without referred errors) shall therefore not attach any liability on
the originator or HCL or its affiliates.
Views or opinions, if any, presented in this email are solely those of the
author and may not necessarily reflect the
views or opinions of HCL or its affiliates. Any form of reproduction,
dissemination, copying, disclosure, modification,
distribution and / or publication of this message without the prior written
consent of authorized representative of
HCL is strictly prohibited. If you have received this email in error please
delete it and notify the sender immediately.
Before opening any email and/or attachments, please check them for viruses and
other defects.
----------------------------------------------------------------------------------------------------------------------------------------------------
_______________________________________________
General mailing list
[email protected]<mailto:[email protected]>
Manage your subscription at:
http://developer.marklogic.com/mailman/listinfo/general
_______________________________________________
General mailing list
[email protected]
Manage your subscription at:
http://developer.marklogic.com/mailman/listinfo/general