Hi Danny and Michael,

Danny, I am trying to avoid writing my own parsing code. It is easy
for a simple AND but it gets complex when query should include
phrases, groups and sub-groups. I was hoping that search:parse will do
it for fields too.
Michael, xqysp seems like a promising solution.

Thank you,
Majid

On 17 November 2011 14:10, Danny Sokolsky <[email protected]> wrote:
> Hi Majid and Mike,
>
> I think you can accomplish this with the search API as follows:
>
> xquery version "1.0-ml";
> import module namespace search =
>  "http://marklogic.com/appservices/search";
>  at "/MarkLogic/appservices/search/search.xqy";
>
> let $options :=
> <options xmlns="http://marklogic.com/appservices/search";>
>  <constraint name="DOC">
>    <word>
>        <field name="my-field"/>
>    </word>
>  </constraint>
> </options>
> return
> search:parse("DOC:foo AND DOC:bar", $options)
>
> =>
> <cts:and-query qtextjoin="AND" strength="20" qtextgroup="( )" 
> xmlns:cts="http://marklogic.com/cts";>
>  <cts:field-word-query qtextpre="DOC:" qtextref="cts:annotation">
>    <cts:annotation qtextref="following-sibling::cts:text"/>
>    <cts:field>my-field</cts:field>
>    <cts:text>foo</cts:text>
>  </cts:field-word-query>
>  <cts:field-word-query qtextpre="DOC:" qtextref="cts:annotation">
>    <cts:annotation qtextref="following-sibling::cts:text"/>
>    <cts:field>my-field</cts:field>
>    <cts:text>bar</cts:text>
>  </cts:field-word-query>
> </cts:and-query>
>
> So if you can write some code to parse your search string into this format, I 
> think you will have what you want.
>
> -Danny
>
> -----Original Message-----
> From: [email protected] 
> [mailto:[email protected]] On Behalf Of Michael Blakeley
> Sent: Thursday, November 17, 2011 9:42 AM
> To: General MarkLogic Developer Discussion
> Subject: Re: [MarkLogic Dev General] Advance queries in fields with 
> search:parse
>
> I don't think search:parse implements the grammar you want. Take a look at 
> https://github.com/mblakele/xqysp - it is a bit more powerful and more 
> flexible than search:parse. Once you have the cts:query you want, you can 
> pass it to search:resolve and continue to use normal search API techniques.
>
> -- Mike
>
> On 17 Nov 2011, at 09:31 , Majid Valipour wrote:
>
>> Hi all,
>>
>> I need to have advanced queries within a field. For example for field
>> "DOC" and query text "foo AND bar" the query text is expected to be
>> parsed into the following query:
>> <cts:and-query>
>>   <cts:field-word-query>
>>     <cts:field>DOC</cts:field>
>>     <cts:text>foo</cts:text>
>>  </cts:field-word-query>
>>  <cts:field-word-query>
>>      <cts:field>DOC</cts:field>
>>      <cts:text>foo</cts:text>
>>  </cts:field-word-query>
>> </cts:and-query>
>>
>> I very much prefer to use search:parse to achieve this because the
>> parser should handle complex queries that may include phrase,
>> grouping, and etc and search:parse does this quite well.
>> However search:parse seems unable to parse the field queries the same
>> way that it does text queries (see below for a demo). Is there any way
>> to do this with search:parse or should I write my own parser?
>>
>> Here is a simple script to demonstrate this.
>> xquery version "1.0-ml";
>> import module namespace search =
>> "http://marklogic.com/appservices/search"; at
>> "/MarkLogic/appservices/search/search.xqy";
>> let $options :=<options xmlns="http://marklogic.com/appservices/search";>
>>                 <constraint name="DOC">
>>                     <word><field name="doc-field"/></word>
>>                 </constraint>
>>             </options>
>> for $query in ('foo AND bar', 'DOC:foo AND bar', 'DOC:"foo AND bar"',
>> 'DOC:(foo AND bar)')
>> return fn:concat($query,' => ',xdmp:quote(search:parse($query, $options)))
>>
>> results in:
>>
>> foo AND bar => <cts:and-query qtextjoin="AND" strength="20"
>> xmlns:cts="http://marklogic.com/cts";>
>>  <cts:word-query qtextref="cts:text">
>>    <cts:text>foo</cts:text>
>>  </cts:word-query>
>>  <cts:word-query qtextref="cts:text">
>>    <cts:text>bar</cts:text>
>>  </cts:word-query>
>> </cts:and-query>
>> DOC:foo AND bar => <cts:and-query qtextjoin="AND" strength="20"
>> xmlns:cts="http://marklogic.com/cts";>
>>  <cts:field-word-query qtextpre="DOC:" qtextref="cts:annotation">
>>    <cts:annotation qtextref="following-sibling::cts:text"/>
>>    <cts:field>doc-field</cts:field>
>>    <cts:text>foo</cts:text>
>>  </cts:field-word-query>
>>  <cts:word-query qtextref="cts:text">
>>    <cts:text>bar</cts:text>
>>  </cts:word-query>
>> </cts:and-query>
>> DOC:"foo AND bar" => <cts:field-word-query qtextpre="DOC:"
>> qtextref="cts:annotation" xmlns:cts="http://marklogic.com/cts";>
>>  <cts:annotation qtextpre="&quot;"
>> qtextref="following-sibling::cts:text" qtextpost="&quot;"/>
>>  <cts:field>doc-field</cts:field>
>>  <cts:text>foo AND bar</cts:text>
>> </cts:field-word-query>
>> DOC:(foo AND bar) => <cts:field-word-query qtextpre="DOC:"
>> qtextref="cts:annotation" xmlns:cts="http://marklogic.com/cts";>
>>  <cts:annotation qtextref="following-sibling::cts:text"/>
>>  <cts:field>doc-field</cts:field>
>>  <cts:text>foo bar</cts:text>
>> </cts:field-word-query>
>>
>> Best regards,
>> Majid Valipour
>> Scholars Portal
>> _______________________________________________
>> General mailing list
>> [email protected]
>> http://developer.marklogic.com/mailman/listinfo/general
>
> _______________________________________________
> General mailing list
> [email protected]
> http://developer.marklogic.com/mailman/listinfo/general
> _______________________________________________
> General mailing list
> [email protected]
> http://developer.marklogic.com/mailman/listinfo/general
>
_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to