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);
}
}
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()));
}
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);
}
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);
}
}
Aggregations