在 Zend Lucene 中,如何更改查询搜索的字段?
我正在尝试创建一个“高级搜索”,在其中我可以让用户仅搜索索引的特定字段。为此,我使用布尔查询:
$sq1 = Zend_Search_Lucene_Search_QueryParser::parse($field1); // <- provided by user
$sq2 = Zend_Search_Lucene_Search_QueryParser::parse($field2); // <- provided by user
$query = new Zend_Search_Lucene_Search_Query_Boolean();
$query->addSubquery($sq1, true);
$query->addSubquery($sq2, true);
$index->find($query);
如何指定 sq1 将搜索字段“foo”,而 sq2 将搜索字段“bar”?
我觉得我应该以不同的方式解析查询以获得效果(因为用户可能输入字段名称),但文档仅提到 QueryParser 用于将用户输入查询与 API 查询连接起来。
I am trying to create an "advanced search", where I can let the user search only specific fields of my index. For that, I'm using a boolean query:
$sq1 = Zend_Search_Lucene_Search_QueryParser::parse($field1); // <- provided by user
$sq2 = Zend_Search_Lucene_Search_QueryParser::parse($field2); // <- provided by user
$query = new Zend_Search_Lucene_Search_Query_Boolean();
$query->addSubquery($sq1, true);
$query->addSubquery($sq2, true);
$index->find($query);
How can I specify specify that sq1 will search field 'foo', and sq2 will search field 'bar'?
I feel like I should be parsing the queries differently for the effect (because the user might type in a field name), but the docs only mention the QueryParser for joining user-input queries with API queries.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎最简单的方法就是捏造用户输入:
$field1 和 $field2 应事先去掉括号和冒号以避免“搜索注入”。
It seems the simplest way to do this is just to fudge the user input:
$field1 and $field2 should be stripped of parenthesis and colons beforehand to avoid "search injection".
你想要的是查询构造API: http://www.zendframework.com/manual/en/zend.search.lucene.query-api.html#zend.search.lucene.queries.multiterm-query
不过,我建议您完全放弃 Zend_Search_Lucene。 Java 的实现很精彩,但是 PHP 的实现却很糟糕。关于您尝试执行的操作,它的行为非常错误,请参阅问题 1508748。它也非常非常慢。
What you want is the query construction API: http://www.zendframework.com/manual/en/zend.search.lucene.query-api.html#zend.search.lucene.queries.multiterm-query
However, I'd recommend that you drop Zend_Search_Lucene altogether. The Java implementation is wonderful, but the PHP implementation is very bad. Regarding what you are trying to do it behaves very buggy, see question 1508748. It's also very, very slow.