Php – How to exclude searching specified fields using Zend Search (Lucene)

full-text-searchlucenePHPsearchzend-framework

I've built a search index using the PHP Zend Framework Search (based on Lucene). The search is for a buy/sell website.

My search index includes the following fields:

item-id (UnIndexed)
item-title (Text)
item-description (UnStored)
item-tags (Text)
item-price (keyword)
seller-id (UnIndexed)
seller-name (Text)

I want the user to search the index, filtering their search by either only searching for items or searching for sellers by name.

If I do a search using Lucene's default search settings I will be searching all 5 item fields and the seller-name field. This is not what I want to happen. What I would like is when the user makes the search I want them to be required to select from a drop-down menu if they are searching for an item, or for a seller-name.

How can I tell the search query when searching for items to ignore the seller-name field? And when searching for seller-name's how can i tell the search query to not search across any of the item fields? Or is it better to create a separate index for the seller names?

Best Answer

There's currently no way to explicitly not search a field in Lucene's query language, or the Zend_Search_Lucene query constructing API.

However, you can explicitly list which fields you do want to search in a query. An example would be:

seller-name: Joe McBob

If using this approach, you will have to explicitly list which fields you want to query, and what to search in them. So if you also needed to search your item-title field with the same text, you would have to duplicate the above, but with the different field name. An example would be:

seller-name: Joe McBob OR item-title: JoeMcBob

You can, of course, do all this through the query building API that Zend_Search_Lucene provides, as well. The manual has some good examples there.

One thing worth noting here is that, as you've discovered, Zend_Search_Lucene will search ALL fields by default. This is one of the ways in which it differs from Java Lucene. You may, however, set a default field to query, using the setDefaultSearchField static method of the Zend_Search_Lucene class.

Related Topic