lucenezend-lucene

Zend Lucene and range search on a field with multiple values


Say my index contains documents with a field called "ages". Example entries for "ages" field:

How would I query this so that I get all the documents whose fields contain ages between 20 and 30? I'm using Zend Lucene.


Solution

  • lmgtfy :-

    $from = new Zend_Search_Lucene_Index_Term(20, 'ages');
    $to   = new Zend_Search_Lucene_Index_Term(30, 'ages');
    $query = new Zend_Search_Lucene_Search_Query_Range(
                 $from, $to, true // inclusive
             );
    $hits  = $index->find($query);
    

    Docs:- http://framework.zend.com/manual/en/zend.search.lucene.query-api.html#zend.search.lucene.queries.range

    Both lucene and solr support multivalued.
    For your case, you did not normalized the data very well.
    Reassign the ages field as multivalued,
    the range query is to match one of the value in multivalued.
    That's mean

    20,29 <-- matched
    20,31 <-- matched
    30,31 <-- does not matched
    

    If you need to match ALL value between 20 to 30,

    20,21,30 <-- matched
    19,21,30 <-- not matched
    

    You can use another field(s),
    like :-

    age_20_30 : store 1 (when all ages are between 20 to 30), else 0
    age_30_40 : store 1 (when all ages are between 30 to 40), else 0
    ... etc
    

    After that, you can change to search on age_20_30:1.
    This is like telling lucene to work on the summary results