Search in sources :

Example 1 with IndexQuery

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

the class ElasticSearchIndexTest method testUpdateAdditionWithLongString.

/**
 * Test adding and overwriting with long string content.
 */
@Test
public void testUpdateAdditionWithLongString() throws Exception {
    initialize("vertex");
    Multimap<String, Object> initialDoc = HashMultimap.create();
    initialDoc.put(TEXT, RandomStringUtils.randomAlphanumeric(500000) + " bob " + RandomStringUtils.randomAlphanumeric(500000));
    add("vertex", "long", initialDoc, true);
    clopen();
    assertEquals(1, tx.queryStream(new IndexQuery("vertex", PredicateCondition.of(TEXT, Text.CONTAINS, "bob"))).count());
    assertEquals(0, tx.queryStream(new IndexQuery("vertex", PredicateCondition.of(TEXT, Text.CONTAINS, "world"))).count());
    tx.add("vertex", "long", TEXT, RandomStringUtils.randomAlphanumeric(500000) + " world " + RandomStringUtils.randomAlphanumeric(500000), false);
    clopen();
    assertEquals(0, tx.queryStream(new IndexQuery("vertex", PredicateCondition.of(TEXT, Text.CONTAINS, "bob"))).count());
    assertEquals(1, tx.queryStream(new IndexQuery("vertex", PredicateCondition.of(TEXT, Text.CONTAINS, "world"))).count());
}
Also used : IndexQuery(org.janusgraph.diskstorage.indexing.IndexQuery) Test(org.junit.Test) IndexProviderTest(org.janusgraph.diskstorage.indexing.IndexProviderTest)

Example 2 with IndexQuery

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

the class ElasticSearchIndex method query.

@Override
public Stream<String> query(IndexQuery query, KeyInformation.IndexRetriever informations, BaseTransaction tx) throws BackendException {
    final ElasticSearchRequest sr = new ElasticSearchRequest();
    final Map<String, Object> esQuery = getFilter(query.getCondition(), informations.get(query.getStore()));
    sr.setQuery(compat.prepareQuery(esQuery));
    if (!query.getOrder().isEmpty()) {
        final List<IndexQuery.OrderEntry> orders = query.getOrder();
        for (final IndexQuery.OrderEntry orderEntry : orders) {
            final String order = orderEntry.getOrder().name();
            final KeyInformation information = informations.get(query.getStore()).get(orderEntry.getKey());
            final Mapping mapping = Mapping.getMapping(information);
            final Class<?> datatype = orderEntry.getDatatype();
            sr.addSort(orderEntry.getKey(), order.toLowerCase(), convertToEsDataType(datatype, mapping));
        }
    }
    sr.setFrom(0);
    if (query.hasLimit()) {
        sr.setSize(Math.min(query.getLimit(), batchSize));
    } else {
        sr.setSize(batchSize);
    }
    ElasticSearchResponse response;
    try {
        final String indexStoreName = getIndexStoreName(query.getStore());
        final String indexType = useMultitypeIndex ? query.getStore() : null;
        response = client.search(indexStoreName, indexType, compat.createRequestBody(sr, NULL_PARAMETERS), sr.getSize() >= batchSize);
        log.debug("First Executed query [{}] in {} ms", query.getCondition(), response.getTook());
        final ElasticSearchScroll resultIterator = new ElasticSearchScroll(client, response, sr.getSize());
        final Stream<RawQuery.Result<String>> toReturn = StreamSupport.stream(Spliterators.spliteratorUnknownSize(resultIterator, Spliterator.ORDERED), false);
        return (query.hasLimit() ? toReturn.limit(query.getLimit()) : toReturn).map(RawQuery.Result::getResult);
    } catch (final IOException | UncheckedIOException e) {
        throw new PermanentBackendException(e);
    }
}
Also used : IndexQuery(org.janusgraph.diskstorage.indexing.IndexQuery) PermanentBackendException(org.janusgraph.diskstorage.PermanentBackendException) Mapping(org.janusgraph.core.schema.Mapping) IndexMapping(org.janusgraph.diskstorage.es.IndexMappings.IndexMapping) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) KeyInformation(org.janusgraph.diskstorage.indexing.KeyInformation)

Example 3 with IndexQuery

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

the class ElasticSearchIndexTest method testIngestPipeline.

/**
 * Test ingest pipeline.
 */
@Test
public void testIngestPipeline() throws Exception {
    if (esr.getEsMajorVersion().value > 2) {
        initialize("ingestvertex");
        final Multimap<String, Object> docs = HashMultimap.create();
        docs.put(TEXT, "bob");
        add("ingestvertex", "pipeline", docs, true);
        clopen();
        assertEquals(1, tx.queryStream(new IndexQuery("ingestvertex", PredicateCondition.of(TEXT, Text.CONTAINS, "bob"))).count());
        assertEquals(1, tx.queryStream(new IndexQuery("ingestvertex", PredicateCondition.of(STRING, Cmp.EQUAL, "hello"))).count());
    }
}
Also used : IndexQuery(org.janusgraph.diskstorage.indexing.IndexQuery) Test(org.junit.Test) IndexProviderTest(org.janusgraph.diskstorage.indexing.IndexProviderTest)

Example 4 with IndexQuery

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

the class SolrIndex method query.

@Override
public Stream<String> query(IndexQuery query, KeyInformation.IndexRetriever information, BaseTransaction tx) throws BackendException {
    final String collection = query.getStore();
    final String keyIdField = getKeyFieldId(collection);
    final SolrQuery solrQuery = new SolrQuery("*:*");
    solrQuery.set(CommonParams.FL, keyIdField);
    final String queryFilter = buildQueryFilter(query.getCondition(), information.get(collection));
    solrQuery.addFilterQuery(queryFilter);
    if (!query.getOrder().isEmpty()) {
        final List<IndexQuery.OrderEntry> orders = query.getOrder();
        for (final IndexQuery.OrderEntry order1 : orders) {
            final String item = order1.getKey();
            final SolrQuery.ORDER order = order1.getOrder() == Order.ASC ? SolrQuery.ORDER.asc : SolrQuery.ORDER.desc;
            solrQuery.addSort(new SolrQuery.SortClause(item, order));
        }
    }
    solrQuery.setStart(0);
    if (query.hasLimit()) {
        solrQuery.setRows(Math.min(query.getLimit(), batchSize));
    } else {
        solrQuery.setRows(batchSize);
    }
    return executeQuery(query.hasLimit() ? query.getLimit() : null, 0, collection, solrQuery, doc -> doc.getFieldValue(keyIdField).toString());
}
Also used : IndexQuery(org.janusgraph.diskstorage.indexing.IndexQuery) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Aggregations

IndexQuery (org.janusgraph.diskstorage.indexing.IndexQuery)4 IndexProviderTest (org.janusgraph.diskstorage.indexing.IndexProviderTest)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 SolrQuery (org.apache.solr.client.solrj.SolrQuery)1 Mapping (org.janusgraph.core.schema.Mapping)1 PermanentBackendException (org.janusgraph.diskstorage.PermanentBackendException)1 IndexMapping (org.janusgraph.diskstorage.es.IndexMappings.IndexMapping)1 KeyInformation (org.janusgraph.diskstorage.indexing.KeyInformation)1