lucene(索引的查询)

lucene(索引的查询)

lucene查询的核心类:

IndexSearcher、Term、Query、TermQuery、TopDocs、ScoreDoc

搜索功能的简单实现

创建IndexReader

// 此处是创建索引存放的地址
Directory driectory = FSDirectory.open(new File("D:/lucene/files2"));
IndexReader reader = IndexReader.open(driectory);

创建IndexSearcher

IndexSearcher indexSearche=new IndexSearcher(reader);

创建Term和TermQuery

IndexSearcher indexSearcher = new IndexSearcher(reader);
Query query=new TermQuery(new Term(field,name));

根据TermQuery获取TopDocs

TopDocs tds = searcher.search(query, num);
System.out.println("一共查询了:" + tds.totalHits);

根据TopDocs获取ScoreDoc

for (ScoreDoc sd : tds.scoreDocs) {
	Document doc = searcher.doc(sd.doc);//根据ScoreDoc获取相应的Document
	System.out.println(doc.get("fieldName"));
}

以上是基础核心代码,举例简单查询代码如下:

  Directory dir = FSDirectory.open(new File("D:/lucene/files2"));
 IndexSearcher searcher = new IndexSearcher(reader);
 Query q = new TermQuery(new Term("fieldName", "keyWord"));
 TopDocs tds = searcher.search(q, 500);
 ScoreDoc[] sds = tds.scoreDocs;
 // 查询500个,从start开始,从end结束
 for (int i = start; i < end; i++) {
   Document doc = searcher.doc(sds[i].doc);
   System.out.println("path" + doc.get("path"));
 }

其他的Query

TermRangeQuery(段域范围查询)

lucene(索引的查询)_第1张图片

NumricRangeQuery(数字范围查询)

Query q =NumericRangeQuery.newIntRange(field,start,end,true,true);
TopDocs tds = searcher.search(q, num);

PrefixQuery(前缀查询)

//此时的value就是通过前缀来匹配的
Query q =new PrefixQuery(new Term(field,value));

WildcardQuery(通配符查询)

//此查询是查询filename以java开始的所有结果集
Query q = new WildcardQuery(new Term("filename","java*"));

BooleanQuery(多条件查询)【经常使用】

lucene(索引的查询)_第2张图片

PhraseQuery(短语查询)

lucene(索引的查询)_第3张图片

FuzzyQuery模糊查询

//调整相似读 允许有个别错误的查询 
Query q = new FuzzyQuery(new Term("field", ""), 0.4f, 0);

QueryParse(重点

创建queryParse

QueryParser parser = new QueryParser(Version.LUCENE_35,"content",new StandardAnalyzer(Version.LUCENE_35));

各种匹配方式

lucene(索引的查询)_第4张图片

分页查询

Directory dir = FSDirectory.open(new File("D:/lucene/files2"));
IndexReader reader = IndexReader.open(dir);
IndexSearcher searcher = new IndexSearcher(reader);
QueryParser parser = new QueryParser(Version.LUCENE_35, "content",new StandardAnalyzer(Version.LUCENE_35));
Query q = parser.parse(query);
TopDocs tds = searcher.search(q, 500);
ScoreDoc[] sds = tds.scoreDocs;
// 每次取出所有数据,从这写数据进行再查询
int start = (pageIndex - 1) * pageSize;
int end = pageIndex * pageSize;
for (int i = start; i < end; i++) {
	Document doc = searcher.doc(sds[i].doc);
	System.out.println("filename=" + doc.get("filename"));
}
searcher.close();

高级搜索

搜索排序

public class AnalyzerUtils {
	public void searcher(String queryStr, Sort sort) {
		try {
			Directory dir = FSDirectory.open(new File("D:/lucene/files2"));
			IndexReader reader = IndexReader.open(dir);
			IndexSearcher searcher = new IndexSearcher(reader);
			Version version = Version.LUCENE_35;
			Analyzer analyzer = new StandardAnalyzer(version);
			QueryParser parser = new QueryParser(version, "content", analyzer);
			Query query = parser.parse(queryStr);
			TopDocs tds = null;
			if (sort == null) {
				// 默认排序
				tds = searcher.search(query, 50);
			} else {
				// 按照穿日的值进行排序
				tds = searcher.search(query, 50, sort);
			}
			for (ScoreDoc sd : tds.scoreDocs) {
				Document doc = searcher.doc(sd.doc);
				System.out.println("评分:" + sd.score + "-->filename"
						+ doc.get("filename"));
			}
			searcher.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

按照分值排序

// 先按大小再按评分排序
		analyzerUtils.searcher("java", new Sort(new SortField("size",
				SortField.INT), SortField.FIELD_SCORE));

按照索引序号排序

// 按照id排序
analyzerUtils.searcher("java", Sort.INDEXORDER);

按照不同的域进行排序

AnalyzerUtils analyzerUtils = new AnalyzerUtils();
// 先按大小再按评分排序
analyzerUtils.searcher("java", new Sort(new SortField("size",SortField.INT), SortField.FIELD_SCORE));
// 按照id排序
analyzerUtils.searcher("java", Sort.INDEXORDER);
// 按照默认评分排序
analyzerUtils.searcher("java", null);
// 按照文件的大小排序
analyzerUtils.searcher("java", new Sort(new SortField("size",SortField.INT)));
// 按照日期排序
analyzerUtils.searcher("java", new Sort(new SortField("date",SortField.LONG)));
// 通过设置一个最后一个参数booleanReverse是否反转的意思就是倒叙即DESC还是ASC
analyzerUtils.searcher("java", new Sort(new SortField("date",SortField.LONG)));


你可能感兴趣的:(lucene)