use of org.hibernate.search.engine.backend.document.IndexFieldReference in project hibernate-search by hibernate.
the class FullNameBinder method bind.
@Override
public void bind(TypeBindingContext context) {
context.dependencies().use("firstName").use("lastName");
IndexFieldReference<String> fullNameField = context.indexSchemaElement().field("fullName", f -> f.asString().analyzer("name")).toReference();
IndexFieldReference<String> fullNameSortField = null;
if (this.sortField) {
// <2>
fullNameSortField = context.indexSchemaElement().field("fullName_sort", f -> f.asString().normalizer("name").sortable(Sortable.YES)).toReference();
}
context.bridge(Author.class, new Bridge(fullNameField, fullNameSortField));
}
use of org.hibernate.search.engine.backend.document.IndexFieldReference in project hibernate-search by hibernate.
the class FullNameBinder method bind.
@Override
public void bind(TypeBindingContext context) {
// <2>
// <3>
context.dependencies().use("firstName").use("lastName");
IndexFieldReference<String> fullNameField = // <4>
context.indexSchemaElement().field("fullName", // <5>
f -> f.asString().analyzer("name")).toReference();
// <6>
context.bridge(// <7>
Author.class, new Bridge(// <8>
fullNameField));
}
use of org.hibernate.search.engine.backend.document.IndexFieldReference in project hibernate-search by hibernate.
the class PropertyBridgeBaseIT method explicitReindexing.
/**
* Basic test checking that a "normal" custom property bridge will work as expected
* when relying on explicit reindexing declaration.
* <p>
* Note that reindexing is tested in depth in the ORM mapper integration tests.
*/
@Test
@TestForIssue(jiraKey = "HSEARCH-3297")
public void explicitReindexing() {
backendMock.expectSchema(INDEX_NAME, b -> b.field("someField", String.class, b2 -> {
// For HSEARCH-2641
b2.analyzerName("myAnalyzer");
}));
SearchMapping mapping = setupHelper.start().withConfiguration(b -> b.programmaticMapping().type(PropertyBridgeExplicitIndexingClasses.IndexedEntity.class).property("child").binder(context -> {
context.dependencies().fromOtherEntity(PropertyBridgeExplicitIndexingClasses.ContainedLevel2Entity.class, "parent").use("stringProperty");
IndexFieldReference<String> indexFieldReference = context.indexSchemaElement().field("someField", f -> f.asString().analyzer("myAnalyzer")).toReference();
context.bridge(PropertyBridgeExplicitIndexingClasses.ContainedLevel1Entity.class, (DocumentElement target, PropertyBridgeExplicitIndexingClasses.ContainedLevel1Entity bridgedElement, PropertyBridgeWriteContext context1) -> {
/*
* In a real application this would run a query,
* but we don't have the necessary infrastructure here
* so we'll cut short and just index a constant.
* We just need to know the bridge is executed anyway.
*/
target.addValue(indexFieldReference, "constant");
});
})).setup(PropertyBridgeExplicitIndexingClasses.IndexedEntity.class, PropertyBridgeExplicitIndexingClasses.ContainedLevel1Entity.class, PropertyBridgeExplicitIndexingClasses.ContainedLevel2Entity.class);
backendMock.verifyExpectationsMet();
PropertyBridgeExplicitIndexingClasses.IndexedEntity entity = new PropertyBridgeExplicitIndexingClasses.IndexedEntity();
entity.id = 1;
PropertyBridgeExplicitIndexingClasses.ContainedLevel1Entity containedLevel1Entity = new PropertyBridgeExplicitIndexingClasses.ContainedLevel1Entity();
containedLevel1Entity.parent = entity;
entity.child = containedLevel1Entity;
PropertyBridgeExplicitIndexingClasses.ContainedLevel2Entity containedLevel2Entity = new PropertyBridgeExplicitIndexingClasses.ContainedLevel2Entity();
containedLevel2Entity.parent = containedLevel1Entity;
containedLevel2Entity.stringProperty = "some string";
try (SearchSession session = mapping.createSession()) {
session.indexingPlan().add(entity);
session.indexingPlan().add(1, null, containedLevel2Entity);
backendMock.expectWorks(INDEX_NAME).add("1", b -> b.field("someField", "constant"));
}
backendMock.verifyExpectationsMet();
try (SearchSession session = mapping.createSession()) {
containedLevel2Entity.stringProperty = "some string";
session.indexingPlan().addOrUpdate(1, null, containedLevel2Entity, new String[] { "stringProperty" });
backendMock.expectWorks(INDEX_NAME).addOrUpdate("1", b -> b.field("someField", "constant"));
}
backendMock.verifyExpectationsMet();
}
use of org.hibernate.search.engine.backend.document.IndexFieldReference in project hibernate-search by hibernate.
the class PropertyBridgeBaseIT method explicitDependencies_inacessibleObject.
/**
* Check that referencing an inaccessible property through "use" will work properly.
* <p>
* Inaccessible properties are properties that we can see through reflection,
* but that we cannot retrieve a value from at runtime,
* because the call to Field.setAccessible is denied by the JVM.
* For example: Enum#name (the field, not the method).
* <p>
* Before HSEARCH-4114 was fixed, this test used to fail with the following report:
*
* <pre>{@literal
* JavaBean mapping:
* type 'org.hibernate.search.integrationtest.mapper.pojo.mapping.definition.
* PropertyBridgeBaseIT$PropertyBridgeExplicitDependenciesInaccessibleObjectClasses$IndexedEntity':
* path '.myEnum':
* failures:
* - HSEARCH700079: Exception while retrieving property type model for 'name' on
* 'org.hibernate.search.integrationtest.mapper.pojo.mapping.definition.
* PropertyBridgeBaseIT$PropertyBridgeExplicitDependenciesInaccessibleObjectClasses$MyEnum'.
* }</pre>
*/
@Test
@TestForIssue(jiraKey = "HSEARCH-3297")
public void explicitDependencies_inacessibleObject() {
backendMock.expectSchema(INDEX_NAME, b -> b.field("someField", String.class, b2 -> {
// For HSEARCH-2641
b2.analyzerName("myAnalyzer");
}));
SearchMapping mapping = setupHelper.start().withConfiguration(b -> b.programmaticMapping().type(PropertyBridgeExplicitDependenciesInaccessibleObjectClasses.IndexedEntity.class).property("myEnum").binder(context -> {
// This references the "name" field, which is not accessible
context.dependencies().use("name");
IndexFieldReference<String> indexFieldReference = context.indexSchemaElement().field("someField", f -> f.asString().analyzer("myAnalyzer")).toReference();
context.bridge(PropertyBridgeExplicitDependenciesInaccessibleObjectClasses.MyEnum.class, (DocumentElement target, PropertyBridgeExplicitDependenciesInaccessibleObjectClasses.MyEnum bridgedElement, PropertyBridgeWriteContext context1) -> {
target.addValue(indexFieldReference, bridgedElement.name());
});
})).setup(PropertyBridgeExplicitDependenciesInaccessibleObjectClasses.IndexedEntity.class);
backendMock.verifyExpectationsMet();
// If the above didn't throw any exception, we're good.
// Check that indexing works, just in case...
PropertyBridgeExplicitDependenciesInaccessibleObjectClasses.IndexedEntity entity = new PropertyBridgeExplicitDependenciesInaccessibleObjectClasses.IndexedEntity();
entity.id = 1;
entity.myEnum = PropertyBridgeExplicitDependenciesInaccessibleObjectClasses.MyEnum.VALUE1;
try (SearchSession session = mapping.createSession()) {
session.indexingPlan().add(entity);
backendMock.expectWorks(INDEX_NAME).add("1", b -> b.field("someField", "VALUE1"));
}
backendMock.verifyExpectationsMet();
}
use of org.hibernate.search.engine.backend.document.IndexFieldReference in project hibernate-search by hibernate.
the class PropertyBridgeBaseIT method accessors.
/**
* Basic test checking that a "normal" custom property bridge will work as expected
* when relying on accessors.
* <p>
* Note that reindexing is tested in depth in the ORM mapper integration tests.
*/
@Test
@TestForIssue(jiraKey = { "HSEARCH-2055", "HSEARCH-2641" })
public void accessors() {
@Indexed(index = INDEX_NAME)
class IndexedEntity {
@DocumentId
Integer id;
String stringProperty;
}
backendMock.expectSchema(INDEX_NAME, b -> b.field("someField", String.class, b2 -> {
// For HSEARCH-2641
b2.analyzerName("myAnalyzer");
}));
SearchMapping mapping = setupHelper.start().withConfiguration(b -> b.programmaticMapping().type(IndexedEntity.class).property("stringProperty").binder(context -> {
PojoElementAccessor<String> pojoPropertyAccessor = context.bridgedElement().createAccessor(String.class);
IndexFieldReference<String> indexFieldReference = context.indexSchemaElement().field("someField", f -> f.asString().analyzer("myAnalyzer")).toReference();
context.bridge((DocumentElement target, Object bridgedElement, PropertyBridgeWriteContext context1) -> {
target.addValue(indexFieldReference, pojoPropertyAccessor.read(bridgedElement));
});
})).setup(IndexedEntity.class);
backendMock.verifyExpectationsMet();
IndexedEntity entity = new IndexedEntity();
entity.id = 1;
entity.stringProperty = "some string";
try (SearchSession session = mapping.createSession()) {
session.indexingPlan().add(entity);
backendMock.expectWorks(INDEX_NAME).add("1", b -> b.field("someField", entity.stringProperty));
}
backendMock.verifyExpectationsMet();
try (SearchSession session = mapping.createSession()) {
entity.stringProperty = "some string 2";
session.indexingPlan().addOrUpdate(entity, new String[] { "stringProperty" });
backendMock.expectWorks(INDEX_NAME).addOrUpdate("1", b -> b.field("someField", entity.stringProperty));
}
backendMock.verifyExpectationsMet();
}
Aggregations