Search code examples
marklogicmarklogic-8

Marklogic search grammar issue


I was under the impression that when a search phrase is in double quotes, it will do the exact search. But I am getting partial matches as well (even though the score was low). I was expecting that it should do exact match. Following is my sample code.. am I missing something

xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";
import module namespace search = "http://marklogic.com/appservices/search" at "/MarkLogic/appservices/search/search.xqy";
import module namespace functx = "http://www.functx.com"  at "/MarkLogic/functx/functx-1.0-doc-2007-01.xqy";

let $q := '(“protein degradation”) AND ((context:PCS)) AND (sort:date_desc)'

let $options := 
  <options xmlns="http://marklogic.com/appservices/search">
    <additional-query>
        <cts:collection-query xmlns:cts="http://marklogic.com/cts">
            <cts:uri>http://XXXXX/type/envelope</cts:uri>
        </cts:collection-query>
    </additional-query>
    <operator name="sort">
        <state name="date_desc">
            <sort-order type="xs:dateTime" direction="descending">
                <field name="upload_date"/>
            </sort-order>
        </state>
        <state name="date_asc">
            <sort-order type="xs:dateTime" direction="ascending">
                <field name="upload_date"/>
            </sort-order>
        </state>
    </operator>
    <constraint name="context">
        <range type="xs:string" facet="true">
            <element name="context" ns="http://XXXXX/metadata"/>
            <facet-option>frequency-order</facet-option>
            <facet-option>descending</facet-option>
        </range>
    </constraint>
    <constraint name="type">
        <range type="xs:string" facet="true">
            <element name="type" ns="http://XXXXX/metadata"/>
            <facet-option>frequency-order</facet-option>
            <facet-option>descending</facet-option>
        </range>
    </constraint>
    <term>
        <term-option>case-insensitive</term-option>
        <term-option>punctuation-insensitive</term-option>
        <term-option>whitespace-insensitive</term-option>
        <term-option>wildcarded</term-option>
    </term>
     <search-option>unfiltered</search-option>
</options>

let $start := 1
let $page-length :=1

let $result := search:search($q, $options, $start, $page-length) 
return $result

Following is what I got.. I am very confused.. I am not why the following result came as a hit

<search:result index="1" uri="/documents/PCS/0ba1e4a0190b77a3962e1218c3c1a7f4cb233ddf.xml" path="fn:doc("/documents/PCS/0ba1e4a0190b77a3962e1218c3c1a7f4cb233ddf.xml")" score="58624" confidence="0.329381" fitness="0.5856407">
  <search:snippet>
    <search:match path="fn:doc("/documents/PCS/0ba1e4a0190b77a3962e1218c3c1a7f4cb233ddf.xml")/*:document-envelope/*:metadata/*:context">
      <search:highlight>PCS</search:highlight>
    </search:match>
    <search:match path="fn:doc("/documents/PCS/0ba1e4a0190b77a3962e1218c3c1a7f4cb233ddf.xml")/*:document-envelope/*:extractedText/*:html/*:body/*:p[1]">
Analysis of the Safety Risks Associated with Hydrazine as a <search:highlight>Degradation</search:highlight> Product in LCIG RD12714 ra-rd12714-hydrazine</search:match>
    <search:match path="fn:doc("/documents/PCS/0ba1e4a0190b77a3962e1218c3c1a7f4cb233ddf.xml")/*:document-envelope/*:extractedText/*:html/*:body/*:p[9]">...of the Safety Risks Associated with Hydrazine as a <search:highlight>Degradation</search:highlight> Product in...</search:match>
  </search:snippet>
</search:result>

If we notice in the result above it match <search:highlight>Degradation</search:highlight> ... Why does it doing partial matches when we are trying to do exact search ?

----- Added the search:parse output ------

<cts:and-query xmlns:cts="http://marklogic.com/cts" xmlns:search="http://marklogic.com/appservices/search">
  <cts:word-query>
    <cts:text xml:lang="en">“protein</cts:text>
    <cts:option>case-insensitive</cts:option>
    <cts:option>punctuation-insensitive</cts:option>
    <cts:option>whitespace-insensitive</cts:option>
    <cts:option>wildcarded</cts:option>
  </cts:word-query>
  <cts:word-query>
    <cts:text xml:lang="en">degradation”</cts:text>
    <cts:option>case-insensitive</cts:option>
    <cts:option>punctuation-insensitive</cts:option>
    <cts:option>whitespace-insensitive</cts:option>
    <cts:option>wildcarded</cts:option>
  </cts:word-query>
  <cts:element-range-query operator="=">
    <cts:element xmlns:_1="http://XXXXX/metadata">_1:context</cts:element>
    <cts:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">PCS</cts:value>
    <cts:option>collation=http://marklogic.com/collation/</cts:option>
  </cts:element-range-query>
  <cts:annotation operator-ref="sort" state-ref="date_desc">
  </cts:annotation>
</cts:and-query>

Solution

  • I think the problem is the fancy quotes:

    import module namespace search = "http://marklogic.com/appservices/search" at "/MarkLogic/appservices/search/search.xqy";
    
    search:parse('"protein degradation"')
    

    gives:

    <cts:word-query xmlns:cts="http://marklogic.com/cts">
      <cts:text xml:lang="en">protein degradation</cts:text>
    </cts:word-query>
    

    while:

    import module namespace search = "http://marklogic.com/appservices/search" at "/MarkLogic/appservices/search/search.xqy";
    
    search:parse('“protein degradation”')
    

    gives:

    <cts:and-query xmlns:cts="http://marklogic.com/cts">
      <cts:word-query>
        <cts:text xml:lang="en">“protein</cts:text>
      </cts:word-query>
      <cts:word-query>
        <cts:text xml:lang="en">degradation”</cts:text>
      </cts:word-query>
    </cts:and-query>