问题
I'm using the MarkLogic Java API. I'm looking for a way to set the Query Type of a StringQueryDefinition. The type should be "Value", as described in: https://docs.marklogic.com/guide/search-dev/searchdev#id_34905.
Value: Match an entire literal value, such as a string or number, in a specific JSON property or XML element. By default, value queries use exact match semantics. For example, a search for 'mark' will not match 'Mark Twain'.
I need this because I want my text searches to match only the exact search terms.
Example: the search for 'mark' should ONLY match with documents containing exactly the term 'mark' attached to some key. So, {"anExample1": {"key1": "mark", "key2":"someotheralue"}}
is a valid match. But {"anExample2": {"key1": "mark twain", "key2":"someotheralue"}}
is not a valid match.
For both examples, I'd like to get the URI and location within the document returned. Hence, the search for "mark"
should return uri/anExample1
and anExample1/key1
.
回答1:
Try adding double-quotes around your string query queryMgr.newStringDefinition().withCriteria("\"one two three\"");
StringQueryDefinition queries follow the rules documented in The Default String Query Grammar in the Search Guide.
回答2:
To search for "key1" = "mark" you'd do the following:
StructuredQueryBuilder sqb = new StructuredQueryBuilder();
QueryDefinition query = sqb.value(sqb.element("key1"), "mark");
SearchHandle results = queryMgr.search(query, new SearchHandle());
From the results you can get the uri:
results.getMatchResults()[i].getUri()
And the match locations:
results.getMatchResults()[i].getMatchLocations()[j].getPath()
来源:https://stackoverflow.com/questions/46753747/marklogic-java-set-query-type-to-value-in-stringquerydefinition