Discussion:
XPath difference in queries
Amir Mistric
2007-06-19 00:46:37 UTC
Permalink
Hi

I am a bit new to Xpath in Jackrabbit but can someone tell me what is the difference between these 3 queries:

Q1: //*[@jcr:primaryType='mgnl:content']//*[jcr:contains(.,'term1') and jcr:contains(., 'term2')]

Q2: //*[@jcr:primaryType='mgnl:content' and jcr:contains(.,'term1') and jcr:contains(.,'term2')]

Q3: //element(*,nt:base)[@jcr:primaryType='mgnl:content' and jcr:contains(.,'term1') and jcr:contains(.,'term2')]


Basically all I want is to extract all the nodes of primary type "mgnl:content" and then to search them for the query terms which a
Amir Mistric
2007-06-19 01:18:34 UTC
Permalink
Now that I am reading the specifications I see following for jcr:contains() function :

--- begin quote ---
At minimum, all implementations must support the simple search-engine syntax defined by exp in the EBNF above. This syntax is based on the syntax of search engines like Google.
The semantics of the simple search expression are as follows:
• Terms separated by whitespace are implicitly ANDed together.
• Terms may also be ORed with explicit use of the OR keyword.
• AND has higher precedence than OR.
• Terms may be excluded by prefixing with a – (minus sign) character. This means that the result set must not contain the excluded term.
• A term may be either a single word or a phrase delimited by double quotes (").
• The entire text search expression (searchexp in the EBNF, above) must be delimited by single quotes (').
• Within the searchexp literal instances of single quote (“'”), double quote (“"”) and hyphen (“-”) must be escaped with a backslash (“\”). Backslash itself must therefore also be escaped, ending up as double backslash (“\\”).
--- end quote ---

Does this mean if I have a form in which user is entering search query I don't have to "build" (tokenize, validate, reconstruct, etc, etc) the XPAth query...
I can just pass in something like

jcr:contains(.,'rock AND roll OR disco -techno') and it will work?

Would I have to take care of the last point (escape special chars) or is that done automatically?


Thanks

Amir
-----Original Message-----
Sent: Monday, June 18, 2007 8:47 PM
Subject: XPath difference in queries
Hi
I am a bit new to Xpath in Jackrabbit but can someone tell me
) and jcr:contains(., 'term2')]
jcr:contains(.,'term1') and jcr:contains(.,'term2')]
jcr:contains(.,'term1') and jcr:contains(.,'term2')]
Basically all I want is to extract all the nodes of primary
type "mgnl:content" and then to search them for the query
terms which are ANDed.
Any help is appreciated
Marcel Reutegger
2007-06-20 14:01:02 UTC
Permalink
Post by Amir Mistric
Does this mean if I have a form in which user is entering search query I
don't have to "build" (tokenize, validate, reconstruct, etc, etc) the XPAth
query... I can just pass in something like
jcr:contains(.,'rock AND roll OR disco -techno') and it will work?
no, that doesn't work you have to create a valid XPath statement.
Post by Amir Mistric
Would I have to take care of the last point (escape special chars) or is that
done automatically?
you have to do that manually.

regards
marcel
Amir Mistric
2007-06-20 14:47:14 UTC
Permalink
So If someone enters in a form field a query string "rock AND roll OR disco -techno"
I cannot just use it in jcr:contains - I have to tokenize the string and use multiple jcr:contains.
SO instead of:

jcr:contains(.,'rock AND roll OR disco -techno')

I have to do:

jcr:contains(.,'rock') and jcr:contains(.,'roll') or jcr:contains(.,'disco') and not jcr:contains(.,'techno')

Is that correct?
If, so are there any utilities that would take a natural query and produce a valid xpath?


Thanks

Amir
-----Original Message-----
Sent: Wednesday, June 20, 2007 10:01 AM
Subject: Re: XPath difference in queries
Post by Amir Mistric
Does this mean if I have a form in which user is entering
search query
Post by Amir Mistric
I don't have to "build" (tokenize, validate, reconstruct, etc, etc)
the XPAth query... I can just pass in something like
jcr:contains(.,'rock AND roll OR disco -techno') and it will work?
no, that doesn't work you have to create a valid XPath statement.
Post by Amir Mistric
Would I have to take care of the last point (escape special
chars) or
Post by Amir Mistric
is that done automatically?
you have to do that manually.
regards
marcel
Marcel Reutegger
2007-06-22 06:37:41 UTC
Permalink
Post by Amir Mistric
So If someone enters in a form field a query string "rock AND roll OR disco
-techno" I cannot just use it in jcr:contains - I have to tokenize the string
and use multiple jcr:contains.
I'm sorry, that was a misunderstanding.

you *can* pass your example query string as is to the jcr:contains function.

I just wanted to point out that a sole jcr:contains function is not a valid
xpath statement.

regards
marcel

Marcel Reutegger
2007-06-20 13:59:48 UTC
Permalink
returns all node that contain 'term1' and 'term2' in one of its properties and
are a descendant of a node with primary type mgnl:content.
returns all node with a primary type mgnl:content that contain 'term1' and
'term2' in one of its properties
same as Q2
Basically all I want is to extract all the nodes of primary type "mgnl:content" and then to search them for the query terms which are ANDed.
that's:

//*[@jcr:primaryType = 'mgnl:content' and jcr:contains(., 'term1 term2')]

or if you are also interested in nodes, which are sub types of mgnl:content:

//element(*, mgnl:content)[jcr:contains(., 'term1 term2')]

regards
marcel
Continue reading on narkive:
Loading...