use of org.apache.lucene.document.Document in project ansj_seg by NLPchina.
the class HeightLightTest method search.
private static void search(Analyzer analyzer, Query query) throws IOException {
DirectoryReader directoryReader = DirectoryReader.open(directory);
// 查询索引
IndexSearcher isearcher = new IndexSearcher(directoryReader);
System.out.println(query);
TopDocs hits = isearcher.search(query, 5);
for (int i = 0; i < hits.scoreDocs.length; i++) {
int docId = hits.scoreDocs[i].doc;
Document document = isearcher.doc(docId);
System.out.println(toHighlighter(analyzer, query, document));
}
}
use of org.apache.lucene.document.Document in project ansj_seg by NLPchina.
the class HeightLightTest method index.
private static void index(Analyzer analysis, String content) throws CorruptIndexException, IOException {
Document doc = new Document();
IndexWriter iwriter = new IndexWriter(directory, new IndexWriterConfig(analysis));
doc.add(new TextField("text", content, Field.Store.YES));
iwriter.addDocument(doc);
iwriter.commit();
iwriter.close();
}
use of org.apache.lucene.document.Document in project ansj_seg by NLPchina.
the class IndexTest method search.
private void search(Analyzer queryAnalyzer, Directory directory, String queryStr) throws CorruptIndexException, IOException, ParseException {
IndexSearcher isearcher;
DirectoryReader directoryReader = DirectoryReader.open(directory);
// 查询索引
isearcher = new IndexSearcher(directoryReader);
QueryParser tq = new QueryParser("text", queryAnalyzer);
Query query = tq.parse(queryStr);
System.out.println(query);
TopDocs hits = isearcher.search(query, 5);
System.out.println(queryStr + ":共找到" + hits.totalHits + "条记录!");
for (int i = 0; i < hits.scoreDocs.length; i++) {
int docId = hits.scoreDocs[i].doc;
Document document = isearcher.doc(docId);
System.out.println(toHighlighter(queryAnalyzer, query, document));
}
}
use of org.apache.lucene.document.Document in project ansj_seg by NLPchina.
the class IndexTest method search.
private void search(Analyzer queryAnalyzer, Directory directory, String queryStr) throws CorruptIndexException, IOException, ParseException {
IndexSearcher isearcher;
DirectoryReader directoryReader = DirectoryReader.open(directory);
// 查询索引
isearcher = new IndexSearcher(directoryReader);
QueryParser tq = new QueryParser("text", queryAnalyzer);
Query query = tq.parse(queryStr);
System.out.println(query);
TopDocs hits = isearcher.search(query, 5);
System.out.println(queryStr + ":共找到" + hits.totalHits + "条记录!");
for (int i = 0; i < hits.scoreDocs.length; i++) {
int docId = hits.scoreDocs[i].doc;
Document document = isearcher.doc(docId);
System.out.println(toHighlighter(queryAnalyzer, query, document));
}
}
use of org.apache.lucene.document.Document in project ansj_seg by NLPchina.
the class PhraseTest method main.
public static void main(String[] args) throws IOException, ParseException {
DicLibrary.insert(DicLibrary.DEFAULT, "上网人");
DicLibrary.insert(DicLibrary.DEFAULT, "网人");
AnsjAnalyzer ansjAnalyzer = new AnsjAnalyzer(AnsjAnalyzer.TYPE.index_ansj);
TokenStream tokenStream = ansjAnalyzer.tokenStream("上网人员测试", "test");
while (tokenStream.incrementToken()) {
System.out.println(tokenStream.getAttribute(CharTermAttribute.class));
}
IndexWriterConfig config = new IndexWriterConfig(ansjAnalyzer);
IndexWriter writer = new IndexWriter(new RAMDirectory(), config);
Document doc = new Document();
doc.add(new TextField("test", "上网人员测试", Field.Store.YES));
writer.addDocument(doc);
writer.commit();
IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer));
System.out.println(searcher.count(new TermQuery(new Term("test", "网人"))));
Query q = new QueryParser("test", new AnsjAnalyzer(AnsjAnalyzer.TYPE.index_ansj)).parse("\"上网人\"");
System.out.println(q);
System.out.println(searcher.count(q));
}
Aggregations