-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HSEARCH-3711 Document the index field type DSL extension for Elastics…
…earch
- Loading branch information
Showing
5 changed files
with
223 additions
and
2 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
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
48 changes: 48 additions & 0 deletions
48
...org/hibernate/search/documentation/backend/elasticsearch/type/asnative/CompanyServer.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,48 @@ | ||
/* | ||
* 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.backend.elasticsearch.type.asnative; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
|
||
import org.hibernate.search.mapper.pojo.bridge.mapping.annotation.ValueBinderRef; | ||
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed; | ||
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.NonStandardField; | ||
|
||
// tag::include[] | ||
@Entity | ||
@Indexed | ||
public class CompanyServer { | ||
|
||
@Id | ||
@GeneratedValue | ||
private Integer id; | ||
|
||
@NonStandardField( // <1> | ||
valueBinder = @ValueBinderRef(type = IpAddressValueBinder.class) // <2> | ||
) | ||
private String ipAddress; | ||
|
||
// Getters and setters | ||
// ... | ||
|
||
// tag::getters-setters[] | ||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public String getIpAddress() { | ||
return ipAddress; | ||
} | ||
|
||
public void setIpAddress(String ipAddress) { | ||
this.ipAddress = ipAddress; | ||
} | ||
// end::getters-setters[] | ||
} | ||
// end::include[] |
70 changes: 70 additions & 0 deletions
70
...e/search/documentation/backend/elasticsearch/type/asnative/ElasticsearchNativeTypeIT.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,70 @@ | ||
/* | ||
* 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.backend.elasticsearch.type.asnative; | ||
|
||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.List; | ||
import javax.persistence.EntityManagerFactory; | ||
|
||
import org.hibernate.search.backend.elasticsearch.ElasticsearchExtension; | ||
import org.hibernate.search.documentation.testsupport.ElasticsearchBackendConfiguration; | ||
import org.hibernate.search.mapper.orm.Search; | ||
import org.hibernate.search.mapper.orm.automaticindexing.AutomaticIndexingSynchronizationStrategyName; | ||
import org.hibernate.search.mapper.orm.cfg.HibernateOrmMapperSettings; | ||
import org.hibernate.search.mapper.orm.session.SearchSession; | ||
import org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmSetupHelper; | ||
import org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmUtils; | ||
|
||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
public class ElasticsearchNativeTypeIT { | ||
@Rule | ||
public OrmSetupHelper setupHelper = OrmSetupHelper.withSingleBackend( new ElasticsearchBackendConfiguration() ); | ||
|
||
private EntityManagerFactory entityManagerFactory; | ||
|
||
@Before | ||
public void setup() { | ||
entityManagerFactory = setupHelper.start() | ||
.withProperty( | ||
HibernateOrmMapperSettings.AUTOMATIC_INDEXING_SYNCHRONIZATION_STRATEGY, | ||
AutomaticIndexingSynchronizationStrategyName.SEARCHABLE | ||
) | ||
.setup( CompanyServer.class ); | ||
} | ||
|
||
@Test | ||
public void smoke() { | ||
OrmUtils.withinJPATransaction( entityManagerFactory, entityManager -> { | ||
CompanyServer companyServer = new CompanyServer(); | ||
companyServer.setIpAddress( "192.168.10.2" ); | ||
entityManager.persist( companyServer ); | ||
} ); | ||
|
||
OrmUtils.withinJPATransaction( entityManagerFactory, entityManager -> { | ||
SearchSession searchSession = Search.session( entityManager ); | ||
|
||
List<CompanyServer> result = searchSession.search( CompanyServer.class ) | ||
.extension( ElasticsearchExtension.get() ) | ||
.predicate( f -> f.fromJson( | ||
"{\n" + | ||
" \"term\": {\n" + | ||
" \"ipAddress\": \"192.168.0.0/16\"\n" + | ||
" }\n" + | ||
"}" | ||
) ) | ||
.fetchHits( 20 ); | ||
|
||
assertThat( result ).hasSize( 1 ); | ||
} ); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
...ernate/search/documentation/backend/elasticsearch/type/asnative/IpAddressValueBinder.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,47 @@ | ||
/* | ||
* 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.backend.elasticsearch.type.asnative; | ||
|
||
import org.hibernate.search.backend.elasticsearch.ElasticsearchExtension; | ||
import org.hibernate.search.mapper.pojo.bridge.ValueBridge; | ||
import org.hibernate.search.mapper.pojo.bridge.binding.ValueBindingContext; | ||
import org.hibernate.search.mapper.pojo.bridge.mapping.programmatic.ValueBinder; | ||
import org.hibernate.search.mapper.pojo.bridge.runtime.ValueBridgeFromIndexedValueContext; | ||
import org.hibernate.search.mapper.pojo.bridge.runtime.ValueBridgeToIndexedValueContext; | ||
|
||
import com.google.gson.Gson; | ||
|
||
//tag::include[] | ||
public class IpAddressValueBinder implements ValueBinder { // <1> | ||
@Override | ||
public void bind(ValueBindingContext<?> context) { | ||
context.setBridge( | ||
String.class, | ||
new IpAddressValueBridge(), | ||
context.getTypeFactory() // <2> | ||
.extension( ElasticsearchExtension.get() ) // <3> | ||
.asNative( // <4> | ||
"{\"type\": \"ip\"}" // <5> | ||
) | ||
); | ||
} | ||
|
||
private static class IpAddressValueBridge implements ValueBridge<String, String> { | ||
private static final Gson GSON = new Gson(); | ||
|
||
@Override | ||
public String toIndexedValue(String value, ValueBridgeToIndexedValueContext context) { | ||
return value == null ? null : GSON.toJson( value ); // <6> | ||
} | ||
|
||
@Override | ||
public String fromIndexedValue(String value, ValueBridgeFromIndexedValueContext context) { | ||
return value == null ? null : GSON.fromJson( value, String.class ); // <7> | ||
} | ||
} | ||
} | ||
//end::include[] |