zoukankan      html  css  js  c++  java
  • lucene的丰富的各种查询

    lucene支持十分丰富的查询,这里列写其中一些比较常用的查询的用法。
    term查询、queryParser查询 ,booleanQuery
    package search;

    import org.apache.lucene.analysis.standard.StandardAnalyzer;
    import org.apache.lucene.index.Term;
    import org.apache.lucene.queryParser.QueryParser;
    import org.apache.lucene.search.BooleanQuery;
    import org.apache.lucene.search.Hits;
    import org.apache.lucene.search.IndexSearcher;
    import org.apache.lucene.search.Query;
    import org.apache.lucene.search.TermQuery;
    import org.apache.lucene.store.Directory;
    import org.apache.lucene.store.FSDirectory;

    public class Searcher {
        
    public static void termQuery() throws Exception{
            Directory directory 
    = FSDirectory.getDirectory("./index"false); 
            IndexSearcher searcher 
    = new IndexSearcher(directory);
            Term t 
    = new Term("body","document");
            Query query 
    = new TermQuery(t);
            Hits hits 
    = searcher.search(query);
            System.out.println(hits.length());
        }

        
    public static void queryParser() throws Exception{
            Directory directory 
    = FSDirectory.getDirectory("./index"false); 
            IndexSearcher searcher 
    = new IndexSearcher(directory);
            Query query 
    = QueryParser.parse("text","body",new StandardAnalyzer());
            Hits hits 
    = searcher.search(query);
            System.out.println(hits.length());
        }

        
    public static void booleanQuery() throws Exception{
            Query parseQuery 
    = QueryParser.parse("text","body",new StandardAnalyzer());
            Term t 
    = new Term("body","document");
            Query termQuery 
    = new TermQuery(t);
            BooleanQuery boolQuery 
    = new BooleanQuery();
            boolQuery.add(parseQuery,
    true,false);
            boolQuery.add(termQuery,
    true,false);
            
            Directory directory 
    = FSDirectory.getDirectory("./index"false); 
            IndexSearcher searcher 
    = new IndexSearcher(directory);
            Hits hits 
    = searcher.search(boolQuery);
            System.out.println(hits.length());
        }

        
    public static void main(String[] args) throws Exception{
            termQuery();
            queryParser();
            booleanQuery();
        }

    }

    和上文一样,列写lucene的查询用法
    包括了RangeQuery  prefixQuery  phraseQuery  wildcastQuery   fuzzyQuery
    被索引查询的文件,按照需求自己构造即可。
    package search;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    import org.apache.lucene.analysis.standard.StandardAnalyzer;
    import org.apache.lucene.document.Document;
    import org.apache.lucene.document.Field;
    import org.apache.lucene.index.IndexWriter;
    import org.apache.lucene.index.Term;
    import org.apache.lucene.search.FuzzyQuery;
    import org.apache.lucene.search.Hits;
    import org.apache.lucene.search.IndexSearcher;
    import org.apache.lucene.search.PhraseQuery;
    import org.apache.lucene.search.PrefixQuery;
    import org.apache.lucene.search.Query;
    import org.apache.lucene.search.RangeQuery;
    import org.apache.lucene.search.TermQuery;
    import org.apache.lucene.search.WildcardQuery;
    import org.apache.lucene.store.Directory;
    import org.apache.lucene.store.RAMDirectory;

    public class SearcherShow {
        
    private static Directory directory = new RAMDirectory();
        
    public static void preIndex() throws Exception{
            String fileName1 
    = "./data/searchShow.txt";
            String fileName2 
    = "./data/searchShow2.txt";
            String fileName3 
    = "./data/test.txt";
            IndexWriter writer 
    = new IndexWriter(directory,new StandardAnalyzer(),true);
            Document doc1 
    = getDocument(fileName1);
            Document doc2 
    = getDocument(fileName2);
            Document doc3 
    = getDocument(fileName3);
            writer.addDocument(doc1);
            writer.addDocument(doc2);
            writer.addDocument(doc3);
            writer.close();
            
        }

        
    //先建立索引才能执行
        
        
    //termQuery   rangeQuery   booleanQuery的查询在Searcher类中
        public static void rangeQuery() throws Exception{
            Term startTerm 
    = new Term("lastmodified","20070620");
            Term endTerm 
    = new Term("lastmodified","20070622");
            RangeQuery query 
    = new RangeQuery(startTerm,endTerm,true);
            IndexSearcher searcher 
    = new IndexSearcher(directory);
            Hits hits 
    = searcher.search(query);
            prtHits(hits);
        }

        
    public static void prefixQuery() throws Exception{
            Term term 
    = new Term("fileName","searchShow.txt");
            Term prefixterm 
    = new Term("fileName","searchShow");
            IndexSearcher searcher 
    = new IndexSearcher(directory);
            Query query 
    = new TermQuery(term);
            Query prefixQuery 
    = new PrefixQuery(prefixterm);
            Hits hits 
    = searcher.search(query);
            Hits prefixHits 
    = searcher.search(prefixQuery);
            prtHits(hits);
            System.out.println(
    "----------");
            prtHits(prefixHits);
        }

        
    public static void phraseQuery() throws Exception{
            IndexSearcher searcher 
    = new IndexSearcher(directory);
            PhraseQuery query 
    = new PhraseQuery();
            query.setSlop(
    2);
            query.add(
    new Term("contents","quick"));
            query.add(
    new Term("contents","fox"));
            Hits hits 
    = searcher.search(query);
            prtHits(hits);
        }

        
    public static void wildcardQuery() throws Exception{
            IndexSearcher searcher 
    = new IndexSearcher(directory);
            Query query 
    = new WildcardQuery(new Term("contents","?ild*"));
            Hits hits 
    = searcher.search(query);
            prtHits(hits);
        }

        
    public static void fuzzyQuery() throws Exception{
            IndexSearcher searcher 
    = new IndexSearcher(directory);
            Term term 
    = new Term("contents","wuzza");
            FuzzyQuery query 
    = new FuzzyQuery(term);
            Hits hits 
    = searcher.search(query);
            prtHits(hits);
        }

        
    public static Document getDocument(String fileName) throws Exception{
            File file 
    = new File(fileName);
            Document doc 
    = new Document();
            doc.add(Field.Keyword(
    "fileName",file.getName() ));
            Date modified 
    = new Date(file.lastModified());
            String lastmodified 
    = new SimpleDateFormat("yyyyMMdd").format(modified);
            doc.add(Field.Keyword(
    "lastmodified", lastmodified));
            BufferedReader br 
    = new BufferedReader(new InputStreamReader(
                    
    new FileInputStream(file)));
            StringBuffer sb 
    = new StringBuffer();
            String line 
    = null;
            
    while ((line = br.readLine()) != null{
                sb.append(line);
            }

            br.close();
            doc.add(Field.Text(
    "contents",sb.toString() ));
            
    return doc;
        }

        
    public static void prtHits(Hits hits) throws Exception{
            
    for(int i=0;i<hits.length();i++){
                Document doc 
    = hits.doc(i);
                System.out.println(doc.get(
    "fileName"));
                System.out.println(doc.get(
    "lastmodified"));
            }

        }

        
    public static void main(String[] args) throws Exception{
            preIndex();
    //        rangeQuery();
    //        prefixQuery();
    //        phraseQuery();
    //        wildcardQuery();
            fuzzyQuery();
        }

    }

     

  • 相关阅读:
    IT 面试题
    elasticsearch学习(三):分布式
    es学习(二):elasticsearch 数据存储
    linux mysql 简单记录
    nginx 初了解
    dubbo 学习(一)
    关于通过angularJs将页面中的html table 导出生成excel
    postgresql编译安装与调试(二)
    postgresql编译安装与调试(一)
    说说shell脚本中的export 和 source,bash
  • 原文地址:https://www.cnblogs.com/pony/p/1486284.html
Copyright © 2011-2022 走看看