1
votes

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.

2
Did you try anything? I'm not clear under what scenario you could search for "one two three" and have it match your second example document. - Sam Mefford
You're right. My example was a bad one, sorry for that. Edited. - sjoerd999
Make sure to also take a look at StructuredQueryBuilder and SearchHandle. Those are probably going to be a part of your answer. Have you read through the Java Guide for search? docs.marklogic.com/guide/java/searches - Sam Mefford
I did. No luck unfortunately. I don't want to pre-specify on what key(s) the search value should match. As a return from the search I only want documents containing the exact value attached to some key within the document. - sjoerd999
What would be the command if I do pre-specify the key? So in the example, I would search for "key1" == "mark". Not very elegant but we could attach an extra key (i.e. "extrakey") to all the values for all documents and then search for "extrakey" == "searcedValue". - sjoerd999

2 Answers

1
votes

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.

1
votes

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