将 search:search 查询限制为特定元素

constrain search:search query to specific element

我在尝试指定搜索参数以仅搜索文件中的特定 xml 元素时遇到问题。这是我用来搜索的文件:

<file>
  <title>red</title>
  <info>
    <section>blurbs</section>
    <section>words</section>
  </info>
  <info>
    <section>first</section>
    <section>this</section>
  </info>
  <info>
    <section>blue</section>
    <section>green</section>
  </info>
  <info>
    <section>red</section>
    <section>yellow</section>
  </info>
</file>

我正在使用的 search:search 查询是:

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">
    <additional-query>
      <cts:document-query depth="infinity" xmlns:cts="http://marklogic.com/cts">
        <cts:uri>/test_data/test_search.xml</cts:uri>
      </cts:document-query>
    </additional-query>
    <extract-document-data selected="include">
      <extract-path>/file/info</extract-path>
    </extract-document-data>
    <constraint>
      <word>
        <element name="info"/>
      </word>
    </constraint> 
    <search-option>filtered</search-option>
  </options>
let $results := search:search("red", $options)

$results 变量包含:

<search:response snippet-format="snippet" total="1" start="1" page-length="10" selected="include" xmlns:search="http://marklogic.com/appservices/search">
  <search:result index="1" uri="/test_data/test_search.xml" path="fn:doc("/test_data/test_search.xml")" score="8448" confidence="0.4065818" fitness="0.8925228">
    <search:snippet>
      <search:match path="fn:doc("/test_data/test_search.xml")/file">
        <search:highlight>red
        </search:highlight>
      </search:match>
      <search:match path="fn:doc("/test_data/test_search.xml")/file/info[4]">
        <search:highlight>red
        </search:highlight>
      </search:match>
    </search:snippet>
    <search:extracted kind="element">
      <info>
        <section>blurbs
        </section>
        <section>words
        </section>
      </info>
      <info>
        <section>first
        </section>
        <section>this
        </section>
      </info>
      <info>
        <section>blue
        </section>
        <section>green
        </section>
      </info>
      <info>
        <section>red
        </section>
        <section>yellow
        </section>
      </info>
    </search:extracted>
  </search:result>
  <search:qtext>red
  </search:qtext>
  <search:metrics>
    <search:query-resolution-time>PT0.00166S
    </search:query-resolution-time>
    <search:snippet-resolution-time>PT0.000992S
    </search:snippet-resolution-time>
    <search:extract-resolution-time>PT0.00049S
    </search:extract-resolution-time>
    <search:total-time>PT0.003748S
    </search:total-time>
  </search:metrics>
</search:response>

如您所见,标题和信息上显示红色,但我只想搜索 xml 信息元素。我在这里做错了什么?

编辑:我对限制搜索 IE 了解甚少 search:search("title:red") 但是当限制是多个单词时会发生什么?

创建约束时,您应该为其指定一个名称,如:

<constraint name="inf">

这就是可以像 inf:red

那样在查询文本中标记术语的原因

有关详细信息,请参阅:

您还可以使用 search:term 元素为未标记的搜索词指定默认处理方式:

要了解从查询文本生成的查询,将调试或 return-query 选项设置为 true 会很有帮助:

顺便说一下,您可以使用 fn:doc() 检索任何文档并使用 XPath 或 search:snippet() 从文档中提取节点。 search:search() 函数不是为通过 URI 检索文档而设计的。

最后,如果可能,您可能想要修改文档模型。 MarkLogic 可以为文档提供更有用的索引,其中:

  • 文档是细粒度的,重点关注单个实体而不是实体列表
  • 元素名称反映了数据的语义(而不是使用通用元素名称)

希望对您有所帮助,