lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Lewis John Mcgibbney <lewis.mcgibb...@gmail.com>
Subject Upgrading Lucene 2.0.0 TermQuery to 4.2 QueryParser
Date Thu, 04 Apr 2013 01:39:03 GMT
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*

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message