Skip to content

Commit

Permalink
Add unit case for infering types for query in lucene datastore
Browse files Browse the repository at this point in the history
  • Loading branch information
cuent committed Apr 6, 2019
1 parent b04910d commit 3e17356
Show file tree
Hide file tree
Showing 5 changed files with 438 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ ivy/ivy*.jar
**/gora-solr/src/test/conf/solr-managed-schema/collection1/data/
test_lucene_index_employee
test_lucene_index_webpage/
test_lucene_index_employeeint/
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@
*/
package org.apache.gora.lucene.store;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.FileSystems;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.avro.Schema;
import org.apache.avro.Schema.Type;
import org.apache.avro.specific.SpecificDatumReader;
Expand All @@ -40,7 +51,11 @@
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.DoublePoint;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.FloatPoint;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.IndexWriter;
Expand All @@ -61,19 +76,6 @@
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.FileSystems;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.Set;

/**
* {@link org.apache.gora.lucene.store.LuceneStore} is the primary class
* responsible for GORA CRUD operations on Lucene.
Expand All @@ -96,7 +98,7 @@ public class LuceneStore<K, T extends PersistentBase>

@Override
public void initialize(Class<K> keyClass, Class<T> persistentClass,
Properties properties) throws GoraException {
Properties properties) throws GoraException {
try {
super.initialize(keyClass, persistentClass, properties);
} catch (GoraException ge) {
Expand Down Expand Up @@ -153,7 +155,6 @@ private LuceneMapping readMapping(String filename) throws IOException {

DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.dom.Document dom = db.parse(getClass().getClassLoader().getResourceAsStream(filename));

Element root = dom.getDocumentElement();

NodeList nl = root.getElementsByTagName("class");
Expand Down Expand Up @@ -301,9 +302,9 @@ public T get(K key, String[] fieldsToLoad) {
}

private Object convertDocFieldToAvroUnion(final Schema fieldSchema,
final Schema.Field field,
final String sf,
final Document doc) throws IOException {
final Schema.Field field,
final String sf,
final Document doc) throws IOException {
Object result;
Schema.Type type0 = fieldSchema.getTypes().get(0).getType();
Schema.Type type1 = fieldSchema.getTypes().get(1).getType();
Expand All @@ -330,9 +331,9 @@ private SpecificDatumReader getDatumReader(Schema fieldSchema) {
}

private Object convertToIndexableFieldToAvroField(final Document doc,
final Schema.Field field,
final Schema fieldSchema,
final String sf) throws IOException {
final Schema.Field field,
final Schema fieldSchema,
final String sf) throws IOException {
Object result = null;
T persistent = newPersistent();
Object sv;
Expand Down Expand Up @@ -448,8 +449,8 @@ public Query<K, T> newQuery() {
}

private IndexableField convertAvroUnionToDocumentField(final String sf,
final Schema fieldSchema,
final Object value) {
final Schema fieldSchema,
final Object value) {
IndexableField result;
Schema.Type type0 = fieldSchema.getTypes().get(0).getType();
Schema.Type type1 = fieldSchema.getTypes().get(1).getType();
Expand Down Expand Up @@ -545,7 +546,17 @@ public void put(K key, T persistent) {
}
LOG.info("DOCUMENT: {}", doc);
try {
doc.add(new StringField(mapping.getPrimaryKey(), key.toString(), Store.YES));
if (key instanceof Integer) {
doc.add(new IntPoint(mapping.getPrimaryKey(), ((Integer) key).intValue()));
} else if (key instanceof Long) {
doc.add(new LongPoint(mapping.getPrimaryKey(), ((Long) key).longValue()));
} else if (key instanceof Float) {
doc.add(new FloatPoint(mapping.getPrimaryKey(), ((Float) key).floatValue()));
} else if (key instanceof Double) {
doc.add(new DoublePoint(mapping.getPrimaryKey(), ((Double) key).doubleValue()));
} else {
doc.add(new StringField(mapping.getPrimaryKey(), key.toString(), Store.YES));
}
LOG.info("DOCUMENT: {}", doc);
if (get(key, null) == null) {
writer.addDocument(doc);
Expand Down
5 changes: 5 additions & 0 deletions gora-lucene/src/test/conf/gora-lucene-mapping.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

<gora-otd>

<class name="org.apache.gora.lucene.store.EmployeeInt" keyClass="java.lang.Integer">
<primarykey column="ssn"/>
<field name="ssn" column="ssn"/>
</class>

<class name="org.apache.gora.examples.generated.Employee" keyClass="java.lang.String">
<primarykey column="ssn"/>
<field name="ssn" column="ssn"/>
Expand Down
Loading

0 comments on commit 3e17356

Please sign in to comment.