use of com.tuplejump.stargate.lucene.json.StreamingJsonDocument in project stargate-core by tuplejump.
the class JsonDocumentTest method shouldParseJsonAndGetFields.
@Test
public void shouldParseJsonAndGetFields() throws Exception {
Properties jsonColProps = new Properties();
jsonColProps.setType(Type.object);
Properties ageProps = new Properties();
ageProps.setType(Type.integer);
//json fields mapping
jsonColProps.setFields(Collections.singletonMap("age", ageProps));
Properties rootProps = new Properties();
rootProps.setFields(Collections.singletonMap("jsoncol", jsonColProps));
StringWriter sw = new StringWriter();
PrintWriter writer = new PrintWriter(sw);
//formatJsonNode(kid, writer, 0);
formatJsonNode(jsonVal, writer, 0);
writer.flush();
String json = sw.toString();
JsonDocument jsonDocument = new StreamingJsonDocument(json, rootProps, "jsoncol");
List<Field> fields = jsonDocument.getFields();
System.out.println(fields);
System.out.println("Overall fields:" + fields.size());
Assert.assertEquals(75, fields.size());
Assert.assertEquals(5, numberOfFieldsWithKey("name", fields));
Assert.assertEquals(15, numberOfFieldsWithPrefix("friends", fields));
Assert.assertEquals(15, numberOfFieldsWithKey("friends.name", fields));
Assert.assertEquals(15, numberOfFieldsWithKey("tags", fields));
Assert.assertEquals(5, numberOfFieldsWithNumericType(FieldType.NumericType.INT, fields));
MemIndex memIndex = new MemIndex(rootProps);
List<JsonNode> kids = jsonVal.getElements();
for (JsonNode kid : kids) {
StringWriter kidSWriter = new StringWriter();
PrintWriter kidWriter = new PrintWriter(kidSWriter);
formatJsonNode(kid, kidWriter, 0);
kidWriter.flush();
String kidJson = kidSWriter.toString();
JsonDocument kidDoc = new StreamingJsonDocument(kidJson, rootProps, "jsoncol");
memIndex.add(kidDoc.getFields());
}
Assert.assertEquals(1, memIndex.hits("age:40", "jsoncol"));
Assert.assertEquals(2, memIndex.hits("eyeColor:blue", "jsoncol"));
Assert.assertEquals(2, memIndex.hits("gender:female", "jsoncol"));
}
use of com.tuplejump.stargate.lucene.json.StreamingJsonDocument in project stargate-core by tuplejump.
the class RowIndexSupport method addFields.
protected void addFields(Cell column, String name, ColumnDefinition columnDefinition, List<Field> fields) {
boolean isObject = options.isObject(name);
if (isObject) {
String value = UTF8Type.instance.compose(column.value());
JsonDocument document = new StreamingJsonDocument(value, options.primary, name);
fields.addAll(document.getFields());
} else if (column.name().isCollectionCell()) {
List<Field> fieldsForField = collectionFields((CollectionType) columnDefinition.type, name, column);
fields.addAll(fieldsForField);
} else {
FieldType fieldType = options.fieldTypes.get(name);
Type type = options.types.get(name);
addField(type, columnDefinition, name, fieldType, column.value(), fields);
if (options.containsDocValues()) {
FieldType docValueType = options.fieldDocValueTypes.get(name);
if (docValueType != null) {
Field docValueField = Fields.docValueField(name, columnDefinition.type, column.value(), docValueType);
fields.add(docValueField);
}
}
}
}
Aggregations