Skip to content

Commit

Permalink
HSEARCH-4173 Factorize code that builds explicit and implicit nested …
Browse files Browse the repository at this point in the history
…predicate for Elasticsearch

Signed-off-by: Yoann Rodière <yoann@hibernate.org>
  • Loading branch information
yrodiere authored and fax4ever committed Mar 10, 2021
1 parent 5c722a6 commit b75a846
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
Expand Up @@ -10,7 +10,6 @@
import java.util.List;
import java.util.Objects;

import org.hibernate.search.backend.elasticsearch.gson.impl.JsonAccessor;
import org.hibernate.search.backend.elasticsearch.logging.impl.Log;
import org.hibernate.search.util.common.logging.impl.LoggerFactory;

Expand All @@ -19,8 +18,6 @@
public abstract class AbstractElasticsearchNestablePredicate extends AbstractElasticsearchPredicate {

private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() );
private static final JsonAccessor<String> PATH_ACCESSOR = JsonAccessor.root().property( "path" ).asString();
private static final JsonAccessor<JsonObject> QUERY_ACCESSOR = JsonAccessor.root().property( "query" ).asObject();

AbstractElasticsearchNestablePredicate(AbstractElasticsearchPredicate.AbstractBuilder builder) {
super( builder );
Expand Down Expand Up @@ -66,13 +63,9 @@ public JsonObject toJsonQuery(PredicateRequestContext context) {
break;
}

JsonObject innerObject = new JsonObject();

PATH_ACCESSOR.set( innerObject, path );
QUERY_ACCESSOR.set( innerObject, result );

JsonObject outerObject = new JsonObject();
outerObject.add( "nested", innerObject );
JsonObject innerObject = new JsonObject();
ElasticsearchNestedPredicate.wrap( indexNames(), path, outerObject, innerObject, result );
result = outerObject;
}

Expand Down
Expand Up @@ -7,6 +7,7 @@
package org.hibernate.search.backend.elasticsearch.search.predicate.impl;

import java.util.List;
import java.util.Set;

import org.hibernate.search.backend.elasticsearch.gson.impl.JsonAccessor;
import org.hibernate.search.backend.elasticsearch.search.impl.ElasticsearchSearchContext;
Expand Down Expand Up @@ -35,16 +36,22 @@ protected JsonObject doToJsonQuery(PredicateRequestContext context, JsonObject o
JsonObject innerObject) {
PredicateRequestContext nestedContext = context.withNestedPath( absoluteFieldPath );

wrap( indexNames(), absoluteFieldPath, outerObject, innerObject,
nestedPredicate.toJsonQuery( nestedContext ) );

return outerObject;
}

static void wrap(Set<String> indexNames, String absoluteFieldPath,
JsonObject outerObject, JsonObject innerObject, JsonObject toWrap) {
PATH_ACCESSOR.set( innerObject, absoluteFieldPath );
QUERY_ACCESSOR.set( innerObject, nestedPredicate.toJsonQuery( nestedContext ) );
if ( indexNames().size() > 1 ) {
QUERY_ACCESSOR.set( innerObject, toWrap );
if ( indexNames.size() > 1 ) {
// There are multiple target indexes; some of them may not declare the nested field.
// Instruct ES to behave as if the nested field had no value in that case.
IGNORE_UNMAPPED_ACCESSOR.set( innerObject, true );
}
outerObject.add( "nested", innerObject );

return outerObject;
}

static class Builder extends AbstractBuilder implements NestedPredicateBuilder {
Expand Down

0 comments on commit b75a846

Please sign in to comment.