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 Lewis John Mcgibbney <le...@gmail.com> on 2013/04/04 03:39:03 UTC

Upgrading Lucene 2.0.0 TermQuery to 4.2 QueryParser

Hi,
I'm currently embarking upon a non trivial upgrade of some legacy 2.0.0
code and encounter the following

        IndexSearcher searcher = null;
        try {
            searcher = new IndexSearcher(indexFilePath);
            Term productIdTerm = new Term("product_id", productId);
            org.apache.lucene.search.Query query = new TermQuery(productIdTerm);
            Hits hits = searcher.search(query);

            // should be exactly 1 hit
            if (hits.length() == 0) {
            	throw new CatalogException("Product: [" + productId + "]
NOT found in the catalog!");
            } else if (hits.length() > 1) {
                throw new CatalogException("Product: [" + productId+
"] is not unique in the catalog!");
            }

            Document productDoc = hits.doc(0);

So far I've got here

        try {
            DirectoryReader reader = DirectoryReader.open(indexFilePath);
            IndexSearcher searcher = new IndexSearcher(reader);
            org.apache.lucene.search.Query query = new QueryParser
                (Version.LUCENE_42, productId, new
StandardAnalyzer(Version.LUCENE_42)).parse(productIdTerm.toString());
            TopDocs topd = searcher.search(query, 10);
            ScoreDoc[] docs = topd.scoreDocs;
            // should be exactly 1 hit
            if (topd.totalHits == 0) {
                throw new CatalogException("Product: [" + productId + "]
NOT found in the catalog!");
            } else if (topd.totalHits > 1) {
                throw new CatalogException("Product: [" + productId+ "] is
not unique in the catalog!");
            }
            Document productDoc = searcher.doc(docs[0].doc);

So I hope this is fine. Please someone call me out where I am going wrong
here.

The issue I have is what if I wish to to a BooleanQuery like

        try {
            searcher = new IndexSearcher(indexFilePath);

            // construct a Boolean query here
            BooleanQuery booleanQuery = new BooleanQuery();
            TermQuery tq = new TermQuery(new Term("myfield", "myvalue"));
            booleanQuery.add(tq, BooleanClause.Occur.MUST);

            Sort sort = new Sort(new SortField("CAS.ProductReceivedTime",
                    SortField.STRING, true));

How do I represent this in the new 4.x syntax?

Thank you for taking the time to read through this code. I will not be
surprised if it is not correct.
Thank you
Lewis

-- 
*Lewis*

Re: Upgrading Lucene 2.0.0 TermQuery to 4.2 QueryParser

Posted by Lewis John Mcgibbney <le...@gmail.com>.
Hi Uwe,
This is dynamite and makes much more sense now when Iook back at the
numerous occasions where I've been upgrading this.
Plenty of work tomorrow!
Your feedback is great, thank you
Lewis

On Wednesday, April 3, 2013, Uwe Schindler <uw...@thetaphi.de> wrote:
> Hi,
>
> You can use TermQuery and BooleanQuery in Lucene 4.x in exactly the same
way like in 2.0. No need to use QueryParser (and it's not a good idea to
use QP for non-analyzed fields like product IDs). TermQuery is the way to
go.
>
> -----
> Uwe Schindler
> H.-H.-Meier-Allee 63, D-28213 Bremen
> http://www.thetaphi.de
> eMail: uwe@thetaphi.de
>
>
>> -----Original Message-----
>> From: Lewis John Mcgibbney [mailto:lewis.mcgibbney@gmail.com]
>> Sent: Thursday, April 04, 2013 3:39 AM
>> To: java-user@lucene.apache.org
>> Subject: Upgrading Lucene 2.0.0 TermQuery to 4.2 QueryParser
>>
>> Hi,
>> I'm currently embarking upon a non trivial upgrade of some legacy 2.0.0
code
>> and encounter the following
>>
>>         IndexSearcher searcher = null;
>>         try {
>>             searcher = new IndexSearcher(indexFilePath);
>>             Term productIdTerm = new Term("product_id", productId);
>>             org.apache.lucene.search.Query query = new
>> TermQuery(productIdTerm);
>>             Hits hits = searcher.search(query);
>>
>>             // should be exactly 1 hit
>>             if (hits.length() == 0) {
>>               throw new CatalogException("Product: [" + productId + "]
NOT found
>> in the catalog!");
>>             } else if (hits.length() > 1) {
>>                 throw new CatalogException("Product: [" + productId+ "]
is not
>> unique in the catalog!");
>>             }
>>
>>             Document productDoc = hits.doc(0);
>>
>> So far I've got here
>>
>>         try {
>>             DirectoryReader reader = DirectoryReader.open(indexFilePath);
>>             IndexSearcher searcher = new IndexSearcher(reader);
>>             org.apache.lucene.search.Query query = new QueryParser
>>                 (Version.LUCENE_42, productId, new
>> StandardAnalyzer(Version.LUCENE_42)).parse(productIdTerm.toString());
>>             TopDocs topd = searcher.search(query, 10);
>>             ScoreDoc[] docs = topd.scoreDocs;
>>             // should be exactly 1 hit
>>             if (topd.totalHits == 0) {
>>                 throw new CatalogException("Product: [" + productId + "]
NOT found
>> in the catalog!");
>>             } else if (topd.totalHits > 1) {
>>                 throw new CatalogException("Product: [" + productId+ "]
is not
>> unique in the catalog!");
>>             }
>>             Document productDoc = searcher.doc(docs[0].doc);
>>
>> So I hope this is fine. Please someone call me out where I am going wrong
>> here.
>>
>> The issue I have is what if I wish to to a BooleanQuery like
>>
>>         try {
>>             searcher = new IndexSearcher(indexFilePath);
>>
>>             // construct a Boolean query here
>>             BooleanQuery booleanQuery = new BooleanQuery();
>>             TermQuery tq = new TermQuery(new Term("myfield", "myvalue"));
>>             booleanQuery.add(tq, BooleanClause.Occur.MUST);
>>
>>             Sort sort = new Sort(new SortField("CAS.ProductReceivedTime",
>>                     SortField.STRING, true));
>>
>> How do I represent this in the new 4.x syntax?
>>
>> Thank you for taking the time to read through this code. I will not be
>> surprised if it is not correct.
>> Thank you
>> Lewis
>>
>> --
>> *Lewis*
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>

-- 
*Lewis*

RE: Upgrading Lucene 2.0.0 TermQuery to 4.2 QueryParser

Posted by Uwe Schindler <uw...@thetaphi.de>.
Hi,

You can use TermQuery and BooleanQuery in Lucene 4.x in exactly the same way like in 2.0. No need to use QueryParser (and it's not a good idea to use QP for non-analyzed fields like product IDs). TermQuery is the way to go.

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


> -----Original Message-----
> From: Lewis John Mcgibbney [mailto:lewis.mcgibbney@gmail.com]
> Sent: Thursday, April 04, 2013 3:39 AM
> To: java-user@lucene.apache.org
> Subject: Upgrading Lucene 2.0.0 TermQuery to 4.2 QueryParser
> 
> Hi,
> I'm currently embarking upon a non trivial upgrade of some legacy 2.0.0 code
> and encounter the following
> 
>         IndexSearcher searcher = null;
>         try {
>             searcher = new IndexSearcher(indexFilePath);
>             Term productIdTerm = new Term("product_id", productId);
>             org.apache.lucene.search.Query query = new
> TermQuery(productIdTerm);
>             Hits hits = searcher.search(query);
> 
>             // should be exactly 1 hit
>             if (hits.length() == 0) {
>             	throw new CatalogException("Product: [" + productId + "] NOT found
> in the catalog!");
>             } else if (hits.length() > 1) {
>                 throw new CatalogException("Product: [" + productId+ "] is not
> unique in the catalog!");
>             }
> 
>             Document productDoc = hits.doc(0);
> 
> So far I've got here
> 
>         try {
>             DirectoryReader reader = DirectoryReader.open(indexFilePath);
>             IndexSearcher searcher = new IndexSearcher(reader);
>             org.apache.lucene.search.Query query = new QueryParser
>                 (Version.LUCENE_42, productId, new
> StandardAnalyzer(Version.LUCENE_42)).parse(productIdTerm.toString());
>             TopDocs topd = searcher.search(query, 10);
>             ScoreDoc[] docs = topd.scoreDocs;
>             // should be exactly 1 hit
>             if (topd.totalHits == 0) {
>                 throw new CatalogException("Product: [" + productId + "] NOT found
> in the catalog!");
>             } else if (topd.totalHits > 1) {
>                 throw new CatalogException("Product: [" + productId+ "] is not
> unique in the catalog!");
>             }
>             Document productDoc = searcher.doc(docs[0].doc);
> 
> So I hope this is fine. Please someone call me out where I am going wrong
> here.
> 
> The issue I have is what if I wish to to a BooleanQuery like
> 
>         try {
>             searcher = new IndexSearcher(indexFilePath);
> 
>             // construct a Boolean query here
>             BooleanQuery booleanQuery = new BooleanQuery();
>             TermQuery tq = new TermQuery(new Term("myfield", "myvalue"));
>             booleanQuery.add(tq, BooleanClause.Occur.MUST);
> 
>             Sort sort = new Sort(new SortField("CAS.ProductReceivedTime",
>                     SortField.STRING, true));
> 
> How do I represent this in the new 4.x syntax?
> 
> Thank you for taking the time to read through this code. I will not be
> surprised if it is not correct.
> Thank you
> Lewis
> 
> --
> *Lewis*


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