I want to search document properties of the documents. I have only documents loaded in Marklogic and no xml files is there. I have turned off content processing. Now I want to search for a metadata (present in xdmp:document-properties(uri)
)
I have the following properties in a document:-
<?xml version="1.0" encoding="UTF-8"?>
<prop:properties xmlns:prop="http://marklogic.com/xdmp/property">
<uploaded>true</uploaded>
<OntologyResourceTypeValue>DOCUMENT</OntologyResourceTypeValue>
<content-type>application/pdf</content-type>
<filter-capabilities>text subfiles HD-HTML</filter-capabilities>
<CreationDate>2002/12/05 09:44:29Z</CreationDate>
<ModDate>2002/12/05 12:02:27+02'00'</ModDate>
<Producer>Acrobat Distiller 5.0 (Windows)</Producer>
<Author>Administrator</Author>
<Creator>PScript5.dll Version 5.2</Creator>
</prop:properties>
Now I want to search for Author only not other properties. If I am using search:search("Administrator")
then it is looking for this word in the entire document. But, I want to search only for the Author tag in document properties. Similarly I want to search in other properties also.
I have also tried this:-
let $options := <options xmlns="http://marklogic.com/appservices/search">
<constraint name="author">
<properties name="prop:Author"/>
</constraint>
</options>
let $results := search:search("author:Administrator", $options, 1, 10)
return
$results
But, this does not work. Please help.
The issue with a properties constraint is, that it only changes the fragment scope, and doesn't accept a name attribute to constrain the search to just one property. If you add <return-query>true</return-query>
you would see what the resulting query would be.
There are a few options though..
First option is to use <fragment-scope>properties</fragment-scope>
. You can use that at top-level to apply it to all search constraints, and also per constraint to influence particular constraints only. It is a relatively easy way to force search queries to run over properties fragments, instead of over document fragments. The down-side is though that it will not influence search matches, aka snippets.
To influence the snippets, you are better off doing what @mblakele suggests, and use searchable-expression: <searchable-expression>xdmp:document-properties()</searchable-expression>
. That will actually influence both snippets, and search queries, so using that will cause you to get search snippets, and have the queries run over properties fragments. The author constraint still doesn't limit to your Author property though.
Once searching runs over properties fragments, limiting your search to a particular property is actually pretty straight-forwards. It is just an element as any other. Use an element word, value or range constraint to make that work.
Below some code to illustrate the above:
xquery version "1.0-ml";
import module namespace search = "http://marklogic.com/appservices/search"
at "/MarkLogic/appservices/search/search.xqy";
(: original approach :)
let $options1 :=
<options xmlns="http://marklogic.com/appservices/search">
<constraint name="author">
<properties name="prop:Author"/>
</constraint>
<return-query>true</return-query>
</options>
let $results1 := search:search("author:Administrator", $options1, 1, 1)
(: using fragment-scope :)
let $options2 :=
<options xmlns="http://marklogic.com/appservices/search">
<fragment-scope>properties</fragment-scope>
<constraint name="author">
<properties name="prop:Author"/>
</constraint>
<return-query>true</return-query>
</options>
let $results2 := search:search("author:Administrator", $options2, 1, 1)
(: using searchable-expression :)
let $options3 :=
<options xmlns="http://marklogic.com/appservices/search">
<searchable-expression>xdmp:document-properties()</searchable-expression>
<constraint name="author">
<properties name="prop:Author"/>
</constraint>
<return-query>true</return-query>
</options>
let $results3 := search:search("author:Administrator", $options3, 1, 1)
(: using searchable-expression with an element word constraint :)
let $options4 :=
<options xmlns="http://marklogic.com/appservices/search">
<searchable-expression>xdmp:document-properties()</searchable-expression>
<constraint name="author">
<word>
<element name="Author" ns="http://marklogic.com/xdmp/property"/>
</word>
</constraint>
<return-query>true</return-query>
</options>
let $results4 := search:search("author:Administrator", $options4, 1, 1)
return (
$results1,
$results2,
$results3,
$results4
)
The fourth example should give you the results you were looking for.
HTH!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With