Skip to content

Commit 431e2d8

Browse files
committed
Bug fix: Fixed incorrect logic to determine if an index should be queried. This was causing some jobs due to fail by submitting an empty query to an index that should not have been queried.
1 parent 52d157a commit 431e2d8

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Once you have installed Elasticsearch, you can install zentity from a remote URL
3131

3232
Example:
3333

34-
`elasticsearch-plugin install https://zentity.io/releases/zentity-1.4.1-elasticsearch-7.3.1.zip`
34+
`elasticsearch-plugin install https://zentity.io/releases/zentity-1.4.2-beta1-elasticsearch-7.3.1.zip`
3535

3636
Read the [installation](https://zentity.io/docs/installation) docs for more details.
3737

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<zentity.author>Dave Moore</zentity.author>
1515
<zentity.classname>org.elasticsearch.plugin.zentity.ZentityPlugin</zentity.classname>
1616
<zentity.website>https://zentity.io</zentity.website>
17-
<zentity.version>1.4.1</zentity.version>
17+
<zentity.version>1.4.2-beta1</zentity.version>
1818
<!-- dependency versions -->
1919
<elasticsearch.version>7.3.1</elasticsearch.version>
2020
<jackson.core.version>2.9.9</jackson.core.version>

src/main/java/io/zentity/resolution/Job.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,16 @@ public Job(NodeClient client) {
8989
this.client = client;
9090
}
9191

92-
public static String serializeElasticsearchException(ElasticsearchException e) throws IOException {
93-
String cause = Strings.toString(e.toXContent(jsonBuilder().startObject(), ToXContent.EMPTY_PARAMS).endObject());
94-
return "{\"error\":{\"root_cause\":[" + cause + "],\"type\":\"" + ElasticsearchException.getExceptionName(e) + "\",\"reason\":\"" + e.getMessage() + "\"},\"status\":" + e.status().getStatus() + "}";
92+
public static String serializeException(Exception e) throws IOException {
93+
String serialized;
94+
if (e instanceof ElasticsearchException) {
95+
ElasticsearchException ee = (ElasticsearchException) e;
96+
String cause = Strings.toString(ee.toXContent(jsonBuilder().startObject(), ToXContent.EMPTY_PARAMS).endObject());
97+
serialized = "{\"error\":{\"root_cause\":[" + cause + "],\"type\":\"" + ElasticsearchException.getExceptionName(ee) + "\",\"reason\":\"" + e.getMessage() + "\"},\"status\":" + ee.status().getStatus() + "}";
98+
} else {
99+
serialized = "{\"error\":{\"type\":\"" + e.getClass() + "\",\"reason\":\"" + e.getMessage() + "\"}}";
100+
}
101+
return serialized;
95102
}
96103

97104
public static String serializeLoggedQuery(Input input, int _hop, int _query, String indexName, String request, String response, List<String> resolvers, TreeMap<Integer, TreeMap<String, TreeMap>> resolversFilterTreeGrouped, List<String> termResolvers, TreeMap<String, TreeMap> termResolversFilterTree) throws JsonProcessingException {
@@ -645,8 +652,6 @@ private void traverse() throws IOException, ValidationException {
645652
if (!this.docIds.containsKey(indexName))
646653
this.docIds.put(indexName, new TreeSet<>());
647654

648-
boolean filterIds = this.hop == 0 && this.input().ids().containsKey(indexName) && !this.input().ids().get(indexName).isEmpty();
649-
650655
// "_explanation" uses named queries, and each value of the "_name" fields must be unique.
651656
// Use a counter to prepend a unique and deterministic identifier for each "_name" field in the query.
652657
AtomicInteger _nameIdCounter = new AtomicInteger();
@@ -656,7 +661,12 @@ private void traverse() throws IOException, ValidationException {
656661
for (String resolverName : this.input.model().resolvers().keySet())
657662
if (canQuery(this.input.model(), indexName, resolverName, this.attributes))
658663
resolvers.add(resolverName);
659-
if (resolvers.size() == 0 && !filterIds && (this.hop == 0 && this.input.terms().isEmpty()))
664+
665+
// Determine if we can query this index.
666+
boolean canQueryIds = this.hop == 0 && this.input().ids().containsKey(indexName) && !this.input().ids().get(indexName).isEmpty();
667+
boolean canQueryTerms = this.hop == 0 && !this.input.terms().isEmpty();
668+
boolean canQueryAttributes = resolvers.size() > 0;
669+
if (!canQueryAttributes && !canQueryIds && !canQueryTerms)
660670
continue;
661671

662672
// Construct query for this index.
@@ -702,7 +712,7 @@ else if (size == 1)
702712

703713
// Construct the "ids" clause if this is the first hop and if any ids are specified for this index.
704714
String idsClause = "";
705-
if (filterIds) {
715+
if (canQueryIds) {
706716
Set<String> ids = this.input().ids().get(indexName);
707717
idsClause = "{\"bool\":{\"filter\":[{\"ids\":{\"values\":[" + String.join(",", ids) + "]}}]}}";
708718
}
@@ -782,7 +792,7 @@ else if (parentResolverClauses.size() == 1)
782792
// unlike structured attribute search where the attributes are assumed be known.
783793
List<String> termResolvers = new ArrayList<>();
784794
TreeMap<String, TreeMap> termResolversFilterTree = new TreeMap<>();
785-
if (this.hop == 0 && !this.input.terms().isEmpty()) {
795+
if (canQueryTerms) {
786796
String termResolversClause = "";
787797

788798
// Get the names of each attribute of each in-scope resolver.
@@ -986,15 +996,15 @@ else if (!resolversClause.isEmpty())
986996

987997
// Submit query to Elasticsearch.
988998
SearchResponse response = null;
989-
ElasticsearchException responseError = null;
999+
Exception responseError = null;
9901000
boolean fatalError = false;
9911001
try {
9921002
response = this.search(indexName, query);
9931003
} catch (IndexNotFoundException e) {
9941004
// Don't fail the job if an index was missing.
9951005
this.missingIndices.add(e.getIndex().getName());
9961006
responseError = e;
997-
} catch (ElasticsearchException e) {
1007+
} catch (Exception e) {
9981008
// Fail the job for any other error.
9991009
fatalError = true;
10001010
responseError = e;
@@ -1018,7 +1028,7 @@ else if (!resolversClause.isEmpty())
10181028
}
10191029
responseString = responseDataCopyObj.toString();
10201030
} else {
1021-
responseString = serializeElasticsearchException(responseError);
1031+
responseString = serializeException(responseError);
10221032
}
10231033
String logged = serializeLoggedQuery(this.input, this.hop, _query, indexName, query, responseString, resolvers, resolversFilterTreeGrouped, termResolvers, termResolversFilterTree);
10241034
this.queries.add(logged);
@@ -1027,7 +1037,7 @@ else if (!resolversClause.isEmpty())
10271037
// Stop traversing if there was an error not due to a missing index.
10281038
// Include the logged query in the response.
10291039
if (fatalError) {
1030-
this.error = serializeLoggedQuery(this.input, this.hop, _query, indexName, query, serializeElasticsearchException(responseError), resolvers, resolversFilterTreeGrouped, termResolvers, termResolversFilterTree);
1040+
this.error = serializeLoggedQuery(this.input, this.hop, _query, indexName, query, serializeException(responseError), resolvers, resolversFilterTreeGrouped, termResolvers, termResolversFilterTree);
10311041
return;
10321042
}
10331043

0 commit comments

Comments
 (0)