Ram Rajamony
Ram Rajamony

Reputation: 1723

Emulating wildcard searches using Lucene-based search on Cloudant

Lucene does not permit use of a * or ? symbol as the first character of a search when going through the query parser. Although lucene permits the use of wildcards at the start for other implementations such as lucene.net, this query parser quirk also flows into Cloudant's Lucene-based search.

Lets say we want to emulate: q=foo:*

Can this be specified as: q=foo:([\u0000 TO \uffff] OR [-Infinity TO Infinity])

and the negation as q=*:* AND NOT foo:([\u0000 TO \uffff] OR [-Infinity TO Infinity])

Upvotes: 1

Views: 285

Answers (1)

Will Holley
Will Holley

Reputation: 1765

One solution is to add an index which names the included field names. For example:

function(doc) {
   var included = [];
   if(doc.foo) {
       index("foo", doc.foo);
       included.push("foo");
   }

   if(doc.bar) {
       index("bar", doc.bar);
       included.push("bar");
   }

   if(included.length > 0) {
       index("has", included.join(" "));
   }
}

You could then use

?q=has:foo 

to find all docs with a foo field.

Upvotes: 1

Related Questions