lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Uwe Schindler" <...@thetaphi.de>
Subject RE: Problem with numeric range query syntax in lucene 4.4.0
Date Thu, 20 Mar 2014 21:42:44 GMT
Hi,

Lucene is schema-less. Because of that QueryParser cannot handle numeric fields out of the
box, because it cannot know what fields are numeric. Because of this it just creates a TermRangeQuery
which will never hit any documents of a field indexed as LongField. You can directly use NumericRangeQuery
to create the range query (recommended). Alternatively you can subclass QueryParser and override
the getRangeQuery protected methods to create the correct query type (NumericRangeQuery) based
on the field name.

Uwe

-----
Uwe Schindler
H.-H.-Meier-Allee 63, D-28213 Bremen
http://www.thetaphi.de
eMail: uwe@thetaphi.de


> -----Original Message-----
> From: Matthew Petersen [mailto:mdpete4@gmail.com]
> Sent: Thursday, March 20, 2014 9:44 PM
> To: java-user@lucene.apache.org
> Subject: Problem with numeric range query syntax in lucene 4.4.0
> 
> Hi
> 
> I'm trying to submit a lucene query string to my index to return a data based
> on a numeric range.  I'm using the syntax provided in the Query Parser
> Syntax document but the results I get indicate that the query is not working
> correctly.  Below is a unit test that proves that the range query does not
> work, at least in this configuration.
> 
> @Test
>     public void test_lucene_numeric_range_query() throws IOException,
> ParseException {
>         Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
>         IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44,
> analyzer);
>         Directory directory = new RAMDirectory();
>         IndexWriter writer = new IndexWriter(directory, config);
> 
>         Field longField1 = new LongField("longField1", 0L, Field.Store.YES);
>         Field longField2 = new LongField("longField2", 0L, Field.Store.YES);
> 
>         for (long i = 1; i < 101; i++)
>         {
>             Document doc = new Document();
>             longField1.setLongValue(i);
>             longField2.setLongValue(i);
> 
>             doc.add(longField1);
>             doc.add(longField2);
> 
>             writer.addDocument(doc);
>         }
> 
>         writer.commit();
>         IndexReader reader = DirectoryReader.open(writer, false);
>         IndexSearcher searcher = new IndexSearcher(reader);
> 
>         Query query = new QueryParser(Version.LUCENE_44, "",
> analyzer).parse("longField1:[51 TO 75]");
> 
>         TopDocs docs = searcher.search(query, 100);
> 
>         Assert.assertEquals(docs.totalHits, 25);
> 
>     }
> 
> 
> Is there something wrong with the query I'm submitting, or the way I'm
> setting up the searcher, or something else?  Or does submitting a query in
> this way for a numeric field not work?
> 
> Thanks in advance,
> Matt


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message