Skip to content

Commit

Permalink
HSEARCH-4217 Use a predicate type key to retrieve the nested predicat…
Browse files Browse the repository at this point in the history
…e builders
  • Loading branch information
yrodiere committed Jun 14, 2021
1 parent ee612af commit 2752b4d
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 49 deletions.
Expand Up @@ -17,6 +17,7 @@
import org.hibernate.search.backend.elasticsearch.search.impl.ElasticsearchSearchCompositeIndexSchemaElementContext;
import org.hibernate.search.backend.elasticsearch.search.impl.ElasticsearchSearchCompositeIndexSchemaElementQueryElementFactory;
import org.hibernate.search.backend.elasticsearch.search.impl.ElasticsearchSearchContext;
import org.hibernate.search.backend.elasticsearch.search.predicate.impl.ElasticsearchNestedPredicate;
import org.hibernate.search.engine.search.common.spi.SearchQueryElementTypeKey;
import org.hibernate.search.backend.elasticsearch.search.predicate.impl.ElasticsearchExistsPredicate;
import org.hibernate.search.engine.search.predicate.spi.PredicateTypeKeys;
Expand All @@ -36,14 +37,6 @@ public class ElasticsearchIndexSchemaObjectFieldNode extends AbstractElasticsear

private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() );

private static final Map<SearchQueryElementTypeKey<?>, ElasticsearchSearchCompositeIndexSchemaElementQueryElementFactory<?>>
DEFAULT_QUERY_ELEMENT_FACTORIES;
static {
Map<SearchQueryElementTypeKey<?>, ElasticsearchSearchCompositeIndexSchemaElementQueryElementFactory<?>> map = new HashMap<>();
map.put( PredicateTypeKeys.EXISTS, new ElasticsearchExistsPredicate.ObjectFieldFactory() );
DEFAULT_QUERY_ELEMENT_FACTORIES = Collections.unmodifiableMap( map );
}

private final List<String> nestedPathHierarchy;

private final ObjectStructure structure;
Expand All @@ -67,13 +60,12 @@ public ElasticsearchIndexSchemaObjectFieldNode(ElasticsearchIndexSchemaObjectNod
this.structure = ObjectStructure.DEFAULT.equals( structure ) ? ObjectStructure.FLATTENED : structure;
// We expect the children to be added to the list externally, just after the constructor call.
this.staticChildrenByName = Collections.unmodifiableMap( notYetInitializedStaticChildren );
if ( queryElementFactories.isEmpty() ) {
this.queryElementFactories = DEFAULT_QUERY_ELEMENT_FACTORIES;
}
else {
this.queryElementFactories = new HashMap<>( DEFAULT_QUERY_ELEMENT_FACTORIES );
this.queryElementFactories.putAll( queryElementFactories );
this.queryElementFactories = new HashMap<>();
this.queryElementFactories.put( PredicateTypeKeys.EXISTS, new ElasticsearchExistsPredicate.ObjectFieldFactory() );
if ( ObjectStructure.NESTED.equals( structure ) ) {
this.queryElementFactories.put( PredicateTypeKeys.NESTED, new ElasticsearchNestedPredicate.Factory() );
}
this.queryElementFactories.putAll( queryElementFactories );
}

@Override
Expand Down
Expand Up @@ -6,18 +6,19 @@
*/
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.AbstractElasticsearchSearchCompositeIndexSchemaElementQueryElementFactory;
import org.hibernate.search.backend.elasticsearch.search.impl.ElasticsearchSearchCompositeIndexSchemaElementContext;
import org.hibernate.search.backend.elasticsearch.search.impl.ElasticsearchSearchContext;
import org.hibernate.search.engine.search.predicate.SearchPredicate;
import org.hibernate.search.engine.search.predicate.spi.NestedPredicateBuilder;

import com.google.gson.JsonObject;


class ElasticsearchNestedPredicate extends AbstractElasticsearchSingleFieldPredicate {
public class ElasticsearchNestedPredicate extends AbstractElasticsearchSingleFieldPredicate {

private static final JsonAccessor<String> PATH_ACCESSOR = JsonAccessor.root().property( "path" ).asString();
private static final JsonAccessor<JsonObject> QUERY_ACCESSOR = JsonAccessor.root().property( "query" ).asObject();
Expand All @@ -37,7 +38,8 @@ protected JsonObject doToJsonQuery(PredicateRequestContext context, JsonObject o
PredicateRequestContext nestedContext = context.withNestedPath( absoluteFieldPath );

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

return outerObject;
}
Expand All @@ -54,18 +56,28 @@ static void wrap(Set<String> indexNames, String absoluteFieldPath,
outerObject.add( "nested", innerObject );
}

static class Builder extends AbstractBuilder implements NestedPredicateBuilder {
public static class Factory
extends AbstractElasticsearchSearchCompositeIndexSchemaElementQueryElementFactory<NestedPredicateBuilder> {
@Override
public NestedPredicateBuilder create(ElasticsearchSearchContext searchContext,
ElasticsearchSearchCompositeIndexSchemaElementContext field) {
return new Builder( searchContext, field );
}
}

private static class Builder extends AbstractBuilder implements NestedPredicateBuilder {
private ElasticsearchSearchPredicate nestedPredicate;

Builder(ElasticsearchSearchContext searchContext, String absoluteFieldPath,
List<String> nestedPathHierarchy) {
// The given list includes absoluteFieldPath at the end, but here we don't want it to be included.
super( searchContext, absoluteFieldPath, nestedPathHierarchy.subList( 0, nestedPathHierarchy.size() - 1 ) );
Builder(ElasticsearchSearchContext searchContext, ElasticsearchSearchCompositeIndexSchemaElementContext field) {
super( searchContext, field.absolutePath(),
// nestedPathHierarchy includes absoluteFieldPath at the end, but here we don't want it to be included.
field.nestedPathHierarchy().subList( 0, field.nestedPathHierarchy().size() - 1 ) );
}

@Override
public void nested(SearchPredicate nestedPredicate) {
ElasticsearchSearchPredicate elasticsearchPredicate = ElasticsearchSearchPredicate.from( searchContext, nestedPredicate );
ElasticsearchSearchPredicate elasticsearchPredicate = ElasticsearchSearchPredicate.from(
searchContext, nestedPredicate );
elasticsearchPredicate.checkNestableWithin( absoluteFieldPath );
this.nestedPredicate = elasticsearchPredicate;
}
Expand Down
Expand Up @@ -134,8 +134,7 @@ public NestedPredicateBuilder nested(String absoluteFieldPath) {
throw log.nonNestedFieldForNestedQuery( absoluteFieldPath,
EventContexts.fromIndexNames( indexes.hibernateSearchIndexNames() ) );
}
return new ElasticsearchNestedPredicate.Builder( searchContext, absoluteFieldPath,
field.nestedPathHierarchy() );
return field.queryElement( PredicateTypeKeys.NESTED, searchContext );
}

@Override
Expand Down
Expand Up @@ -15,6 +15,7 @@
import org.hibernate.search.backend.lucene.search.impl.LuceneSearchCompositeIndexSchemaElementContext;
import org.hibernate.search.backend.lucene.search.impl.LuceneSearchCompositeIndexSchemaElementQueryElementFactory;
import org.hibernate.search.backend.lucene.search.impl.LuceneSearchContext;
import org.hibernate.search.backend.lucene.search.predicate.impl.LuceneNestedPredicate;
import org.hibernate.search.engine.search.common.spi.SearchQueryElementTypeKey;
import org.hibernate.search.engine.search.predicate.spi.PredicateTypeKeys;
import org.hibernate.search.backend.lucene.types.predicate.impl.LuceneObjectExistsPredicate;
Expand All @@ -31,14 +32,6 @@ public class LuceneIndexSchemaObjectFieldNode extends AbstractLuceneIndexSchemaF
implements IndexObjectFieldDescriptor, LuceneIndexSchemaObjectNode,
IndexObjectFieldTypeDescriptor, LuceneSearchCompositeIndexSchemaElementContext {

private static final Map<SearchQueryElementTypeKey<?>, LuceneSearchCompositeIndexSchemaElementQueryElementFactory<?>>
DEFAULT_QUERY_ELEMENT_FACTORIES;
static {
Map<SearchQueryElementTypeKey<?>, LuceneSearchCompositeIndexSchemaElementQueryElementFactory<?>> map = new HashMap<>();
map.put( PredicateTypeKeys.EXISTS, LuceneObjectExistsPredicate.Factory.INSTANCE );
DEFAULT_QUERY_ELEMENT_FACTORIES = Collections.unmodifiableMap( map );
}

private final List<String> nestedPathHierarchy;

private final ObjectStructure structure;
Expand All @@ -61,13 +54,12 @@ public LuceneIndexSchemaObjectFieldNode(LuceneIndexSchemaObjectNode parent, Stri
this.structure = structure;
// We expect the children to be added to the list externally, just after the constructor call.
this.staticChildrenByName = Collections.unmodifiableMap( notYetInitializedStaticChildren );
if ( queryElementFactories.isEmpty() ) {
this.queryElementFactories = DEFAULT_QUERY_ELEMENT_FACTORIES;
}
else {
this.queryElementFactories = new HashMap<>( DEFAULT_QUERY_ELEMENT_FACTORIES );
this.queryElementFactories.putAll( queryElementFactories );
this.queryElementFactories = new HashMap<>();
this.queryElementFactories.put( PredicateTypeKeys.EXISTS, LuceneObjectExistsPredicate.Factory.INSTANCE );
if ( ObjectStructure.NESTED.equals( structure ) ) {
this.queryElementFactories.put( PredicateTypeKeys.NESTED, LuceneNestedPredicate.Factory.INSTANCE );
}
this.queryElementFactories.putAll( queryElementFactories );
}

@Override
Expand Down
Expand Up @@ -6,9 +6,9 @@
*/
package org.hibernate.search.backend.lucene.search.predicate.impl;

import java.util.List;

import org.hibernate.search.backend.lucene.lowlevel.query.impl.Queries;
import org.hibernate.search.backend.lucene.search.impl.AbstractLuceneSearchCompositeIndexSchemaElementQueryElementFactory;
import org.hibernate.search.backend.lucene.search.impl.LuceneSearchCompositeIndexSchemaElementContext;
import org.hibernate.search.backend.lucene.search.impl.LuceneSearchContext;
import org.hibernate.search.engine.search.predicate.SearchPredicate;
import org.hibernate.search.engine.search.predicate.spi.NestedPredicateBuilder;
Expand All @@ -20,7 +20,7 @@
import org.apache.lucene.search.join.ScoreMode;
import org.apache.lucene.search.join.ToParentBlockJoinQuery;

class LuceneNestedPredicate extends AbstractLuceneSingleFieldPredicate {
public class LuceneNestedPredicate extends AbstractLuceneSingleFieldPredicate {

private final LuceneSearchPredicate nestedPredicate;

Expand Down Expand Up @@ -54,13 +54,26 @@ public static Query createNestedQuery(String parentNestedDocumentPath, String ne
return new ToParentBlockJoinQuery( childQueryBuilder.build(), parentFilter, ScoreMode.Avg );
}

static class Builder extends AbstractBuilder implements NestedPredicateBuilder {
public static class Factory
extends AbstractLuceneSearchCompositeIndexSchemaElementQueryElementFactory<NestedPredicateBuilder> {
public static final Factory INSTANCE = new Factory();

private Factory() {
}

@Override
public NestedPredicateBuilder create(LuceneSearchContext searchContext, LuceneSearchCompositeIndexSchemaElementContext field) {
return new Builder( searchContext, field );
}
}

private static class Builder extends AbstractBuilder implements NestedPredicateBuilder {
private LuceneSearchPredicate nestedPredicate;

Builder(LuceneSearchContext searchContext, String absoluteFieldPath,
List<String> nestedPathHierarchy) {
// The given list includes absoluteFieldPath at the end, but here we don't want it to be included.
super( searchContext, absoluteFieldPath, nestedPathHierarchy.subList( 0, nestedPathHierarchy.size() - 1 ) );
Builder(LuceneSearchContext searchContext, LuceneSearchCompositeIndexSchemaElementContext field) {
super( searchContext, field.absolutePath(),
// nestedPathHierarchy includes absoluteFieldPath at the end, but here we don't want it to be included.
field.nestedPathHierarchy().subList( 0, field.nestedPathHierarchy().size() - 1 ) );
}

@Override
Expand Down
Expand Up @@ -136,8 +136,7 @@ public NestedPredicateBuilder nested(String absoluteFieldPath) {
throw log.nonNestedFieldForNestedQuery( absoluteFieldPath,
EventContexts.fromIndexNames( indexes.indexNames() ) );
}
return new LuceneNestedPredicate.Builder( searchContext, absoluteFieldPath,
field.nestedPathHierarchy() );
return field.queryElement( PredicateTypeKeys.NESTED, searchContext );
}

@Override
Expand Down
Expand Up @@ -21,6 +21,7 @@ public static SearchQueryElementTypeKey<NamedPredicateBuilder> named(String name
return key( "named:" + name );
}

public static final SearchQueryElementTypeKey<NestedPredicateBuilder> NESTED = key( "nested" );
public static final SearchQueryElementTypeKey<MatchPredicateBuilder> MATCH = key( "match" );
public static final SearchQueryElementTypeKey<RangePredicateBuilder> RANGE = key( "range" );
public static final SearchQueryElementTypeKey<ExistsPredicateBuilder> EXISTS = key( "exists" );
Expand Down

0 comments on commit 2752b4d

Please sign in to comment.