zoukankan      html  css  js  c++  java
  • lucene的基本查询及lucene3.0.1API

    lucene 3.0.1 api
    http://lucene.apache.org/core/old_versioned_docs/versions/3_0_1/api/core/overview-summary.html


    package
    com.tianditu.searchDemo; import java.io.File; import org.apache.lucene.analysis.KeywordAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; public class SearchDemo { public static void main(String[] args){ try {
                Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
                Directory directory = FSDirectory.open(new File("D:/indexes/part-00000"));
                IndexSearcher isearcher = new IndexSearcher(directory, true);
                QueryParser parser = new QueryParser(Version.LUCENE_30,
                        "content", analyzer);
                Query query = parser.parse("建管机构");
                
                ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;
                for (int i = 0; i < hits.length; i++) {
                  Document hitDoc = isearcher.doc(hits[i].doc);
                  System.out.println(hitDoc.get("title"));
                  System.out.println(hitDoc.get("url"));
                }
                System.out.println(hits.length);
                isearcher.close();
                directory.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
    } }
    带编辑距离的查询
    //
    ===========建立按字分词的Span为0的顺序Query============ char[] arrChars = strQuery.toCharArray(); int iCount = arrChars.length; char[] arrTerm = new char[1]; ArrayList<SpanQuery> listGroupQuery = new ArrayList<SpanQuery>(); for( int i = 0; i < iCount; i++){ // 去掉空格 if(arrChars[i] == ' ') continue; arrTerm[0] = arrChars[i]; SpanTermQuery termQuery = new SpanTermQuery(new Term(strField, new String(arrTerm))); listGroupQuery.add(termQuery); } SpanNearQuery suggestQuery = new SpanNearQuery(listGroupQuery.toArray(new SpanQuery[0]), 0, true); //==================================================== return suggestQuery;
  • 相关阅读:
    Apache Thrift的简单使用
    ExternalInterface的简单使用方法
    Android各种屏幕分辨率(VGA、HVGA、QVGA、WQVGA、WVGA、FWVGA) 具体解释
    白话经典算法系列之六 高速排序 高速搞定
    HTML学习_01
    Codeforces Round #256 (Div. 2) A. Rewards
    activity
    自己生产签名和数字证书的方法
    Android项目目录结构
    Android程序的安装和打包
  • 原文地址:https://www.cnblogs.com/i80386/p/2937194.html
Copyright © 2011-2022 走看看