Lucene 4.* QueryParser Range Searcher (範圍查詢)

來源:互聯網
上載者:User

標籤:

 

 由於Lucene4.9.1的QueryParser支援類似Solr的範圍檢索( FL:[ 111 TO 222] ) 但是實際中會出現不準確的現象。

這是由於QueryParser 把範圍查詢當做String類型。而無論索引時使用LongField 和 TextField 都會出現不準確問題。

解決辦法是手動將範圍檢索改成long類型或int類型。

 

1 自訂QueryParser 。

public class RangeQueryParser extends QueryParser {    public RangeQueryParser(Version matchVersion, String f, Analyzer a) {        super(matchVersion, f, a);    }    @Override    protected Query getRangeQuery(String field, String part1, String part2, boolean arg3, boolean arg4) throws ParseException {        try {            long num1 = Long.parseLong(part1);            long num2 = Long.parseLong(part2);            return NumericRangeQuery.newLongRange(field, num1, num2, true, true);        } catch (NumberFormatException e) {            throw new ParseException(e.getMessage());        }    }}

 

2 使用方法:

 

package org.bidlink.index;import java.io.File;import java.text.SimpleDateFormat;import java.util.Date;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field.Store;import org.apache.lucene.document.LongField;import org.apache.lucene.document.TextField;import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.queryparser.classic.QueryParser;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.TopDocs;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.util.Version;import org.wltea.analyzer.lucene.IKAnalyzer;import com.bidlink.general.index.RangeQueryParser;import com.bidlink.general.index.SearcherCallBack;import com.bidlink.general.index.WordBankConst;public class TestIndex {    public void index() throws Exception {        Directory d = FSDirectory.open(new File("Index"));        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_4_9, new IKAnalyzer());        IndexWriter iw = new IndexWriter(d, iwc);        Document doc = new Document();        doc.add(new TextField("WORD", "測試北京市第102個", Store.YES));        doc.add(new LongField("ENDDATE", 1234556L, Store.YES));        iw.addDocument(doc);        iw.commit();        iw.close();    }    public void search(String fl, String word, SearcherCallBack callBack) throws Exception {        Directory d = FSDirectory.open(new File("Index"));        IndexReader r = DirectoryReader.open(d);        IndexSearcher s = new IndexSearcher(r);        callBack.maxDocs = s.getIndexReader().maxDoc();        callBack.numDocs = s.getIndexReader().numDocs();        QueryParser qp = new RangeQueryParser(Version.LUCENE_4_9, fl, new IKAnalyzer());        qp.setDefaultOperator(QueryParser.Operator.AND);        try {            TopDocs tdocs = s.search(qp.parse(word), 1000);            callBack.numFound = tdocs.totalHits;            ScoreDoc scoreDocs[] = tdocs.scoreDocs;            for (ScoreDoc sDoc : scoreDocs) {                int docNumber = sDoc.doc;                Document doc = s.doc(docNumber);                callBack.process(doc);            }        } catch (Exception e) {            e.printStackTrace();        }    }    public static void main(String[] args) throws Exception {        SearcherCallBack s = new SearcherCallBack() {            @Override            public void process(Document doc) {                // System.out.println(doc.getField(WordBankConst.ID).stringValue());                System.out.println(doc.getField(WordBankConst.WORD).stringValue());                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");                String date = sdf.format(new Date(Long.parseLong(doc.getField(WordBankConst.ENDDATE).stringValue())));                System.out.println(Long.parseLong(doc.getField(WordBankConst.ENDDATE).stringValue()) + "   " + date);            }        };        TestIndex index = new TestIndex();        // index.index();        index.search("", "WORD:地鐵  AND ENDDATE:[1111 TO 222222222222 ]", s);        System.out.println("Doc numFound :  " + s.numFound + "  Max Doc num :" + s.maxDocs);    }}

 

Lucene 4.* QueryParser Range Searcher (範圍查詢)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.