【问题标题】:constrain search:search query to specific element约束搜索:搜索查询到特定元素
【发布时间】:2017-05-10 04:59:48
【问题描述】:

我在尝试指定搜索参数以仅搜索我的文件中的特定 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>

我使用的搜索:搜索查询是:

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") 有一点了解,但是当约束是多个单词时会发生什么?

【问题讨论】:

    标签: xml xquery marklogic


    【解决方案1】:

    当创建一个约束时,你应该给它一个名字,如:

    &lt;constraint name="inf"&gt;

    这使得在查询文本中标记术语成为可能,就像在inf:red中一样

    更多详情见:

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

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

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

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

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

    希望对您有所帮助,

    【讨论】:

      猜你喜欢
      • 2013-09-03
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      • 2021-06-29
      • 2020-03-14
      • 2013-12-02
      • 1970-01-01
      相关资源
      最近更新 更多