|
83 | 83 | import java.util.Collections; |
84 | 84 | import java.util.HashMap; |
85 | 85 | import java.util.HashSet; |
86 | | -import java.util.Iterator; |
87 | 86 | import java.util.List; |
88 | 87 | import java.util.Map; |
89 | 88 | import java.util.Objects; |
90 | 89 | import java.util.Set; |
91 | | -import java.util.Spliterator; |
92 | | -import java.util.Spliterators; |
93 | 90 | import java.util.UUID; |
94 | 91 | import java.util.concurrent.ConcurrentHashMap; |
95 | 92 | import java.util.function.Function; |
96 | 93 | import java.util.stream.Collectors; |
97 | 94 | import java.util.stream.IntStream; |
98 | 95 | import java.util.stream.Stream; |
99 | | -import java.util.stream.StreamSupport; |
100 | 96 |
|
101 | 97 | import static org.janusgraph.diskstorage.configuration.ConfigOption.disallowEmpty; |
102 | 98 | import static org.janusgraph.diskstorage.es.ElasticSearchConstants.ES_DOC_KEY; |
@@ -450,18 +446,13 @@ public class ElasticSearchIndex implements IndexProvider { |
450 | 446 | private static final String MAX_OPEN_SCROLL_CONTEXT_PARAMETER = "search.max_open_scroll_context"; |
451 | 447 | private static final Map<String, Object> MAX_RESULT_WINDOW = ImmutableMap.of("index.max_result_window", Integer.MAX_VALUE); |
452 | 448 |
|
453 | | - private static final Parameter[] NULL_PARAMETERS = null; |
454 | | - |
455 | | - private static final String TRACK_TOTAL_HITS_PARAMETER = "track_total_hits"; |
456 | | - private static final Parameter[] TRACK_TOTAL_HITS_DISABLED_PARAMETERS = new Parameter[]{new Parameter<>(TRACK_TOTAL_HITS_PARAMETER, false)}; |
457 | | - private static final Map<String, Object> TRACK_TOTAL_HITS_DISABLED_REQUEST_BODY = ImmutableMap.of(TRACK_TOTAL_HITS_PARAMETER, false); |
458 | | - |
459 | 449 | private final Function<String, String> generateIndexStoreNameFunction = this::generateIndexStoreName; |
460 | 450 | private final Map<String, String> indexStoreNamesCache = new ConcurrentHashMap<>(); |
461 | 451 | private final boolean indexStoreNameCacheEnabled; |
462 | 452 |
|
463 | 453 | private final AbstractESCompat compat; |
464 | 454 | private final ElasticSearchClient client; |
| 455 | + private final ElasticSearchSearcher searcher; |
465 | 456 | private final String indexName; |
466 | 457 | private final int batchSize; |
467 | 458 | private final boolean useExternalMappings; |
@@ -501,6 +492,7 @@ public ElasticSearchIndex(Configuration config) throws BackendException { |
501 | 492 | checkClusterHealth(config.get(HEALTH_REQUEST_TIMEOUT)); |
502 | 493 |
|
503 | 494 | compat = ESCompatUtils.acquireCompatForVersion(client.getMajorVersion()); |
| 495 | + searcher = new ElasticSearchSearcher(client, compat, batchSize); |
504 | 496 |
|
505 | 497 | indexSetting = ElasticSearchSetup.getSettingsFromJanusGraphConf(config); |
506 | 498 |
|
@@ -1551,37 +1543,18 @@ public Stream<String> query(IndexQuery query, KeyInformation.IndexRetriever info |
1551 | 1543 | if (!query.getOrder().isEmpty()) { |
1552 | 1544 | addOrderToQuery(informations, sr, query.getOrder(), query.getStore()); |
1553 | 1545 | } |
1554 | | - sr.setFrom(0); |
1555 | | - if (query.hasLimit()) { |
1556 | | - sr.setSize(Math.min(query.getLimit(), batchSize)); |
1557 | | - } else { |
1558 | | - sr.setSize(batchSize); |
1559 | | - } |
1560 | | - |
1561 | 1546 | sr.setDisableSourceRetrieval(true); |
1562 | 1547 |
|
1563 | | - ElasticSearchResponse response; |
1564 | 1548 | try { |
1565 | | - final String indexStoreName = getIndexStoreName(query.getStore()); |
1566 | | - final boolean useScroll = sr.getSize() >= batchSize; |
1567 | | - response = client.search(indexStoreName, |
1568 | | - compat.createRequestBody(sr, useScroll? NULL_PARAMETERS : TRACK_TOTAL_HITS_DISABLED_PARAMETERS), |
1569 | | - useScroll); |
1570 | | - log.debug("First Executed query [{}] in {} ms", query.getCondition(), response.getTook()); |
1571 | | - final Iterator<RawQuery.Result<String>> resultIterator = getResultsIterator(useScroll, response, sr.getSize()); |
1572 | | - final Stream<RawQuery.Result<String>> toReturn |
1573 | | - = StreamSupport.stream(Spliterators.spliteratorUnknownSize(resultIterator, Spliterator.ORDERED), false); |
1574 | | - return (query.hasLimit() ? toReturn.limit(query.getLimit()) : toReturn).map(RawQuery.Result::getResult); |
1575 | | - } catch (final IOException | UncheckedIOException e) { |
| 1549 | + log.debug("Executing query [{}]", query.getCondition()); |
| 1550 | + return searcher.search(getIndexStoreName(query.getStore()), sr, null, 0, query.getLimit()) |
| 1551 | + .map(RawQuery.Result::getResult); |
| 1552 | + } catch (final IOException e) { |
1576 | 1553 | //Classified like a write failure, so that BackendOperation reattempts a transient one within the read time |
1577 | 1554 | throw convert(e); |
1578 | 1555 | } |
1579 | 1556 | } |
1580 | 1557 |
|
1581 | | - private Iterator<RawQuery.Result<String>> getResultsIterator(boolean useScroll, ElasticSearchResponse response, int windowSize){ |
1582 | | - return (useScroll)? new ElasticSearchScroll(client, response, windowSize) : response.getResults().iterator(); |
1583 | | - } |
1584 | | - |
1585 | 1558 | private String convertToEsDataType(Class<?> dataType, Mapping mapping) { |
1586 | 1559 | if(String.class.isAssignableFrom(dataType)) { |
1587 | 1560 | return "string"; |
@@ -1614,35 +1587,6 @@ else if (Geoshape.class.isAssignableFrom(dataType)) { |
1614 | 1587 | return null; |
1615 | 1588 | } |
1616 | 1589 |
|
1617 | | - private ElasticSearchResponse runCommonQuery(RawQuery query, KeyInformation.IndexRetriever informations, BaseTransaction tx, int size, |
1618 | | - boolean useScroll) throws BackendException{ |
1619 | | - final ElasticSearchRequest sr = new ElasticSearchRequest(); |
1620 | | - sr.setQuery(compat.queryString(query.getQuery())); |
1621 | | - if (!query.getOrders().isEmpty()) { |
1622 | | - addOrderToQuery(informations, sr, query.getOrders(), query.getStore()); |
1623 | | - } |
1624 | | - sr.setFrom(0); |
1625 | | - sr.setSize(size); |
1626 | | - sr.setDisableSourceRetrieval(true); |
1627 | | - try { |
1628 | | - Map<String, Object> requestBody = compat.createRequestBody(sr, query.getParameters()); |
1629 | | - if(!useScroll) { |
1630 | | - if (requestBody == null) { |
1631 | | - requestBody = TRACK_TOTAL_HITS_DISABLED_REQUEST_BODY; |
1632 | | - } else { |
1633 | | - requestBody.put(TRACK_TOTAL_HITS_PARAMETER, false); |
1634 | | - } |
1635 | | - } |
1636 | | - return client.search( |
1637 | | - getIndexStoreName(query.getStore()), |
1638 | | - requestBody, |
1639 | | - useScroll); |
1640 | | - } catch (final IOException | UncheckedIOException e) { |
1641 | | - //Classified like a write failure, so that BackendOperation reattempts a transient one within the read time |
1642 | | - throw convert(e); |
1643 | | - } |
1644 | | - } |
1645 | | - |
1646 | 1590 | private long runCountQuery(RawQuery query) throws BackendException{ |
1647 | 1591 | try { |
1648 | 1592 | long countTotal = client.countTotal( |
@@ -1670,22 +1614,20 @@ private void addOrderToQuery(KeyInformation.IndexRetriever informations, Elastic |
1670 | 1614 | @Override |
1671 | 1615 | public Stream<RawQuery.Result<String>> query(RawQuery query, KeyInformation.IndexRetriever information, |
1672 | 1616 | BaseTransaction tx) throws BackendException { |
1673 | | - final int size = query.hasLimit() ? Math.min(query.getLimit() + query.getOffset(), batchSize) : batchSize; |
1674 | | - final boolean useScroll = size >= batchSize; |
1675 | | - final ElasticSearchResponse response = runCommonQuery(query, information, tx, size, useScroll); |
1676 | | - log.debug("First Executed query [{}] in {} ms", query.getQuery(), response.getTook()); |
1677 | | - final Iterator<RawQuery.Result<String>> resultIterator; |
| 1617 | + final ElasticSearchRequest sr = new ElasticSearchRequest(); |
| 1618 | + sr.setQuery(compat.queryString(query.getQuery())); |
| 1619 | + if (!query.getOrders().isEmpty()) { |
| 1620 | + addOrderToQuery(information, sr, query.getOrders(), query.getStore()); |
| 1621 | + } |
| 1622 | + sr.setDisableSourceRetrieval(true); |
1678 | 1623 | try { |
1679 | | - //A scroll whose first page is also its last is closed right here, which is one more request that can |
1680 | | - //fail the way the search itself can, so it is classified the same way |
1681 | | - resultIterator = getResultsIterator(useScroll, response, size); |
1682 | | - } catch (final UncheckedIOException e) { |
| 1624 | + log.debug("Executing query [{}]", query.getQuery()); |
| 1625 | + return searcher.search(getIndexStoreName(query.getStore()), sr, query.getParameters(), query.getOffset(), |
| 1626 | + query.getLimit()); |
| 1627 | + } catch (final IOException e) { |
| 1628 | + //Classified like a write failure, so that BackendOperation reattempts a transient one within the read time |
1683 | 1629 | throw convert(e); |
1684 | 1630 | } |
1685 | | - final Stream<RawQuery.Result<String>> toReturn |
1686 | | - = StreamSupport.stream(Spliterators.spliteratorUnknownSize(resultIterator, Spliterator.ORDERED), |
1687 | | - false).skip(query.getOffset()); |
1688 | | - return query.hasLimit() ? toReturn.limit(query.getLimit()) : toReturn; |
1689 | 1631 | } |
1690 | 1632 |
|
1691 | 1633 | @Override |
|
0 commit comments