sjoerd999
sjoerd999

Reputation: 139

MarkLogic: only return exact search results

I'm using the MarkLogic Java API. I want a search for a certain String to return ONLY the documents where exactly this String matches with some key within the document. I explicitly want to search for Strings only and I don't want to specify the keys where this String has to match in advance.

Example1:

    DatabaseClient client = DatabaseClientFactory.newClient(Config.host, Config.port, new DigestAuthContext(Config.user, Config.password));
    QueryManager queryMgr = client.newQueryManager();
    queryMgr.setPageLength(100);

    // create a search definition
    StringQueryDefinition query = queryMgr.newStringDefinition();

    // Search for the term
    query.setCriteria("2017");

    SearchHandle resultsHandle = new SearchHandle();

    // run the search
    queryMgr.search(query, resultsHandle);

The search for "2017" in this example also returns documents in the form of:

{"Example1":{"key1":"bla", "key2":"2017-10-12T15:37:53.204Z"}}

It should ONLY return documents like this:

{"Example1":{"key1":"bla", "key2":"2017"}}

I also tried the following. Example2:

    DatabaseClient client = DatabaseClientFactory.newClient(Config.host, Config.port, new DigestAuthContext(Config.user, Config.password));
    QueryManager queryMgr = client.newQueryManager();
    queryMgr.setPageLength(100);

    // create a search definition
    StructuredQueryBuilder qb = new StructuredQueryBuilder();
    StructuredQueryDefinition query = qb.term( "2017");

    SearchHandle resultsHandle = new SearchHandle();

    // run the search
    queryMgr.search(query, resultsHandle);

And I tried to encapsulate the search term in the two examples above with \"\" like "\"2017\"". Also no luck there.

Upvotes: 0

Views: 116

Answers (1)

Sam Mefford
Sam Mefford

Reputation: 2475

I don't know of any way that doesn't require you to specify the keys where this string has to match. I think the closest you're going to come is creating a field which encapsulates the list of possible element names. It shouldn't be impossible to enumerate them, even if you had to automate the field creation and update it as new content (and new element names) are ingested. Then you could do this:

StructuredQueryBuilder sqb = new StructuredQueryBuilder();
QueryDefinition query = sqb.value(sqb.field("catchall"), "mark");
SearchHandle results = queryMgr.search(query, new SearchHandle());

Also note, this question is a duplicate of MarkLogic Java: Set Query Type to Value in StringQueryDefinition

Upvotes: 1

Related Questions