use of org.apache.lucene.queryparser.classic.QueryParser in project ansj_seg by NLPchina.
the class IndexAndTest 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.queryparser.classic.QueryParser in project ansj_seg by NLPchina.
the class IndexAndTest 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.queryparser.classic.QueryParser 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));
}
use of org.apache.lucene.queryparser.classic.QueryParser in project orientdb by orientechnologies.
the class VertexIndexTest method testSpacesInQuery.
@Test
public void testSpacesInQuery() throws IOException, ParseException {
IndexWriterConfig conf = new IndexWriterConfig(new StandardAnalyzer());
final RAMDirectory directory = new RAMDirectory();
final IndexWriter writer = new IndexWriter(directory, conf);
Document doc = new Document();
doc.add(new TextField("name", "Max Water", Field.Store.YES));
writer.addDocument(doc);
doc = new Document();
doc.add(new TextField("name", "Max Waterson", Field.Store.YES));
writer.addDocument(doc);
doc = new Document();
doc.add(new TextField("name", "Cory Watney", Field.Store.YES));
writer.addDocument(doc);
writer.commit();
IndexReader reader = DirectoryReader.open(directory);
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer();
QueryParser queryParser = new QueryParser("name", analyzer);
final Query query = queryParser.parse("name:Max AND name:Wat*");
final TopDocs topDocs = searcher.search(query, 10);
assertThat(topDocs.totalHits).isEqualTo(2);
for (int i = 0; i < topDocs.totalHits; i++) {
final Document found = searcher.doc(topDocs.scoreDocs[i].doc);
assertThat(found.get("name")).startsWith("Max");
}
reader.close();
writer.close();
}
use of org.apache.lucene.queryparser.classic.QueryParser in project orientdb by orientechnologies.
the class LuceneBooleanIndexTest method testMemoryIndex.
@Test
public void testMemoryIndex() throws ParseException {
// TODO To be used in evaluate Record
MemoryIndex index = new MemoryIndex();
Document doc = new Document();
doc.add(new StringField("text", "my text", Field.Store.YES));
StandardAnalyzer analyzer = new StandardAnalyzer();
for (IndexableField field : doc.getFields()) {
index.addField(field.name(), field.stringValue(), analyzer);
}
QueryParser parser = new QueryParser("text", analyzer);
float score = index.search(parser.parse("+text:my"));
}
Aggregations