使用基于Lucene的Cloudant搜索模拟通配符搜索

时间:2014-06-10 17:25:18

标签: search lucene cloudant

Lucene不允许使用*或?通过查询解析器时符号as the first character of a search。虽然lucene允许在开始时使用通配符用于其他实现such as lucene.net,但此查询解析器的怪癖也会流入Cloudant's Lucene-based search

让我们说我们想模仿:q=foo:*

可以指定为:q=foo:([\u0000 TO \uffff] OR [-Infinity TO Infinity])

和否定 q=*:* AND NOT foo:([\u0000 TO \uffff] OR [-Infinity TO Infinity])

1 个答案:

答案 0 :(得分:1)

一种解决方案是添加一个索引,该索引命名包含的字段名称。例如:

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(" "));
   }
}

然后你可以使用

?q=has:foo 

查找带有foo字段的所有文档。

相关问题