lucene4.9之Query_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > lucene4.9之Query

lucene4.9之Query

 2015/1/28 13:43:48  w62268458  程序员俱乐部  我要评论(0)
  • 摘要:测试Query类型importjava.io.File;importorg.apache.lucene.document.Document;importorg.apache.lucene.index.DirectoryReader;importorg.apache.lucene.index.IndexReader;importorg.apache.lucene.index.Term;importorg.apache.lucene.search.BooleanQuery;importorg
  • 标签:lucene

测试Query类型

class="java">import java.io.File;

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.PhraseQuery;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TermRangeQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;

import com.test.utils.File2DocumentUtil;

public class QueryTest {
	String filePath = "F:\\eclipse\\LuceneTest\\luceneDatasource\\IndexWriter addDocument's a javadoc .txt";
	String indexPath = "F:\\eclipse\\LuceneTest\\luceneIndex";

	/**
	 * 按词条搜索—TermQuery
	 * 
	 * @throws Exception
	 */
	@Test
	public void testTermQuery() throws Exception {
		Term term = new Term("content", "document");
		Query query = new TermQuery(term);

		queryAndPrintResult(query);
	}

	/**
	 * 范围检索 看不懂
	 * 
	 * @throws Exception
	 */
	@Test
	public void testRangeQuery() throws Exception {
		Term lowerTerm = new Term("size", "0");
		Term upperTerm = new Term("size", "4000");
		// Query query = new TermRangeQuery("size", lowerTerm.bytes(),
		// upperTerm.bytes(), true, false);
		Query query = TermRangeQuery.newStringRange("size", "000", "500", true, true);

		queryAndPrintResult(query);
	}

	// 使用前缀搜索—PrefixQuery
	@Test
	public void testPrefixQuery() throws Exception {
		Term term = new Term("content", "房");
		PrefixQuery query = new PrefixQuery(term);
		queryAndPrintResult(query);

	}

	/*
	 * 通配符查询
	 * '?'代表一个字符。'*'代表0个或多个字符
	 */
	@Test
	public void testWildCardQuery() throws Exception {
		Term term = new Term("content", "*间");
		WildcardQuery query = new WildcardQuery(term);
		queryAndPrintResult(query);

	}

	// 短语搜索—PhraseQuery
	@Test
	public void testPhraseQuery() throws Exception {
		PhraseQuery query = new PhraseQuery();
		//设定position位置
//		query.add(new Term("content", "旅游"), 0);
//		query.add(new Term("content", "的"), 2);
		query.add(new Term("content", "旅游"));
		query.add(new Term("content", "的"));
		//指定多个未知短语
		query.setSlop(1);
		queryAndPrintResult(query);
	}

	/**
	 * 按“与或”搜索—BooleanQuery 1.和: MUST与MUST_NOT
	 * 
	 * 2.或: SHOULD与SHOULD
	 * 
	 * 3.A与B的并集-B MUST与MUST_NOT
	 * 
	 * @throws Exception
	 */
	@Test
	public void testBooleanQuery() throws Exception {
		PhraseQuery query = new PhraseQuery();
		query.add(new Term("content", "adds"));
		query.add(new Term("content", "room"));
		query.setSlop(2);

		Term term = new Term("content", "add1");
		PrefixQuery query2 = new PrefixQuery(term);

		BooleanQuery booleanQuery = new BooleanQuery();
		booleanQuery.add(query, Occur.SHOULD);
		booleanQuery.add(query2, Occur.SHOULD);
		queryAndPrintResult(booleanQuery);
	}

	public void queryAndPrintResult(Query query) throws Exception {
		System.out.println("对应的查询字符串:" + query);
		IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexPath)));
		IndexSearcher indexSearcher = new IndexSearcher(reader);
		TopDocs topDocs = indexSearcher.search(query, 10000);
		System.out.println("总共有【" + topDocs.totalHits + "】条匹配结果");
		for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
			int docSn = scoreDoc.doc;
			Document doc = indexSearcher.doc(docSn);
			File2DocumentUtil.printDocumnetInfo(doc);
		}
	}
}

?

上一篇: lucene4.9之highlight 下一篇: lucene4.9之analyzer
发表评论
用户名: 匿名