查询索引
package com.gykalc.rediscluster.lucene.index;
import com.gykalc.rediscluster.lucene.ikanalyzer.IKAnalyzer6x;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.DoublePoint;
import org.apache.lucene.index.*;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.*;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
public class SearchIndex {
@Test
public void searchMulti() throws IOException, ParseException {
Path indexdir = Paths.get("indexdir");
FSDirectory dir = FSDirectory.open(indexdir);
IndexReader reader = DirectoryReader.open(dir);
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer ikAnalyzer6x = new IKAnalyzer6x();
String[] fields = {"title", "sell_point"};
MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, ikAnalyzer6x);
Query query = parser.parse("平板");
System.out.println(query.toString());
TopDocs topDocs = searcher.search(query, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc: scoreDocs) {
Document doc = searcher.doc(scoreDoc.doc);
System.out.println(doc.get("id"));
System.out.println(doc.get("title"));
System.out.println(doc.get("sell_point"));
}
}
@Test
public void termQuery() throws IOException {
Path indexdir = Paths.get("indexdir");
FSDirectory dir = FSDirectory.open(indexdir);
IndexReader reader = DirectoryReader.open(dir);
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer ikAnalyzer6x = new IKAnalyzer6x();
Term term = new Term("title", "平板");
Query query = new TermQuery(term);
System.out.println(query.toString());
TopDocs topDocs = searcher.search(query, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc: scoreDocs) {
Document doc = searcher.doc(scoreDoc.doc);
System.out.println(doc.get("id"));
System.out.println(doc.get("title"));
System.out.println(doc.get("sell_point"));
}
}
@Test
public void booleanQuery() throws IOException {
Path indexdir = Paths.get("indexdir");
FSDirectory dir = FSDirectory.open(indexdir);
IndexReader reader = DirectoryReader.open(dir);
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer ikAnalyzer6x = new IKAnalyzer6x();
Query query1 = new TermQuery(new Term("title", "达内"));
Query query2 = new TermQuery(new Term("sell_point", "好用"));
BooleanClause bc1 = new BooleanClause(query1, BooleanClause.Occur.SHOULD);
BooleanClause bc2 = new BooleanClause(query2, BooleanClause.Occur.SHOULD);
BooleanQuery query = new BooleanQuery.Builder().add(bc1).add(bc2).build();
System.out.println(query.toString());
TopDocs topDocs = searcher.search(query, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc: scoreDocs) {
Document doc = searcher.doc(scoreDoc.doc);
System.out.println(doc.get("id"));
System.out.println(doc.get("title"));
System.out.println(doc.get("sell_point"));
}
}
@Test
public void rangeQuery() throws IOException {
Path indexdir = Paths.get("indexdir");
FSDirectory dir = FSDirectory.open(indexdir);
IndexReader reader = DirectoryReader.open(dir);
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer ikAnalyzer6x = new IKAnalyzer6x();
Query query = DoublePoint.newRangeQuery("price", 2000, 3000);
System.out.println(query.toString());
TopDocs topDocs = searcher.search(query, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc: scoreDocs) {
Document doc = searcher.doc(scoreDoc.doc);
System.out.println(doc.get("id"));
System.out.println(doc.get("title"));
System.out.println(doc.get("sell_point"));
System.out.println(doc.get("price"));
}
}
@Test
public void prefixQuery() throws IOException {
Path indexdir = Paths.get("indexdir");
FSDirectory dir = FSDirectory.open(indexdir);
IndexReader reader = DirectoryReader.open(dir);
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer ikAnalyzer6x = new IKAnalyzer6x();
Term term = new Term("title", "达内");
Query query = new PrefixQuery(term);
System.out.println(query.toString());
TopDocs topDocs = searcher.search(query, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc: scoreDocs) {
Document doc = searcher.doc(scoreDoc.doc);
System.out.println(doc.get("id"));
System.out.println(doc.get("title"));
System.out.println(doc.get("sell_point"));
System.out.println(doc.get("price"));
}
}
@Test
public void multiTermQuery() throws IOException {
Path indexdir = Paths.get("indexdir");
FSDirectory dir = FSDirectory.open(indexdir);
IndexReader reader = DirectoryReader.open(dir);
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer ikAnalyzer6x = new IKAnalyzer6x();
PhraseQuery.Builder builder = new PhraseQuery.Builder();
builder.add(new Term("title", "达内"));
Query query = builder.build();
System.out.println(query.toString());
TopDocs topDocs = searcher.search(query, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc: scoreDocs) {
Document doc = searcher.doc(scoreDoc.doc);
System.out.println(doc.get("id"));
System.out.println(doc.get("title"));
System.out.println(doc.get("sell_point"));
System.out.println(doc.get("price"));
}
}
@Test
public void fuzzyQuery() throws IOException {
Path indexdir = Paths.get("indexdir");
FSDirectory dir = FSDirectory.open(indexdir);
IndexReader reader = DirectoryReader.open(dir);
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer ikAnalyzer6x = new IKAnalyzer6x();
Query query = new FuzzyQuery(new Term("title", "格达"));
System.out.println(query.toString());
TopDocs topDocs = searcher.search(query, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc: scoreDocs) {
Document doc = searcher.doc(scoreDoc.doc);
System.out.println(doc.get("id"));
System.out.println(doc.get("title"));
System.out.println(doc.get("sell_point"));
System.out.println(doc.get("price"));
}
}
}