Skip to content

Commit c01bfea

Browse files
committed
Fix ElasticSearch scroll result is created even on no scroll CTR
Fixes #2038 Signed-off-by: Oleksandr Porunov <alexandr.porunov@gmail.com>
1 parent 4e09ad6 commit c01bfea

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

janusgraph-backend-testutils/src/main/java/org/janusgraph/graphdb/JanusGraphBaseTest.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -453,13 +453,17 @@ public static <T> Stream<T> asStream(final Iterator<T> source) {
453453
return StreamSupport.stream(iterable.spliterator(),false);
454454
}
455455

456-
public JanusGraph getForceIndexGraph() throws BackendException {
457-
final ModifiableConfiguration adjustedConfig = new ModifiableConfiguration(GraphDatabaseConfiguration.ROOT_NS, getConfiguration(), BasicConfiguration.Restriction.NONE);
456+
public JanusGraph getForceIndexGraph() {
457+
return getForceIndexGraph(getConfiguration());
458+
}
459+
460+
public JanusGraph getForceIndexGraph(WriteConfiguration writeConfiguration) {
461+
final ModifiableConfiguration adjustedConfig = new ModifiableConfiguration(GraphDatabaseConfiguration.ROOT_NS, writeConfiguration, BasicConfiguration.Restriction.NONE);
458462
adjustedConfig.set(GraphDatabaseConfiguration.FORCE_INDEX_USAGE, true);
459-
final WriteConfiguration writeConfig = adjustedConfig.getConfiguration();
460-
TestGraphConfigs.applyOverrides(writeConfig);
461-
Preconditions.checkNotNull(writeConfig);
462-
return JanusGraphFactory.open(writeConfig);
463+
final WriteConfiguration adjustedWriteConfig = adjustedConfig.getConfiguration();
464+
TestGraphConfigs.applyOverrides(adjustedWriteConfig);
465+
Preconditions.checkNotNull(adjustedWriteConfig);
466+
return JanusGraphFactory.open(adjustedWriteConfig);
463467
}
464468

465469
}

janusgraph-backend-testutils/src/main/java/org/janusgraph/graphdb/JanusGraphIndexTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,6 +2224,24 @@ public void testOrForceIndexPartialIndex() throws Exception {
22242224
}
22252225
}
22262226

2227+
@Test
2228+
public void testIndexDataRetrievalWithLimitLessThenBatch() throws Exception {
2229+
WriteConfiguration config = getConfiguration();
2230+
config.set("index.search.max-result-set-size", 10);
2231+
JanusGraph customGraph = getForceIndexGraph(config);
2232+
final JanusGraphManagement management = customGraph.openManagement();
2233+
final PropertyKey num = management.makePropertyKey("num").dataType(Integer.class).cardinality(Cardinality.SINGLE).make();
2234+
management.buildIndex("oridx", Vertex.class).addKey(num).buildMixedIndex(INDEX);
2235+
management.commit();
2236+
customGraph.tx().commit();
2237+
final GraphTraversalSource g = customGraph.traversal();
2238+
g.addV().property("num", 1).next();
2239+
g.addV().property("num", 2).next();
2240+
customGraph.tx().commit();
2241+
assertEquals(2, customGraph.traversal().V().has("num", P.lt(3)).limit(4).toList().size());
2242+
JanusGraphFactory.close(customGraph);
2243+
}
2244+
22272245
@Test
22282246
public void testOrForceIndexComposite() throws Exception {
22292247
JanusGraph customGraph = null;

janusgraph-es/src/main/java/org/janusgraph/diskstorage/es/ElasticSearchIndex.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ public Stream<String> query(IndexQuery query, KeyInformation.IndexRetriever info
11121112
compat.createRequestBody(sr, useScroll? NULL_PARAMETERS : TRACK_TOTAL_HITS_DISABLED_PARAMETERS),
11131113
useScroll);
11141114
log.debug("First Executed query [{}] in {} ms", query.getCondition(), response.getTook());
1115-
final ElasticSearchScroll resultIterator = new ElasticSearchScroll(client, response, sr.getSize());
1115+
final Iterator<RawQuery.Result<String>> resultIterator = getResultsIterator(useScroll, response, sr.getSize());
11161116
final Stream<RawQuery.Result<String>> toReturn
11171117
= StreamSupport.stream(Spliterators.spliteratorUnknownSize(resultIterator, Spliterator.ORDERED), false);
11181118
return (query.hasLimit() ? toReturn.limit(query.getLimit()) : toReturn).map(RawQuery.Result::getResult);
@@ -1121,6 +1121,10 @@ public Stream<String> query(IndexQuery query, KeyInformation.IndexRetriever info
11211121
}
11221122
}
11231123

1124+
private Iterator<RawQuery.Result<String>> getResultsIterator(boolean useScroll, ElasticSearchResponse response, int windowSize){
1125+
return (useScroll)? new ElasticSearchScroll(client, response, windowSize) : response.getResults().iterator();
1126+
}
1127+
11241128
private String convertToEsDataType(Class<?> dataType, Mapping mapping) {
11251129
if(String.class.isAssignableFrom(dataType)) {
11261130
return "string";
@@ -1206,9 +1210,10 @@ private void addOrderToQuery(KeyInformation.IndexRetriever informations, Elastic
12061210
public Stream<RawQuery.Result<String>> query(RawQuery query, KeyInformation.IndexRetriever information,
12071211
BaseTransaction tx) throws BackendException {
12081212
final int size = query.hasLimit() ? Math.min(query.getLimit() + query.getOffset(), batchSize) : batchSize;
1209-
final ElasticSearchResponse response = runCommonQuery(query, information, tx, size, size >= batchSize );
1213+
final boolean useScroll = size >= batchSize;
1214+
final ElasticSearchResponse response = runCommonQuery(query, information, tx, size, useScroll);
12101215
log.debug("First Executed query [{}] in {} ms", query.getQuery(), response.getTook());
1211-
final ElasticSearchScroll resultIterator = new ElasticSearchScroll(client, response, size);
1216+
final Iterator<RawQuery.Result<String>> resultIterator = getResultsIterator(useScroll, response, size);
12121217
final Stream<RawQuery.Result<String>> toReturn
12131218
= StreamSupport.stream(Spliterators.spliteratorUnknownSize(resultIterator, Spliterator.ORDERED),
12141219
false).skip(query.getOffset());

0 commit comments

Comments
 (0)