Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HSEARCH-4001 Document ReindexOnUpdate.SHALLOW/NO
- Loading branch information
Showing
13 changed files
with
729 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
...h/documentation/mapper/orm/reindexing/reindexonupdate/no/correct/ReindexOnUpdateNoIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* Hibernate Search, full-text search for your domain model | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.search.documentation.mapper.orm.reindexing.reindexonupdate.no.correct; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.List; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.EntityManagerFactory; | ||
|
||
import org.hibernate.search.documentation.testsupport.BackendConfigurations; | ||
import org.hibernate.search.documentation.testsupport.DocumentationSetupHelper; | ||
import org.hibernate.search.mapper.orm.Search; | ||
import org.hibernate.search.mapper.pojo.automaticindexing.ReindexOnUpdate; | ||
import org.hibernate.search.mapper.pojo.mapping.definition.programmatic.TypeMappingStep; | ||
import org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmUtils; | ||
|
||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
@RunWith(Parameterized.class) | ||
public class ReindexOnUpdateNoIT { | ||
|
||
@Parameterized.Parameters(name = "{0}") | ||
public static List<?> params() { | ||
return DocumentationSetupHelper.testParamsForBothAnnotationsAndProgrammatic( | ||
BackendConfigurations.simple(), | ||
mapping -> { | ||
//tag::programmatic[] | ||
TypeMappingStep sensorMapping = mapping.type( Sensor.class ); | ||
sensorMapping.indexed(); | ||
sensorMapping.property( "name" ) | ||
.fullTextField(); | ||
sensorMapping.property( "status" ) | ||
.keywordField(); | ||
sensorMapping.property( "rollingAverage" ) | ||
.genericField() | ||
.indexingDependency().reindexOnUpdate( ReindexOnUpdate.NO ); | ||
//end::programmatic[] | ||
} ); | ||
} | ||
|
||
@Parameterized.Parameter | ||
@Rule | ||
public DocumentationSetupHelper setupHelper; | ||
|
||
private EntityManagerFactory entityManagerFactory; | ||
|
||
@Before | ||
public void setup() { | ||
entityManagerFactory = setupHelper.start().setup( Sensor.class ); | ||
} | ||
|
||
@Test | ||
public void reindexOnUpdateNo() { | ||
OrmUtils.withinJPATransaction( entityManagerFactory, entityManager -> { | ||
for ( int i = 0 ; i < 2000 ; ++i ) { | ||
Sensor sensor = new Sensor(); | ||
sensor.setId( i ); | ||
sensor.setName( "Sensor " + i ); | ||
sensor.setStatus( SensorStatus.ONLINE ); | ||
sensor.setValue( 1.0 ); | ||
sensor.setRollingAverage( 1.0 ); | ||
entityManager.persist( sensor ); | ||
} | ||
} ); | ||
|
||
OrmUtils.withinJPATransaction( entityManagerFactory, entityManager -> { | ||
assertThat( countSensorsWithinOperatingParameters( entityManager ) ) | ||
.isEqualTo( 2000L ); | ||
} ); | ||
|
||
OrmUtils.withinJPATransaction( entityManagerFactory, entityManager -> { | ||
Sensor sensor = entityManager.getReference( Sensor.class, 50 ); | ||
sensor.setStatus( SensorStatus.OFFLINE ); | ||
} ); | ||
|
||
OrmUtils.withinJPATransaction( entityManagerFactory, entityManager -> { | ||
// The sensor was reindexed, as expected. | ||
assertThat( countSensorsWithinOperatingParameters( entityManager ) ) | ||
.isEqualTo( 1999L ); | ||
} ); | ||
|
||
OrmUtils.withinJPATransaction( entityManagerFactory, entityManager -> { | ||
Sensor sensor = entityManager.getReference( Sensor.class, 70 ); | ||
sensor.setRollingAverage( 0.5 ); | ||
} ); | ||
|
||
OrmUtils.withinJPATransaction( entityManagerFactory, entityManager -> { | ||
// The sensor was *not* been reindexed, as expected. | ||
assertThat( countSensorsWithinOperatingParameters( entityManager ) ) | ||
.isEqualTo( 1999L ); | ||
} ); | ||
|
||
OrmUtils.withinJPATransaction( entityManagerFactory, entityManager -> { | ||
Sensor sensor = entityManager.getReference( Sensor.class, 70 ); | ||
Search.session( entityManager ).indexingPlan().addOrUpdate( sensor ); | ||
} ); | ||
|
||
OrmUtils.withinJPATransaction( entityManagerFactory, entityManager -> { | ||
// The sensor was reindexed, as expected. | ||
assertThat( countSensorsWithinOperatingParameters( entityManager ) ) | ||
.isEqualTo( 1998L ); | ||
} ); | ||
} | ||
|
||
private long countSensorsWithinOperatingParameters(EntityManager entityManager) { | ||
return Search.session( entityManager ).search( Sensor.class ) | ||
.where( f -> f.bool() | ||
.must( f.match().field( "status" ).matching( SensorStatus.ONLINE ) ) | ||
.must( f.range().field( "rollingAverage" ).between( 0.9, 1.1 ) ) ) | ||
.fetchTotalHitCount(); | ||
} | ||
|
||
} |
87 changes: 87 additions & 0 deletions
87
...bernate/search/documentation/mapper/orm/reindexing/reindexonupdate/no/correct/Sensor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Hibernate Search, full-text search for your domain model | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.search.documentation.mapper.orm.reindexing.reindexonupdate.no.correct; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
|
||
import org.hibernate.search.mapper.pojo.automaticindexing.ReindexOnUpdate; | ||
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.FullTextField; | ||
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.GenericField; | ||
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed; | ||
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.IndexingDependency; | ||
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.KeywordField; | ||
|
||
//tag::include[] | ||
@Entity | ||
@Indexed | ||
public class Sensor { | ||
|
||
@Id | ||
private Integer id; | ||
|
||
@FullTextField | ||
private String name; | ||
|
||
@KeywordField | ||
private SensorStatus status; | ||
|
||
private double value; | ||
|
||
@GenericField | ||
@IndexingDependency(reindexOnUpdate = ReindexOnUpdate.NO) // <1> | ||
private double rollingAverage; | ||
|
||
public Sensor() { | ||
} | ||
|
||
// Getters and setters | ||
// ... | ||
|
||
//tag::getters-setters[] | ||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public SensorStatus getStatus() { | ||
return status; | ||
} | ||
|
||
public void setStatus(SensorStatus status) { | ||
this.status = status; | ||
} | ||
|
||
public double getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(double value) { | ||
this.value = value; | ||
} | ||
|
||
public double getRollingAverage() { | ||
return rollingAverage; | ||
} | ||
|
||
public void setRollingAverage(double rollingAverage) { | ||
this.rollingAverage = rollingAverage; | ||
} | ||
//end::getters-setters[] | ||
} | ||
//end::include[] |
Oops, something went wrong.