Search in sources :

Example 6 with JSONParser

use of org.noggit.JSONParser in project lucene-solr by apache.

the class TestBulkSchemaAPI method testSimilarityParser.

public void testSimilarityParser() throws Exception {
    RestTestHarness harness = restTestHarness;
    final float k1 = 2.25f;
    final float b = 0.33f;
    String fieldTypeName = "MySimilarityField";
    String fieldName = "similarityTestField";
    String payload = "{\n" + "  'add-field-type' : {" + "    'name' : '" + fieldTypeName + "',\n" + "    'class':'solr.TextField',\n" + "    'analyzer' : {'tokenizer':{'class':'solr.WhitespaceTokenizerFactory'}},\n" + "    'similarity' : {'class':'org.apache.solr.search.similarities.BM25SimilarityFactory', 'k1':" + k1 + ", 'b':" + b + " }\n" + "  },\n" + "  'add-field' : {\n" + "    'name':'" + fieldName + "',\n" + "    'type': 'MySimilarityField',\n" + "    'stored':true,\n" + "    'indexed':true\n" + "  }\n" + "}\n";
    String response = harness.post("/schema?wt=json&indent=on", json(payload));
    Map map = (Map) ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
    assertNull(response, map.get("errors"));
    Map fields = getObj(harness, fieldName, "fields");
    assertNotNull("field " + fieldName + " not created", fields);
    assertFieldSimilarity(fieldName, BM25Similarity.class, sim -> assertEquals("Unexpected k1", k1, sim.getK1(), .001), sim -> assertEquals("Unexpected b", b, sim.getB(), .001));
    final String independenceMeasure = "Saturated";
    final boolean discountOverlaps = false;
    payload = "{\n" + "  'replace-field-type' : {" + "    'name' : '" + fieldTypeName + "',\n" + "    'class':'solr.TextField',\n" + "    'analyzer' : {'tokenizer':{'class':'solr.WhitespaceTokenizerFactory'}},\n" + "    'similarity' : {\n" + "      'class':'org.apache.solr.search.similarities.DFISimilarityFactory',\n" + "      'independenceMeasure':'" + independenceMeasure + "',\n" + "      'discountOverlaps':" + discountOverlaps + "\n" + "     }\n" + "  }\n" + "}\n";
    response = harness.post("/schema?wt=json&indent=on", json(payload));
    map = (Map) ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
    assertNull(response, map.get("errors"));
    fields = getObj(harness, fieldName, "fields");
    assertNotNull("field " + fieldName + " not created", fields);
    assertFieldSimilarity(fieldName, DFISimilarity.class, sim -> assertEquals("Unexpected independenceMeasure", independenceMeasure, sim.getIndependence().toString()), sim -> assertEquals("Unexpected discountedOverlaps", discountOverlaps, sim.getDiscountOverlaps()));
}
Also used : RestTestHarness(org.apache.solr.util.RestTestHarness) StringReader(java.io.StringReader) JSONParser(org.noggit.JSONParser) Map(java.util.Map)

Example 7 with JSONParser

use of org.noggit.JSONParser in project lucene-solr by apache.

the class TestBulkSchemaAPI method testMultipleAddFieldWithErrors.

public void testMultipleAddFieldWithErrors() throws Exception {
    String payload = "{\n" + "    'add-field' : {\n" + "                 'name':'a1',\n" + "                 'type': 'string1',\n" + "                 'stored':true,\n" + "                 'indexed':false\n" + "                 },\n" + "    'add-field' : {\n" + "                 'type': 'string',\n" + "                 'stored':true,\n" + "                 'indexed':true\n" + "                 }\n" + "    }";
    String response = restTestHarness.post("/schema?wt=json", json(payload));
    Map map = (Map) ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
    List l = (List) map.get("errors");
    assertNotNull("No errors", l);
    List errorList = (List) ((Map) l.get(0)).get("errorMessages");
    assertEquals(1, errorList.size());
    assertTrue(((String) errorList.get(0)).contains("Field 'a1': Field type 'string1' not found.\n"));
    errorList = (List) ((Map) l.get(1)).get("errorMessages");
    assertEquals(1, errorList.size());
    assertTrue(((String) errorList.get(0)).contains("is a required field"));
}
Also used : StringReader(java.io.StringReader) JSONParser(org.noggit.JSONParser) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 8 with JSONParser

use of org.noggit.JSONParser in project lucene-solr by apache.

the class TestBulkSchemaAPI method testAddIllegalFields.

public void testAddIllegalFields() throws Exception {
    RestTestHarness harness = restTestHarness;
    // 1. Make sure you can't create a new field with an asterisk in its name
    String newFieldName = "asterisk*";
    String payload = "{\n" + "    'add-field' : {\n" + "         'name':'" + newFieldName + "',\n" + "         'type':'string',\n" + "         'stored':true,\n" + "         'indexed':true\n" + "     }\n" + "}";
    String response = harness.post("/schema?wt=json", json(payload));
    Map map = (Map) ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
    assertNotNull(response, map.get("errors"));
    map = getObj(harness, newFieldName, "fields");
    assertNull(newFieldName + " illegal dynamic field should not have been added to schema", map);
    // 2. Make sure you get an error when you try to create a field that already exists
    // Make sure 'wdf_nocase' field exists
    newFieldName = "wdf_nocase";
    Map m = getObj(harness, newFieldName, "fields");
    assertNotNull("'" + newFieldName + "' field does not exist in the schema", m);
    payload = "{\n" + "    'add-field' : {\n" + "         'name':'" + newFieldName + "',\n" + "         'type':'string',\n" + "         'stored':true,\n" + "         'indexed':true\n" + "     }\n" + "}";
    response = harness.post("/schema?wt=json", json(payload));
    map = (Map) ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
    assertNotNull(response, map.get("errors"));
}
Also used : RestTestHarness(org.apache.solr.util.RestTestHarness) StringReader(java.io.StringReader) JSONParser(org.noggit.JSONParser) Map(java.util.Map)

Example 9 with JSONParser

use of org.noggit.JSONParser in project lucene-solr by apache.

the class TestBulkSchemaAPI method testAddFieldWithExistingCatchallDynamicField.

public void testAddFieldWithExistingCatchallDynamicField() throws Exception {
    RestTestHarness harness = restTestHarness;
    String newFieldName = "NewField1";
    Map map = getObj(harness, newFieldName, "fields");
    assertNull("Field '" + newFieldName + "' already exists in the schema", map);
    map = getObj(harness, "*", "dynamicFields");
    assertNull("'*' dynamic field already exists in the schema", map);
    map = getObj(harness, "string", "fieldTypes");
    assertNotNull("'boolean' field type does not exist in the schema", map);
    map = getObj(harness, "boolean", "fieldTypes");
    assertNotNull("'boolean' field type does not exist in the schema", map);
    String payload = "{\n" + "    'add-dynamic-field' : {\n" + "         'name':'*',\n" + "         'type':'string',\n" + "         'stored':true,\n" + "         'indexed':true\n" + "    }\n" + "}";
    String response = harness.post("/schema?wt=json", json(payload));
    map = (Map) ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
    assertNull(response, map.get("errors"));
    map = getObj(harness, "*", "dynamicFields");
    assertNotNull("Dynamic field '*' is not in the schema", map);
    payload = "{\n" + "    'add-field' : {\n" + "                 'name':'" + newFieldName + "',\n" + "                 'type':'boolean',\n" + "                 'stored':true,\n" + "                 'indexed':true\n" + "                 }\n" + "    }";
    response = harness.post("/schema?wt=json", json(payload));
    map = (Map) ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
    assertNull(response, map.get("errors"));
    map = getObj(harness, newFieldName, "fields");
    assertNotNull("Field '" + newFieldName + "' is not in the schema", map);
}
Also used : RestTestHarness(org.apache.solr.util.RestTestHarness) StringReader(java.io.StringReader) JSONParser(org.noggit.JSONParser) Map(java.util.Map)

Example 10 with JSONParser

use of org.noggit.JSONParser in project lucene-solr by apache.

the class TestConfigReload method getAsMap.

private Map getAsMap(String uri) throws Exception {
    HttpGet get = new HttpGet(uri);
    HttpEntity entity = null;
    try {
        entity = cloudClient.getLbClient().getHttpClient().execute(get).getEntity();
        String response = EntityUtils.toString(entity, StandardCharsets.UTF_8);
        return (Map) ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
    } finally {
        EntityUtils.consumeQuietly(entity);
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) StringReader(java.io.StringReader) JSONParser(org.noggit.JSONParser) Map(java.util.Map)

Aggregations

JSONParser (org.noggit.JSONParser)37 Map (java.util.Map)30 StringReader (java.io.StringReader)25 RestTestHarness (org.apache.solr.util.RestTestHarness)12 ArrayList (java.util.ArrayList)10 List (java.util.List)10 IOException (java.io.IOException)6 HashSet (java.util.HashSet)6 LinkedHashMap (java.util.LinkedHashMap)6 HttpEntity (org.apache.http.HttpEntity)6 Test (org.junit.Test)5 HashMap (java.util.HashMap)4 SolrException (org.apache.solr.common.SolrException)4 HttpGet (org.apache.http.client.methods.HttpGet)3 HttpPost (org.apache.http.client.methods.HttpPost)3 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)3 NamedList (org.apache.solr.common.util.NamedList)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2