educoder Lucene - 索引库的维护与查询

educoder Lucene - 索引库的维护与查询,第1张

educoder Lucene - 索引库的维护与查询 第1关:Lucene索引库维护 - 添加修改和删除索引
package step1;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.document;
import org.apache.lucene.document.TextField;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class HelloWorld {
	
	
	//删除索引
	public void delete()throws IOException{
		
		 Directory directory = FSDirectory.open(new File("/temp/doc/1101/index"));
    IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, new StandardAnalyzer());
    //创建一个indexwriter对象
    IndexWriter indexWriter = new IndexWriter(directory, config);
    //创建一个查询条件
    Query query = new TermQuery(new Term("content", "mybatis"));
    //根据查询条件删除
    indexWriter.deletedocuments(query);
    //关闭indexwriter
    indexWriter.close();
		
		
		
		
	}
	
	//修改索引
	public void update()throws IOException{
		
	Directory directory = FSDirectory.open(new File("/temp/doc/1101/index"));
    IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, new StandardAnalyzer());
    //创建一个indexwriter对象
    IndexWriter indexWriter = new IndexWriter(directory, config);
    //创建一个document对象
    document document = new document();
    //向document对象中添加域。
    //不同的document可以有不同的域,同一个document可以有相同的域。
    document.add(new TextField("filename", "web", Store.YES));
    document.add(new TextField("content", "hello lucene", Store.YES));
    indexWriter.updatedocument(new Term("content", "web"), document);
    //关闭indexWriter
    indexWriter.close();
		
		
		
		
		
	}
	
	//添加索引
	public void add() throws IOException{
		
		    //索引库存放路径
    Directory directory = FSDirectory.open(new File("/temp/doc/1101/index"));
    IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, new StandardAnalyzer());
    //创建一个indexwriter对象
    IndexWriter indexWriter = new IndexWriter(directory, config);
    //创建一个document对象
    document document = new document();
    //向document对象中添加域。
    //不同的document可以有不同的域,同一个document可以有相同的域。
    document.add(new TextField("filename", "test", Store.YES));
    document.add(new TextField("content", "www.educoder.net", Store.YES));
    

    //添加文档到索引库
    indexWriter.adddocument(document);
    //关闭indexwriter
    indexWriter.close();
		
		
		
		
		
	}
}
第2关:Lucene索引库查询 - 使用Query的子类查询
package step2;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.document.document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class QueryIndex {
	
	
	//查询
	public void query() throws IOException{
		
		    IndexSearcher indexSearcher = getIndexSearcher();
    //创建一个布尔查询对象
    BooleanQuery query = new BooleanQuery();
    //创建第一个查询条件
    Query query1 = new TermQuery(new Term("filename", "web"));
    Query query2 = new TermQuery(new Term("content", "lucene"));
	Query query3 = NumericRangeQuery.newLongRange("size", 1l, 40l, true, true);
    //组合查询条件
    query.add(query1, Occur.SHOULD);
    query.add(query2, Occur.SHOULD);
	query.add(query3, Occur.MUST);
    //执行查询
    printResult(query, indexSearcher);
		
		
		
		
		
		
		
	}
	
	//输出结果
	private void printResult(Query query, IndexSearcher indexSearcher) throws IOException {
		
		    TopDocs topDocs = indexSearcher.search(query, 10);
    ScoreDoc[] topDocArr = topDocs.scoreDocs;
    System.out.println("查询的结果总条数" + topDocs.totalHits);
    for (ScoreDoc scoreDoc : topDocArr) {
        document document = indexSearcher.doc(scoreDoc.doc); //获取document对象
        System.out.println("filename:" + document.get("filename"));
        System.out.println("size:" + document.get("size"));
		System.out.println("content:"+document.get("content"));
		
		
		
		
		
	}
		
	}

	//获取IndexSearcher对象
	private IndexSearcher getIndexSearcher() throws IOException {
		
		    //指定索引库存放的路径
    Directory directory = FSDirectory.open(new File("/temp/doc/1101/index"));
    //创建indexReader对象
    IndexReader reader = DirectoryReader.open(directory);
    //创建indexsearcher对象
    IndexSearcher searcher = new IndexSearcher(reader);

    return searcher;
		
	}

	
	
	
	
}
第3关:Lucene索引库查询 - 使用QueryParser查询
package step3;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.document.document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.wltea.analyzer.lucene.IKAnalyzer;

public class QueryIndex {
	
	
	//查询
	public void query() throws Exception{
		
		    IndexSearcher indexSearcher = getIndexSearcher();
    //可以指定默认搜索的域是多个
    String[] fields = {"filename", "content"};
    //创建一个MulitFiledQueryParser对象
    MultiFieldQueryParser queryParser = new MultiFieldQueryParser(fields, new IKAnalyzer());
    Query query = queryParser.parse("+filename:web +content:spring");
    //输出生成的查询语句
   
    //执行查询
    printResult(query, indexSearcher);
	
		
		
		
		
		
	}
	
	//输出结果
	private void printResult(Query query, IndexSearcher indexSearcher) throws IOException {
		TopDocs topDocs = indexSearcher.search(query, 10);
        ScoreDoc[] topDocArr = topDocs.scoreDocs;
        System.out.println("查询的结果总条数" + topDocs.totalHits);
        for (ScoreDoc scoreDoc : topDocArr) {
            document document = indexSearcher.doc(scoreDoc.doc); //获取document对象
            System.out.println("filename:" + document.get("filename"));
            System.out.println("size:" + document.get("size"));
        }
	}

	//获取IndexSearcher对象
	private IndexSearcher getIndexSearcher() throws IOException {
		//指定索引库存放的路径
        Directory directory = FSDirectory.open(new File("/temp/doc/1101/index"));
        //创建indexReader对象
        IndexReader reader = DirectoryReader.open(directory);
        //创建indexsearcher对象
        IndexSearcher searcher = new IndexSearcher(reader);
         return searcher;
	}
}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5719694.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存