Skip to content

Commit

Permalink
HSEARCH-3752 Implement implicit nested simpleQueryString predicate in ES
Browse files Browse the repository at this point in the history
  • Loading branch information
fax4ever committed Mar 18, 2020
1 parent 1a2a1c8 commit ba1b106
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
Expand Up @@ -28,13 +28,16 @@ public AbstractElasticsearchSearchNestedPredicateBuilder(List<String> nestedPath
public final JsonObject build(ElasticsearchSearchPredicateContext context) {
JsonObject result = super.build( context );
if ( nestedPathHierarchy != null && !nestedPathHierarchy.isEmpty() ) {
result = applyImplicitNested( result, new LinkedList<>( nestedPathHierarchy ), context );
result = applyImplicitNested( result, nestedPathHierarchy, context );
}
return result;
}

private JsonObject applyImplicitNested(JsonObject partialResult, List<String> nestedPathHierarchy,
ElasticsearchSearchPredicateContext context) {
public static JsonObject applyImplicitNested(JsonObject partialResult, List<String> nestedPathHierarchy, ElasticsearchSearchPredicateContext context) {
return applyImplicitNestedStep( partialResult, new LinkedList<>( nestedPathHierarchy ), context );
}

private static JsonObject applyImplicitNestedStep(JsonObject partialResult, List<String> nestedPathHierarchy, ElasticsearchSearchPredicateContext context) {
if ( nestedPathHierarchy.isEmpty() ) {
return partialResult;
}
Expand All @@ -48,7 +51,7 @@ private JsonObject applyImplicitNested(JsonObject partialResult, List<String> ne
JsonObject innerObject = new JsonObject();

PATH_ACCESSOR.set( innerObject, lastPath );
QUERY_ACCESSOR.set( innerObject, applyImplicitNested( partialResult, nestedPathHierarchy, context ) );
QUERY_ACCESSOR.set( innerObject, applyImplicitNestedStep( partialResult, nestedPathHierarchy, context ) );

JsonObject outerObject = new JsonObject();
outerObject.add( "nested", innerObject );
Expand Down
Expand Up @@ -7,6 +7,7 @@
package org.hibernate.search.backend.elasticsearch.search.predicate.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.search.backend.elasticsearch.gson.impl.JsonAccessor;
Expand Down Expand Up @@ -111,8 +112,11 @@ protected JsonObject doBuild(ElasticsearchSearchPredicateContext context,
QUERY_ACCESSOR.set( innerObject, simpleQueryString );
DEFAULT_OPERATOR_ACCESSOR.set( innerObject, defaultOperator );

List<String> nestedPathHierarchy = null;
JsonArray fieldArray = new JsonArray();
for ( ElasticsearchSimpleQueryStringPredicateBuilderFieldState fieldContext : fields.values() ) {
// TODO: check if the fields belong **all** to the same nested object or to the root object.
nestedPathHierarchy = fieldContext.getNestedPathHierarchy( scopeModel );
fieldArray.add( fieldContext.build() );
}
FIELDS_ACCESSOR.set( innerObject, fieldArray );
Expand All @@ -133,7 +137,9 @@ protected JsonObject doBuild(ElasticsearchSearchPredicateContext context,
}

SIMPLE_QUERY_STRING_ACCESSOR.set( outerObject, innerObject );
return outerObject;

return ( nestedPathHierarchy == null || nestedPathHierarchy.isEmpty() ) ? outerObject :
AbstractElasticsearchSearchNestedPredicateBuilder.applyImplicitNested( outerObject, nestedPathHierarchy, context );
}

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

import java.util.List;

import org.hibernate.search.backend.elasticsearch.scope.model.impl.ElasticsearchScopeModel;
import org.hibernate.search.engine.search.predicate.spi.SimpleQueryStringPredicateBuilder;

import com.google.gson.JsonPrimitive;
Expand Down Expand Up @@ -33,4 +36,8 @@ public JsonPrimitive build() {
}
return new JsonPrimitive( sb.toString() );
}

public List<String> getNestedPathHierarchy(ElasticsearchScopeModel scopeModel) {
return scopeModel.getNestedPathHierarchy( absoluteFieldPath );
}
}
Expand Up @@ -31,7 +31,6 @@
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;

Expand Down Expand Up @@ -132,7 +131,6 @@ public void predicate_wildcard() {
verify_implicit_nest( p -> p.wildcard().field( "nested.text" ).matching( SOME_WILDCARD_PATTERN ) );
}

@Ignore // TODO: support this
@Test
public void predicate_simpleQueryString() {
verify_implicit_nest( p -> p.simpleQueryString().field( "nested.text" ).matching( SOME_SIMPLE_QUERY_STRING ) );
Expand Down

0 comments on commit ba1b106

Please sign in to comment.