• 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;
  • 相关阅读:
    并发编程(2)-进程、并发和并行讲解
    并发编程(5)-管道、数据共享、进程池
    并发编程(4)-进程中的锁、信号量、 事件和队列
    人工智能及数学运算的基础方法
    并发编程(3)-进程模块
    判断一个数是否是水仙花数
    js中隐式类型转换测试
    webpack使用webpack-dev-middleware进行热重载
    网页打包安卓APP流程
    「postgres」查看数据库连接数
  • 原文地址:https://www.cnblogs.com/i80386/p/2937194.html
Copyright © 2020-2023  润新知