Search in sources :

Example 56 with QueryParser

use of org.apache.lucene.queryparser.classic.QueryParser in project orientdb by orientechnologies.

the class OLuceneIndexType method getQueryParser.

protected static Query getQueryParser(OIndexDefinition index, String key, Analyzer analyzer) throws ParseException {
    QueryParser queryParser;
    if ((key).startsWith("(")) {
        queryParser = new QueryParser("", analyzer);
    } else {
        String[] fields = null;
        if (index.isAutomatic()) {
            fields = index.getFields().toArray(new String[index.getFields().size()]);
        } else {
            int length = index.getTypes().length;
            fields = new String[length];
            for (int i = 0; i < length; i++) {
                fields[i] = "k" + i;
            }
        }
        queryParser = new MultiFieldQueryParser(fields, analyzer);
    }
    return queryParser.parse(key);
}
Also used : MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser)

Example 57 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 58 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)

Example 59 with QueryParser

use of org.apache.lucene.queryparser.classic.QueryParser in project neo4j by neo4j.

the class IndexType method query.

Query query(String keyOrNull, Object value, QueryContext contextOrNull) {
    if (value instanceof Query) {
        return (Query) value;
    }
    QueryParser parser = new QueryParser(keyOrNull, analyzer);
    parser.setAllowLeadingWildcard(true);
    parser.setLowercaseExpandedTerms(toLowerCase);
    if (contextOrNull != null && contextOrNull.getDefaultOperator() != null) {
        parser.setDefaultOperator(contextOrNull.getDefaultOperator());
    }
    try {
        return parser.parse(value.toString());
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }
}
Also used : QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) ParseException(org.apache.lucene.queryparser.classic.ParseException)

Example 60 with QueryParser

use of org.apache.lucene.queryparser.classic.QueryParser in project incubator-atlas by apache.

the class QueryFactory method create.

private QueryExpression create(Request request, ResourceDefinition resourceDefinition) throws InvalidQueryException {
    String queryString;
    if (request.getCardinality() == Request.Cardinality.INSTANCE) {
        String idPropertyName = resourceDefinition.getIdPropertyName();
        queryString = String.format("%s:%s", idPropertyName, request.<String>getProperty(idPropertyName));
    } else {
        queryString = request.getQueryString();
    }
    QueryExpression queryExpression;
    if (queryString != null && !queryString.isEmpty()) {
        QueryParser queryParser = new QueryParser(Version.LUCENE_48, "name", new KeywordAnalyzer());
        queryParser.setLowercaseExpandedTerms(false);
        queryParser.setAllowLeadingWildcard(true);
        Query query;
        try {
            query = queryParser.parse((String) escape(queryString));
        } catch (ParseException e) {
            throw new InvalidQueryException(e.getMessage());
        }
        LOG.info("LuceneQuery: {}", query);
        queryExpression = create(query, resourceDefinition);
    } else {
        queryExpression = new AlwaysQueryExpression();
    }
    // add query properties to request so that they are returned
    request.addAdditionalSelectProperties(queryExpression.getProperties());
    return queryExpression;
}
Also used : KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) RegexQuery(org.apache.lucene.sandbox.queries.regex.RegexQuery) ParseException(org.apache.lucene.queryparser.classic.ParseException) InvalidQueryException(org.apache.atlas.catalog.exception.InvalidQueryException)

Aggregations

QueryParser (org.apache.lucene.queryparser.classic.QueryParser)73 Query (org.apache.lucene.search.Query)50 IndexSearcher (org.apache.lucene.search.IndexSearcher)32 Document (org.apache.lucene.document.Document)26 IOException (java.io.IOException)24 Analyzer (org.apache.lucene.analysis.Analyzer)21 TopDocs (org.apache.lucene.search.TopDocs)21 IndexReader (org.apache.lucene.index.IndexReader)18 ScoreDoc (org.apache.lucene.search.ScoreDoc)18 ArrayList (java.util.ArrayList)16 ParseException (org.apache.lucene.queryparser.classic.ParseException)16 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)14 BooleanQuery (org.apache.lucene.search.BooleanQuery)14 TermQuery (org.apache.lucene.search.TermQuery)13 ScoredDocuments (io.anserini.rerank.ScoredDocuments)6 Term (org.apache.lucene.index.Term)6 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)6 WildcardQuery (org.apache.lucene.search.WildcardQuery)6 EnglishAnalyzer (org.apache.lucene.analysis.en.EnglishAnalyzer)5 IndexWriter (org.apache.lucene.index.IndexWriter)5