Search in sources :

Example 1 with RawQuery

use of org.janusgraph.diskstorage.indexing.RawQuery in project janusgraph by JanusGraph.

the class LuceneIndex method totals.

@Override
public Long totals(RawQuery query, KeyInformation.IndexRetriever information, BaseTransaction tx) throws BackendException {
    final Query q;
    try {
        q = getQueryParser(query.getStore(), information).parse(query.getQuery());
    } catch (final ParseException e) {
        throw new PermanentBackendException("Could not parse raw query: " + query.getQuery(), e);
    }
    try {
        final IndexSearcher searcher = ((Transaction) tx).getSearcher(query.getStore());
        // Index does not yet exist
        if (searcher == null)
            return 0L;
        final long time = System.currentTimeMillis();
        // Lucene doesn't like limits of 0.  Also, it doesn't efficiently build a total list.
        query.setLimit(1);
        // We ignore offset and limit for totals
        final TopDocs docs = searcher.search(q, 1);
        log.debug("Executed query [{}] in {} ms", q, System.currentTimeMillis() - time);
        return docs.totalHits.value;
    } catch (final IOException e) {
        throw new TemporaryBackendException("Could not execute Lucene query", e);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) TemporaryBackendException(org.janusgraph.diskstorage.TemporaryBackendException) Query(org.apache.lucene.search.Query) RegexpQuery(org.apache.lucene.search.RegexpQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) IndexQuery(org.janusgraph.diskstorage.indexing.IndexQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) RawQuery(org.janusgraph.diskstorage.indexing.RawQuery) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) TermQuery(org.apache.lucene.search.TermQuery) NormsFieldExistsQuery(org.apache.lucene.search.NormsFieldExistsQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) BaseTransaction(org.janusgraph.diskstorage.BaseTransaction) PermanentBackendException(org.janusgraph.diskstorage.PermanentBackendException) ParseException(org.apache.lucene.queryparser.classic.ParseException) IOException(java.io.IOException)

Example 2 with RawQuery

use of org.janusgraph.diskstorage.indexing.RawQuery in project janusgraph by JanusGraph.

the class IndexSerializer method executeQuery.

public Stream<RawQuery.Result> executeQuery(IndexQueryBuilder query, final ElementCategory resultType, final BackendTransaction backendTx, final StandardJanusGraphTx transaction) {
    final MixedIndexType index = getMixedIndex(query.getIndex(), transaction);
    final String queryStr = createQueryString(query, resultType, transaction, index);
    ImmutableList<IndexQuery.OrderEntry> orders = getOrders(query, resultType, transaction, index);
    final RawQuery rawQuery = new RawQuery(index.getStoreName(), queryStr, orders, query.getParameters());
    if (query.hasLimit())
        rawQuery.setLimit(query.getLimit());
    rawQuery.setOffset(query.getOffset());
    return backendTx.rawQuery(index.getBackingIndexName(), rawQuery).map(result -> new RawQuery.Result(string2ElementId(result.getResult()), result.getScore()));
}
Also used : MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) RawQuery(org.janusgraph.diskstorage.indexing.RawQuery)

Example 3 with RawQuery

use of org.janusgraph.diskstorage.indexing.RawQuery in project janusgraph by JanusGraph.

the class IndexSerializer method executeTotals.

public Long executeTotals(IndexQueryBuilder query, final ElementCategory resultType, final BackendTransaction backendTx, final StandardJanusGraphTx transaction) {
    final MixedIndexType index = getMixedIndex(query.getIndex(), transaction);
    final String queryStr = createQueryString(query, resultType, transaction, index);
    final RawQuery rawQuery = new RawQuery(index.getStoreName(), queryStr, query.getParameters());
    if (query.hasLimit())
        rawQuery.setLimit(query.getLimit());
    rawQuery.setOffset(query.getOffset());
    return backendTx.totals(index.getBackingIndexName(), rawQuery);
}
Also used : MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) RawQuery(org.janusgraph.diskstorage.indexing.RawQuery)

Example 4 with RawQuery

use of org.janusgraph.diskstorage.indexing.RawQuery in project janusgraph by JanusGraph.

the class LuceneIndex method query.

@Override
public Stream<RawQuery.Result<String>> query(RawQuery query, KeyInformation.IndexRetriever information, BaseTransaction tx) throws BackendException {
    final Query q;
    try {
        q = getQueryParser(query.getStore(), information).parse(query.getQuery());
    // Lucene query parser does not take additional parameters so any parameters on the RawQuery are ignored.
    } catch (final ParseException e) {
        throw new PermanentBackendException("Could not parse raw query: " + query.getQuery(), e);
    }
    try {
        final IndexSearcher searcher = ((Transaction) tx).getSearcher(query.getStore());
        if (searcher == null) {
            // Index does not yet exist
            return Collections.unmodifiableList(new ArrayList<RawQuery.Result<String>>()).stream();
        }
        final long time = System.currentTimeMillis();
        // TODO: can we make offset more efficient in Lucene?
        final int offset = query.getOffset();
        int adjustedLimit = query.hasLimit() ? query.getLimit() : Integer.MAX_VALUE - 1;
        if (adjustedLimit < Integer.MAX_VALUE - 1 - offset)
            adjustedLimit += offset;
        else
            adjustedLimit = Integer.MAX_VALUE - 1;
        final TopDocs docs;
        if (query.getOrders().isEmpty()) {
            docs = searcher.search(q, adjustedLimit);
        } else {
            docs = searcher.search(q, adjustedLimit, getSortOrder(query.getOrders(), information.get(query.getStore())));
        }
        log.debug("Executed query [{}] in {} ms", q, System.currentTimeMillis() - time);
        final List<RawQuery.Result<String>> result = new ArrayList<>(docs.scoreDocs.length);
        for (int i = offset; i < docs.scoreDocs.length; i++) {
            final IndexableField field = searcher.doc(docs.scoreDocs[i].doc, FIELDS_TO_LOAD).getField(DOCID);
            result.add(new RawQuery.Result<>(field == null ? null : field.stringValue(), docs.scoreDocs[i].score));
        }
        return result.stream();
    } catch (final IOException e) {
        throw new TemporaryBackendException("Could not execute Lucene query", e);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) RegexpQuery(org.apache.lucene.search.RegexpQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) IndexQuery(org.janusgraph.diskstorage.indexing.IndexQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) RawQuery(org.janusgraph.diskstorage.indexing.RawQuery) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) TermQuery(org.apache.lucene.search.TermQuery) NormsFieldExistsQuery(org.apache.lucene.search.NormsFieldExistsQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) PermanentBackendException(org.janusgraph.diskstorage.PermanentBackendException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DoublePoint(org.apache.lucene.document.DoublePoint) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint) TopDocs(org.apache.lucene.search.TopDocs) IndexableField(org.apache.lucene.index.IndexableField) TemporaryBackendException(org.janusgraph.diskstorage.TemporaryBackendException) BaseTransaction(org.janusgraph.diskstorage.BaseTransaction) RawQuery(org.janusgraph.diskstorage.indexing.RawQuery) ParseException(org.apache.lucene.queryparser.classic.ParseException)

Aggregations

RawQuery (org.janusgraph.diskstorage.indexing.RawQuery)4 IOException (java.io.IOException)2 ParseException (org.apache.lucene.queryparser.classic.ParseException)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 DocValuesFieldExistsQuery (org.apache.lucene.search.DocValuesFieldExistsQuery)2 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)2 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)2 NormsFieldExistsQuery (org.apache.lucene.search.NormsFieldExistsQuery)2 PrefixQuery (org.apache.lucene.search.PrefixQuery)2 Query (org.apache.lucene.search.Query)2 RegexpQuery (org.apache.lucene.search.RegexpQuery)2 TermQuery (org.apache.lucene.search.TermQuery)2 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)2 TopDocs (org.apache.lucene.search.TopDocs)2 BaseTransaction (org.janusgraph.diskstorage.BaseTransaction)2 PermanentBackendException (org.janusgraph.diskstorage.PermanentBackendException)2 TemporaryBackendException (org.janusgraph.diskstorage.TemporaryBackendException)2 IndexQuery (org.janusgraph.diskstorage.indexing.IndexQuery)2