Alex J
Alex J

Reputation: 10205

In Zend Lucene, how can I change the field which a query searches?

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.

Upvotes: 2

Views: 1005

Answers (2)

Alex J
Alex J

Reputation: 10205

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".

Upvotes: 1

Matthijs Bierman
Matthijs Bierman

Reputation: 1757

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.

Upvotes: 0

Related Questions