0

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

  • 25
  • 24, 28
  • 25, 31

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.

StackOverflowNewbie
  • 39,403
  • 111
  • 277
  • 441

1 Answers1

1

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

ajreal
  • 46,720
  • 11
  • 89
  • 119
  • This will work despite the "ages" field containing 1 or more values? – StackOverflowNewbie Dec 12 '11 at 04:01
  • what is multivalued? Is there a way to put multiple values in a field and let Lucene know how to treat them? If so, can I search for something like this: 1.) all ages values are within 20-30, 2.) at least one ages value is within 20-30, etc.? – StackOverflowNewbie Dec 12 '11 at 04:15
  • multivalued is a field that can store multiple values. an example will be tags. in order to make it work, you have to change the solr schema, http://wiki.apache.org/solr/SchemaXml#Data_Types . The biggest drawback is you cannot sort on a multivalued field. Your question 1 is not possible (or require workaround). 2. is possible, it always search at least one value – ajreal Dec 12 '11 at 04:27
  • I'm not using Solr. #1 is the problem I am trying to solve. #2 is basic fulltext search, I guess. What is the workaround for #1? – StackOverflowNewbie Dec 12 '11 at 08:16
  • Which is create a separate field (refer to the updated answer) – ajreal Dec 14 '11 at 02:54
  • The explanation has confused me even more. Say my documents represent multiple profiles and each profile has an age associated with it. I need the ability to search within the age field somehow -- keeping in mind that there needs to be more than one age value in the field. – StackOverflowNewbie Dec 14 '11 at 11:56
  • Are you using multiValued? I guess you don't.Are you store the age value as CSV in a single field? – ajreal Dec 14 '11 at 11:58
  • If I store the age values as CSV in a single field, how do I do range search? – StackOverflowNewbie Dec 15 '11 at 02:58
  • You can't. You refer back to the answer. If you want to do a either search, then use multiValued (which is already a OR search). If you want to do a ALL MATCH search, then you use another field, which should set to 1 IF all ages are between 20 and 30. – ajreal Dec 15 '11 at 04:22
  • @ajreal I don't think there is multivalue field support for Zend Lucene Search – developarvin Dec 15 '11 at 06:30
  • Search for multivalue isjust like a normal query search. For index (input), Solr PHP already support this - http://www.php.net/manual/en/solrdocument.addfield.php (loop the array, and call the above method for each element). I pretty sure is compatible in zend lucene. – ajreal Dec 15 '11 at 06:45