Skip to content

Commit 91a2df2

Browse files
committed
HSEARCH-3909 Allow looking up available predicates/sorts/projections/aggregations for each field in the metamodel
1 parent d7e9595 commit 91a2df2

File tree

42 files changed

+856
-117
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+856
-117
lines changed

backend/elasticsearch/src/main/java/org/hibernate/search/backend/elasticsearch/search/predicate/impl/ElasticsearchPredicateTypeKeys.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@
77
package org.hibernate.search.backend.elasticsearch.search.predicate.impl;
88

99
import org.hibernate.search.backend.elasticsearch.types.predicate.impl.ElasticsearchSimpleQueryStringPredicateBuilderFieldState;
10+
import org.hibernate.search.engine.backend.types.IndexFieldTraits;
1011
import org.hibernate.search.engine.search.common.spi.SearchQueryElementTypeKey;
1112

1213
public final class ElasticsearchPredicateTypeKeys {
1314

1415
private ElasticsearchPredicateTypeKeys() {
1516
}
1617

17-
private static <T> SearchQueryElementTypeKey<T> key(String name) {
18-
return SearchQueryElementTypeKey.of( "predicate", name );
19-
}
20-
2118
public static final SearchQueryElementTypeKey<
2219
ElasticsearchSimpleQueryStringPredicateBuilderFieldState> SIMPLE_QUERY_STRING =
23-
key( "simple-query-string" );
20+
SearchQueryElementTypeKey.of( IndexFieldTraits.Predicates.SIMPLE_QUERY_STRING );
2421

2522
}

backend/elasticsearch/src/main/java/org/hibernate/search/backend/elasticsearch/search/projection/impl/ElasticsearchProjectionTypeKeys.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
package org.hibernate.search.backend.elasticsearch.search.projection.impl;
88

9-
import static org.hibernate.search.engine.search.projection.spi.ProjectionTypeKeys.key;
9+
import static org.hibernate.search.engine.search.common.spi.SearchQueryElementTypeKey.of;
1010

1111
import org.hibernate.search.engine.search.common.spi.SearchQueryElementTypeKey;
1212

@@ -15,8 +15,8 @@ public final class ElasticsearchProjectionTypeKeys {
1515
private ElasticsearchProjectionTypeKeys() {
1616
}
1717

18-
public static final SearchQueryElementTypeKey<?> JSON_HIT = key( "json-hit" );
19-
public static final SearchQueryElementTypeKey<?> SOURCE = key( "source" );
20-
public static final SearchQueryElementTypeKey<?> EXPLANATION = key( "explanation" );
18+
public static final SearchQueryElementTypeKey<?> JSON_HIT = of( "projection:json-hit" );
19+
public static final SearchQueryElementTypeKey<?> SOURCE = of( "projection:source" );
20+
public static final SearchQueryElementTypeKey<?> EXPLANATION = of( "projection:explanation" );
2121

2222
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Hibernate Search, full-text search for your domain model
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.search.backend.elasticsearch.types;
8+
9+
import org.hibernate.search.engine.backend.types.IndexFieldTraits;
10+
11+
/**
12+
* Constants for the names of Elasticsearch-specific traits that can be exposed by index fields.
13+
* <p>
14+
* Currently empty, because there are no Elasticsearch-specific traits.
15+
*
16+
* @see IndexFieldTraits
17+
*/
18+
public final class ElasticsearchIndexFieldTraits {
19+
20+
private ElasticsearchIndexFieldTraits() {
21+
}
22+
23+
}

backend/lucene/src/main/java/org/hibernate/search/backend/lucene/search/predicate/impl/LucenePredicateTypeKeys.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@
66
*/
77
package org.hibernate.search.backend.lucene.search.predicate.impl;
88

9+
import static org.hibernate.search.engine.search.common.spi.SearchQueryElementTypeKey.of;
10+
911
import org.hibernate.search.backend.lucene.types.predicate.impl.LuceneSimpleQueryStringPredicateBuilderFieldState;
12+
import org.hibernate.search.engine.backend.types.IndexFieldTraits;
1013
import org.hibernate.search.engine.search.common.spi.SearchQueryElementTypeKey;
11-
import org.hibernate.search.engine.search.predicate.spi.PredicateTypeKeys;
1214

1315
public final class LucenePredicateTypeKeys {
1416

1517
private LucenePredicateTypeKeys() {
1618
}
1719

1820
public static final SearchQueryElementTypeKey<LuceneSimpleQueryStringPredicateBuilderFieldState> SIMPLE_QUERY_STRING =
19-
PredicateTypeKeys.key( "simple-query-string" );
21+
of( IndexFieldTraits.Predicates.SIMPLE_QUERY_STRING );
2022

2123
}

backend/lucene/src/main/java/org/hibernate/search/backend/lucene/search/projection/impl/LuceneProjectionTypeKeys.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
package org.hibernate.search.backend.lucene.search.projection.impl;
88

9-
import static org.hibernate.search.engine.search.projection.spi.ProjectionTypeKeys.key;
9+
import static org.hibernate.search.engine.search.common.spi.SearchQueryElementTypeKey.of;
1010

1111
import org.hibernate.search.engine.search.common.spi.SearchQueryElementTypeKey;
1212

@@ -15,7 +15,7 @@ public final class LuceneProjectionTypeKeys {
1515
private LuceneProjectionTypeKeys() {
1616
}
1717

18-
public static final SearchQueryElementTypeKey<?> EXPLANATION = key( "explanation" );
19-
public static final SearchQueryElementTypeKey<?> DOCUMENT = key( "document" );
18+
public static final SearchQueryElementTypeKey<?> EXPLANATION = of( "projection:explanation" );
19+
public static final SearchQueryElementTypeKey<?> DOCUMENT = of( "projection:document" );
2020

2121
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Hibernate Search, full-text search for your domain model
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.search.backend.lucene.types;
8+
9+
import org.hibernate.search.engine.backend.types.IndexFieldTraits;
10+
11+
/**
12+
* Constants for the names of Lucene-specific traits that can be exposed by index fields.
13+
* <p>
14+
* Currently empty, because there are no Lucene-specific traits.
15+
*
16+
* @see IndexFieldTraits
17+
*/
18+
public final class LuceneIndexFieldTraits {
19+
20+
private LuceneIndexFieldTraits() {
21+
}
22+
23+
24+
}

documentation/src/main/asciidoc/public/reference/_mapping-inspect.adoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ is it searchable, sortable, projectable,
4848
what is the expected java class for arguments to the <<search-dsl,Search DSL>>,
4949
what are the analyzers/normalizer set on this field,
5050
...
51-
<9> Object fields can also be inspected.
51+
<9> Inspect the "traits" of a field type:
52+
each trait represents a predicate/sort/projection/aggregation
53+
that can be used on fields of that type.
54+
<10> Object fields can also be inspected.
5255
====
5356

5457
[TIP]

documentation/src/test/java/org/hibernate/search/documentation/mapper/orm/indexedentities/SearchMappingIndexedEntitiesIT.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
package org.hibernate.search.documentation.mapper.orm.indexedentities;
88

99
import static org.assertj.core.api.Assertions.assertThat;
10+
import static org.assertj.core.api.Assertions.fail;
1011

1112
import java.sql.Date;
1213
import java.util.Optional;
14+
import java.util.Set;
1315

1416
import jakarta.persistence.EntityManagerFactory;
1517

@@ -22,6 +24,7 @@
2224
import org.hibernate.search.engine.backend.metamodel.IndexObjectFieldTypeDescriptor;
2325
import org.hibernate.search.engine.backend.metamodel.IndexValueFieldDescriptor;
2426
import org.hibernate.search.engine.backend.metamodel.IndexValueFieldTypeDescriptor;
27+
import org.hibernate.search.engine.backend.types.IndexFieldTraits;
2528
import org.hibernate.search.mapper.orm.Search;
2629
import org.hibernate.search.mapper.orm.entity.SearchIndexedEntity;
2730
import org.hibernate.search.mapper.orm.mapping.SearchMapping;
@@ -109,8 +112,26 @@ void indexMetamodel() {
109112
assertThat( searchAnalyzerName ).isEmpty();
110113
assertThat( normalizerName ).isEmpty();
111114
//tag::indexMetamodel[]
115+
Set<String> traits = type.traits(); // <9>
116+
if ( traits.contains( IndexFieldTraits.Aggregations.RANGE ) ) {
117+
// ...
118+
//end::indexMetamodel[]
119+
}
120+
else {
121+
fail( "The field should be aggregable!" );
122+
//tag::indexMetamodel[]
123+
}
124+
//end::indexMetamodel[]
125+
assertThat( traits ).contains(
126+
IndexFieldTraits.Predicates.EXISTS,
127+
IndexFieldTraits.Predicates.MATCH,
128+
IndexFieldTraits.Predicates.RANGE,
129+
IndexFieldTraits.Aggregations.TERMS,
130+
IndexFieldTraits.Aggregations.RANGE
131+
);
132+
//tag::indexMetamodel[]
112133
}
113-
else if ( field.isObjectField() ) { // <9>
134+
else if ( field.isObjectField() ) { // <10>
114135
IndexObjectFieldDescriptor objectField = field.toObjectField();
115136

116137
IndexObjectFieldTypeDescriptor type = objectField.type();

engine/src/main/java/org/hibernate/search/engine/backend/document/model/spi/AbstractIndexField.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
package org.hibernate.search.engine.backend.document.model.spi;
88

99
import org.hibernate.search.engine.backend.common.spi.FieldPaths;
10+
import org.hibernate.search.engine.backend.types.spi.AbstractIndexNodeType;
1011
import org.hibernate.search.engine.common.tree.spi.TreeNodeInclusion;
1112
import org.hibernate.search.engine.reporting.spi.EventContexts;
12-
import org.hibernate.search.engine.search.common.spi.SearchIndexNodeTypeContext;
1313
import org.hibernate.search.engine.search.common.spi.SearchIndexScope;
1414
import org.hibernate.search.util.common.reporting.EventContext;
1515

1616
public abstract class AbstractIndexField<
1717
S extends AbstractIndexField<S, SC, ?, C>,
1818
SC extends SearchIndexScope<?>,
19-
NT extends SearchIndexNodeTypeContext<SC, ? super S>,
19+
NT extends AbstractIndexNodeType<SC, ? super S>,
2020
C extends IndexCompositeNode<SC, ?, ?>>
2121
extends AbstractIndexNode<S, SC, NT>
2222
implements IndexField<SC, C> {

engine/src/main/java/org/hibernate/search/engine/backend/metamodel/IndexFieldDescriptor.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ public interface IndexFieldDescriptor {
3838
*/
3939
IndexValueFieldDescriptor toValueField();
4040

41+
/**
42+
* @return The type of this field, exposing its various capabilities.
43+
* @see IndexFieldTypeDescriptor
44+
*/
45+
IndexFieldTypeDescriptor type();
46+
4147
/**
4248
* @return The parent of this field, either the {@link IndexCompositeElementDescriptor#isRoot() index root}
4349
* or an {@link IndexCompositeElementDescriptor#isObjectField() object field}.

0 commit comments

Comments
 (0)