Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add an example using dynamic properties

  • Loading branch information...
commit d90204356f147c51cbe581bb932fd317fbf69613 1 parent 55bd781
@Sanne Sanne authored
View
59 hibernate-search-engine/src/test/java/org/hibernate/search/test/bridge/DynamicIndexedValueHolder.java
@@ -0,0 +1,59 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2013, Red Hat, Inc. and/or its affiliates or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat, Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.search.test.bridge;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.hibernate.search.annotations.Analyze;
+import org.hibernate.search.annotations.DocumentId;
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.FieldBridge;
+import org.hibernate.search.annotations.Indexed;
+import org.hibernate.search.annotations.Norms;
+import org.hibernate.search.annotations.Store;
+
+/**
+ * @author Sanne Grinovero <sanne@hibernate.org> (C) 2013 Red Hat Inc.
+ */
+@Indexed(index="all")
+public class DynamicIndexedValueHolder {
+
+ @DocumentId
+ public final String id;
+
+ @Field(norms = Norms.NO, store = Store.YES, analyze = Analyze.YES, name="value")
+ @FieldBridge(impl = MultiFieldMapBridge.class)
+ private Map<String, String> values = new HashMap<String, String>();
+
+ public DynamicIndexedValueHolder(String id) {
+ this.id = id;
+ }
+
+ public DynamicIndexedValueHolder property(String key, String value) {
+ values.put( key, value );
+ return this;
+ }
+
+}
View
54 hibernate-search-engine/src/test/java/org/hibernate/search/test/bridge/MultiFieldMapBridge.java
@@ -0,0 +1,54 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2013, Red Hat, Inc. and/or its affiliates or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat, Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.search.test.bridge;
+
+import java.util.Map;
+
+import org.apache.lucene.document.Document;
+import org.hibernate.search.bridge.FieldBridge;
+import org.hibernate.search.bridge.LuceneOptions;
+
+/**
+ * @author Sanne Grinovero <sanne@hibernate.org> (C) 2013 Red Hat Inc.
+ */
+public class MultiFieldMapBridge implements FieldBridge {
+
+ @Override
+ public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
+ if ( ! ( value instanceof Map ) ) {
+ throw new IllegalArgumentException( "This field can only be applied on a Map type field" );
+ }
+ else {
+ Map<Object,Object> userValue = (Map) value;
+ for ( Map.Entry<Object,Object> e : userValue.entrySet() ) {
+ setField( name, String.valueOf( e.getKey() ), String.valueOf( e.getValue() ), document, luceneOptions );
+ }
+ }
+ }
+
+ private void setField(String fieldPrefix, String key, String value, Document document, LuceneOptions luceneOptions) {
+ luceneOptions.addFieldToDocument( fieldPrefix + "." + key, value, document );
+ }
+
+}
View
84 hibernate-search-engine/src/test/java/org/hibernate/search/test/bridge/PropertiesExampleBridgeTest.java
@@ -0,0 +1,84 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2013, Red Hat, Inc. and/or its affiliates or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat, Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.search.test.bridge;
+
+import java.util.Arrays;
+import java.util.List;
+
+import junit.framework.Assert;
+
+import org.apache.lucene.search.Query;
+import org.hibernate.search.backend.spi.Work;
+import org.hibernate.search.backend.spi.WorkType;
+import org.hibernate.search.engine.spi.SearchFactoryImplementor;
+import org.hibernate.search.query.dsl.QueryBuilder;
+import org.hibernate.search.query.engine.spi.EntityInfo;
+import org.hibernate.search.test.programmaticmapping.TestingSearchFactoryHolder;
+import org.hibernate.search.test.util.ManualTransactionContext;
+import org.junit.Rule;
+import org.junit.Test;
+
+/**
+ * @author Sanne Grinovero <sanne@hibernate.org> (C) 2013 Red Hat Inc.
+ */
+public class PropertiesExampleBridgeTest {
+
+ @Rule
+ public TestingSearchFactoryHolder sfHolder = new TestingSearchFactoryHolder( DynamicIndexedValueHolder.class );
+
+ @Test
+ public void testPropertiesIndexing() {
+ SearchFactoryImplementor searchFactory = sfHolder.getSearchFactory();
+ Assert.assertNotNull( searchFactory.getAllIndexesManager().getIndexManager( "all" ) );
+
+ {
+ // Store some test data:
+ DynamicIndexedValueHolder holder = new DynamicIndexedValueHolder( "1" )
+ .property( "age", "227" )
+ .property( "name", "Thorin" )
+ .property( "surname", "Oakenshield" )
+ .property( "race", "dwarf" );
+
+ Work work = new Work( holder, holder.id, WorkType.ADD, false );
+ ManualTransactionContext tc = new ManualTransactionContext();
+ searchFactory.getWorker().performWork( work, tc );
+ tc.end();
+ }
+
+ QueryBuilder guestQueryBuilder = searchFactory.buildQueryBuilder().forEntity( DynamicIndexedValueHolder.class ).get();
+
+ Query queryAllGuests = guestQueryBuilder.all().createQuery();
+
+ List<EntityInfo> queryEntityInfos = searchFactory.createHSQuery().luceneQuery( queryAllGuests )
+ .targetedEntities( Arrays.asList( new Class<?>[] { DynamicIndexedValueHolder.class } ) )
+ .projection( "value.surname" )
+ .queryEntityInfos();
+
+ Assert.assertEquals( 1, queryEntityInfos.size() );
+ EntityInfo entityInfo = queryEntityInfos.get( 0 );
+ Assert.assertEquals( "Oakenshield", entityInfo.getProjection()[0] );
+
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.