views:

164

answers:

2

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.

A: 

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.

Matthijs Bierman
The problem is that the API doesn't explain how to do this. How do I get user input and turn it into a multiterm query without tokenizing it manually?I realize a PHP search engine is not a great idea, but I'm looking for ease of implementation right now and I'll migrate if it becomes a problem later.
Ilia Jerebtsov
In that case it seems you have no choice but to use the solution provided by Ilia Jerebtsov. Again, be aware that the PHP implementation is very slow. You still have a choice to go with another search framework. I strongly suggest you do so.
Matthijs Bierman
A: 

It seems the simplest way to do this is just to fudge the user input:

$sq1 = Zend_Search_Lucene_Search_QueryParser::parse("foo:($field1)");
$sq2 = Zend_Search_Lucene_Search_QueryParser::parse("bar:($field2)");

$field1 and $field2 should be stripped of parenthesis and colons beforehand to avoid "search injection".

Ilia Jerebtsov