PHP Zend Lucene Search Query with multiple criteria fail

745 views Asked by At

Zend Lucene Search Document

Lucene Document
    pk:Keyword
    category_id:Keyword
    title:UnStored
    description:UnStored

This is my string query "java lucene AND +category_id:7". Result here:

Array
(
    [0] => Array
        (
            [pk] => 209
            [category_id] => 7
            [id] => 0
            [score] => 0.40750848701418
        )
   [1] => Array
        (
            [pk] => 225
            [category_id] => 7
            [id] => 3
            [score] => 0.30750848701619
        )

    [2] => Array
        (
            [pk] => 211
            [category_id] => 8 ====>>> WRONG!!! 
            [id] => 2
            [score] => 0.37152213415004
        )

)

Can you do a Query search on the category_id = 7 only?? Thanks in advance.

2

There are 2 answers

1
phapsu On BEST ANSWER

I had solved this problem by using Zend Query Parsing

$strQuery = Zend_Search_Lucene_Search_QueryParser::parse('java lucene');

$cateTerm  = new Zend_Search_Lucene_Index_Term(7 , 'category_id');
$cateQuery = new Zend_Search_Lucene_Search_Query_Term($cateTerm);

$query = new Zend_Search_Lucene_Search_Query_Boolean();
$query->addSubquery($strQuery, true /* required */);
$query->addSubquery($cateQuery, true /* required */);

Results will be only in category_id = 7 :)

1
Ehab Al-Hakawati On

You can remove the AND +category_id:7 from you query, what you want is a filter since +category_id:7 is not needed as a ranked value.

I don't know how to implement it with Zend_Lucene but in solr I used to pass fq parameter, this may give you a hint :)

Filtering is a process that constrains the search space and allows only a subset of documents to be considered for search hits. You can use this feature to implement search-within-search results. Lucene comes with various built-in filters such as BooleanFilter, CachingWrapperFilter, ChainedFilter, DuplicateFilter, PrefixFilter, QueryWrapperFilter, RangeFilter, RemoteCachingWrapperFilter, SpanFilter, etc.(THE NATIVE JAVA VERSION) Filter can be passed to IndexSearcher's search method to filter documents that match the filter criteria.