sjoerd999
sjoerd999

Reputation: 139

MarkLogic Java: Set Query Type to Value in StringQueryDefinition

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.

Upvotes: 1

Views: 139

Answers (2)

Sam Mefford
Sam Mefford

Reputation: 2475

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()

Upvotes: 1

Sam Mefford
Sam Mefford

Reputation: 2475

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.

Upvotes: 1

Related Questions