1

lucene入门-搜索方式-LMLPHP
 package com.home.utils;

 import java.util.ArrayList;
import java.util.List; import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.PhraseQuery;
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.WildcardQuery;
import org.junit.Test; /**
* 1、关键词查询 2、查询所有文档 3、范围查询 4、通配符查询 5、短语查询 6、Boolean查询
*
* @author Administrator
*
*/
public class QueryTest { private void showData(Query query) throws Exception {
IndexSearcher indexSearcher = new IndexSearcher(LuceneUtils.directory);
TopDocs topDocs = indexSearcher.search(query, 25);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
List<Article> articleList = new ArrayList<Article>();
for (ScoreDoc scoreDoc : scoreDocs) {
Document document = indexSearcher.doc(scoreDoc.doc);
Article article = DocumentUtils.document2Article(document);
articleList.add(article);
} for (Article article : articleList) {
System.out.println(article.getId());
System.out.println(article.getTitle());
System.out.println(article.getContent());
}
} /**
* 关键词查询 因为没有分词器,所以区分大小写
*/
@Test
public void testTermQuery() throws Exception {
Term term = new Term("title", "lucene");
Query query = new TermQuery(term);
this.showData(query);
} /**
* 查询所有的文档
*/
@Test
public void testAllDocQuery() throws Exception {
Query query = new MatchAllDocsQuery();
this.showData(query);
} /**
* 通配符查询 * 代表任意多个任意字符 ? 任意一个任意字符
*/
@Test
public void testWildCartQuery() throws Exception {
Term term = new Term("title", "*.java");
Query query = new WildcardQuery(term);
this.showData(query);
} /**
* 短语查询 所有的关键词对象必须针对同一个属性
*
* @param query
* @throws Exception
*/
@Test
public void testPharseQuery() throws Exception {
Term term = new Term("title", "lucene");
Term term2 = new Term("title", "搜索");
PhraseQuery query = new PhraseQuery();
query.add(term);
query.add(term2, 4);
this.showData(query); } /**
* boolean查询 各种关键词的组合
*
* @param query
* @throws Exception
*/
@Test
public void testBooleanQuery() throws Exception {
Term term = new Term("title", "北京");
TermQuery termQuery = new TermQuery(term);
Term term2 = new Term("title", "美女");
TermQuery termQuery2 = new TermQuery(term2);
Term term3 = new Term("title", "北京美女");
TermQuery termQuery3 = new TermQuery(term3); BooleanQuery booleanQuery = new BooleanQuery();
booleanQuery.add(termQuery, Occur.SHOULD);
booleanQuery.add(termQuery2, Occur.SHOULD);
booleanQuery.add(termQuery3, Occur.SHOULD);
this.showData(booleanQuery);
} /**
* 范围查询
* @param query
* @throws Exception
*/
@Test
public void testRangeQuery() throws Exception{
Query query = NumericRangeQuery.newLongRange("id", 5L, 10L, true, true);
this.showData(query);
} }
lucene入门-搜索方式-LMLPHP

原文地址:https://www.cnblogs.com/sharpest/p/5992564.html

05-23 17:11