Search in sources :

Example 1 with JsonDocument

use of com.tuplejump.stargate.lucene.json.JsonDocument 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"));
}
Also used : Field(org.apache.lucene.document.Field) JsonField(argo.jdom.JsonField) StreamingJsonDocument(com.tuplejump.stargate.lucene.json.StreamingJsonDocument) JsonNode(argo.jdom.JsonNode) Properties(com.tuplejump.stargate.lucene.Properties) JsonDocument(com.tuplejump.stargate.lucene.json.JsonDocument) StreamingJsonDocument(com.tuplejump.stargate.lucene.json.StreamingJsonDocument) Test(org.junit.Test)

Example 2 with JsonDocument

use of com.tuplejump.stargate.lucene.json.JsonDocument 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);
            }
        }
    }
}
Also used : Field(org.apache.lucene.document.Field) FieldType(org.apache.lucene.document.FieldType) CQL3Type(org.apache.cassandra.cql3.CQL3Type) CType(org.apache.cassandra.db.composites.CType) StreamingJsonDocument(com.tuplejump.stargate.lucene.json.StreamingJsonDocument) FastList(javolution.util.FastList) JsonDocument(com.tuplejump.stargate.lucene.json.JsonDocument) StreamingJsonDocument(com.tuplejump.stargate.lucene.json.StreamingJsonDocument) FieldType(org.apache.lucene.document.FieldType)

Aggregations

JsonDocument (com.tuplejump.stargate.lucene.json.JsonDocument)2 StreamingJsonDocument (com.tuplejump.stargate.lucene.json.StreamingJsonDocument)2 Field (org.apache.lucene.document.Field)2 JsonField (argo.jdom.JsonField)1 JsonNode (argo.jdom.JsonNode)1 Properties (com.tuplejump.stargate.lucene.Properties)1 FastList (javolution.util.FastList)1 CQL3Type (org.apache.cassandra.cql3.CQL3Type)1 CType (org.apache.cassandra.db.composites.CType)1 FieldType (org.apache.lucene.document.FieldType)1 Test (org.junit.Test)1