You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by Matthew Petersen <md...@gmail.com> on 2014/03/20 21:43:38 UTC

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

Re: Problem with numeric range query syntax in lucene 4.4.0

Posted by Matthew Petersen <md...@gmail.com>.
Thanks for the response.  I had seen references to this explanation in
other areas but for older versions and was hoping it changed in 4.4.  I
guess since it's schema-less it really can't be fixed for the masses and
must be fixed through customization.

Thanks again,
Matt


On Thu, Mar 20, 2014 at 3:42 PM, Uwe Schindler <uw...@thetaphi.de> wrote:

> 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
>
>

RE: Problem with numeric range query syntax in lucene 4.4.0

Posted by Uwe Schindler <uw...@thetaphi.de>.
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