package com.lucene.demo2;
import java.nio.file.Paths;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
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.WildcardQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
public class SearchDemo {
public static Analyzer analyzer = new StandardAnalyzer();
public static void main(String[] args) throws Exception {
SearchDemo demo = new SearchDemo();
demo.index();
demo.TermQuery();
demo.BooleanQuery();
demo.PhraseQuery();
demo.NumericRangeQuery();
demo.WildcardQuery();
}
public void QueryParser() throws Exception {
QueryParser parser = new QueryParser("fieldname", analyzer);
Query query = parser.parse("hello");
search(query, "QueryParser");
}
/**
* 查询fieldname域中包含hello的document
*
* @throws Exception
*/
public void TermQuery() throws Exception {
Term term = new Term("fieldname", "hello");
Query query = new TermQuery(term);
search(query, "TermQuery");
}
/**
* 组合查询,可以添加其他查询
*
* @throws Exception
*/
public void BooleanQuery() throws Exception {
BooleanQuery query = new BooleanQuery();
// 第一个
Term term1 = new Term("fieldname", "hello");
Query query1 = new TermQuery(term1);
// 第二个
Term term2 = new Term("fieldname", "work");
Query query2 = new TermQuery(term2);
// 第三个
Term term3 = new Term("fieldname", "my");
Query query3 = new TermQuery(term3);
// 组合
query.add(query1, Occur.MUST_NOT);// 必须不能有hello,非的意思
query.add(query2, Occur.SHOULD);// 可以有work,或的意思
query.add(query3, Occur.MUST);// 必须有my,与的意思
search(query, "BooleanQuery");
}
/**
* 间隔查询,默认0个字间隔
*
* @throws Exception
*/
public void PhraseQuery() throws Exception {
PhraseQuery query = new PhraseQuery();
query.setSlop(1);// 设置多少个字内的距离
query.add(new Term("fieldname", "my"));
query.add(new Term("fieldname", "my"));
search(query, "PhraseQuery");
}
/**
* 数字范围内查询
*
* @throws Exception
*/
public void NumericRangeQuery() throws Exception {
NumericRangeQuery query = NumericRangeQuery.newIntRange("num", 10, 111, true, true);
search(query, "NumericRangeQuery");
}
/**
* 通配符查询
*
* @throws Exception
*/
public void WildcardQuery() throws Exception {
WildcardQuery query = new WildcardQuery(new Term("fieldname", "?orld"));
//WildcardQuery query = new WildcardQuery(new Term("fieldname", "*world"));
search(query, "WildcardQuery");
}
public void index() throws Exception {
Directory directory = FSDirectory.open(Paths.get("index"));
IndexWriterConfig config = new IndexWriterConfig(analyzer);
config.setOpenMode(OpenMode.CREATE);
// 把文件添加到索引
IndexWriter iwriter = new IndexWriter(directory, config);
// 创建Document对象
Document doc = new Document();
String text = "hello world";
doc.add(new Field("fieldname", text, TextField.TYPE_STORED));
iwriter.addDocument(doc);
doc = new Document();
String text2 = "my world";
doc.add(new Field("fieldname", text2, TextField.TYPE_STORED));
iwriter.addDocument(doc);
doc = new Document();
String text3 = "my world my";
doc.add(new Field("fieldname", text3, TextField.TYPE_STORED));
iwriter.addDocument(doc);
doc = new Document();
int num1 = 1;
doc.add(new IntField("num", num1, IntField.TYPE_STORED));
iwriter.addDocument(doc);
doc = new Document();
int num2 = 11;
doc.add(new IntField("num", num2, IntField.TYPE_STORED));
iwriter.addDocument(doc);
doc = new Document();
int num3 = 111;
doc.add(new IntField("num", num3, IntField.TYPE_STORED));
iwriter.addDocument(doc);
iwriter.close();
directory.close();
}
public void search(Query query, String orgStr) throws Exception {
Directory directory = FSDirectory.open(Paths.get("index"));
DirectoryReader ireader = DirectoryReader.open(directory);
IndexSearcher isearcher = new IndexSearcher(ireader);
ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;
// 输出查询结果:
for (int i = 0; i < hits.length; i++) {
Document hitDoc = isearcher.doc(hits[i].doc);
if (null != hitDoc.get("num")) {
System.out.println(orgStr + ":" + hitDoc.get("num"));
} else if (null != hitDoc.get("fieldname")) {
System.out.println(orgStr + ":" + hitDoc.get("fieldname"));
}
}
ireader.close();
directory.close();
}
}
结果打印:
TermQuery:hello world
BooleanQuery:my world my
BooleanQuery:my world
PhraseQuery:my world my
NumericRangeQuery:11
NumericRangeQuery:111
WildcardQuery:hello world
WildcardQuery:my world
WildcardQuery:my world my