Search in sources :

Example 91 with QueryParser

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));
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Query(org.apache.lucene.search.Query) Document(org.apache.lucene.document.Document)

Example 92 with QueryParser

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));
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Query(org.apache.lucene.search.Query) Document(org.apache.lucene.document.Document)

Example 93 with QueryParser

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));
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) TokenStream(org.apache.lucene.analysis.TokenStream) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) RAMDirectory(org.apache.lucene.store.RAMDirectory) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) CharTermAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute) AnsjAnalyzer(org.ansj.lucene5.AnsjAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) TextField(org.apache.lucene.document.TextField) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 94 with QueryParser

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();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) Document(org.apache.lucene.document.Document) Analyzer(org.apache.lucene.analysis.Analyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) RAMDirectory(org.apache.lucene.store.RAMDirectory) TopDocs(org.apache.lucene.search.TopDocs) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) IndexWriter(org.apache.lucene.index.IndexWriter) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) IndexReader(org.apache.lucene.index.IndexReader) TextField(org.apache.lucene.document.TextField) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Test(org.junit.Test)

Example 95 with QueryParser

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"));
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) MemoryIndex(org.apache.lucene.index.memory.MemoryIndex) StringField(org.apache.lucene.document.StringField) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Document(org.apache.lucene.document.Document) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Aggregations

QueryParser (org.apache.lucene.queryparser.classic.QueryParser)114 Query (org.apache.lucene.search.Query)79 IndexSearcher (org.apache.lucene.search.IndexSearcher)54 Document (org.apache.lucene.document.Document)47 TopDocs (org.apache.lucene.search.TopDocs)39 ParseException (org.apache.lucene.queryparser.classic.ParseException)38 IOException (java.io.IOException)35 Analyzer (org.apache.lucene.analysis.Analyzer)34 ScoreDoc (org.apache.lucene.search.ScoreDoc)31 IndexReader (org.apache.lucene.index.IndexReader)27 ArrayList (java.util.ArrayList)25 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)24 BooleanQuery (org.apache.lucene.search.BooleanQuery)23 TermQuery (org.apache.lucene.search.TermQuery)19 MultiFieldQueryParser (org.apache.lucene.queryparser.classic.MultiFieldQueryParser)12 HashSet (java.util.HashSet)10 Directory (org.apache.lucene.store.Directory)10 IndexWriter (org.apache.lucene.index.IndexWriter)9 IndexableField (org.apache.lucene.index.IndexableField)8 KeywordAnalyzer (org.apache.lucene.analysis.core.KeywordAnalyzer)7