Search in sources :

Example 16 with SolrInputField

use of org.apache.solr.common.SolrInputField in project lucene-solr by apache.

the class JsonLoaderTest method testEmptyChildDocs.

@Test
public void testEmptyChildDocs() throws Exception {
    String str = "{\n" + "    \"add\": {\n" + "        \"doc\": {\n" + "            \"id\": \"1\",\n" + "            \"_childDocuments_\": []\n" + "        }\n" + "    }\n" + "}";
    SolrQueryRequest req = req("commit", "true");
    SolrQueryResponse rsp = new SolrQueryResponse();
    BufferingRequestProcessor p = new BufferingRequestProcessor(null);
    JsonLoader loader = new JsonLoader();
    loader.load(req, rsp, new ContentStreamBase.StringStream(str), p);
    assertEquals(1, p.addCommands.size());
    AddUpdateCommand add = p.addCommands.get(0);
    SolrInputDocument d = add.solrDoc;
    SolrInputField f = d.getField("id");
    assertEquals("1", f.getValue());
    List<SolrInputDocument> cd = d.getChildDocuments();
    assertNull(cd);
    req.close();
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) BufferingRequestProcessor(org.apache.solr.update.processor.BufferingRequestProcessor) SolrInputField(org.apache.solr.common.SolrInputField) JsonLoader(org.apache.solr.handler.loader.JsonLoader) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase) Test(org.junit.Test)

Example 17 with SolrInputField

use of org.apache.solr.common.SolrInputField in project lucene-solr by apache.

the class FieldMutatingUpdateProcessorTest method testLastValue.

public void testLastValue() throws Exception {
    SolrInputDocument d = null;
    // basics
    d = processAdd("last-value", doc(f("id", "1111"), f("foo_s", "string1", "string222"), f("bar_s", "string3"), f("yak_t", "string4", "string5")));
    assertNotNull(d);
    assertEquals(Arrays.asList("string222"), d.getFieldValues("foo_s"));
    assertEquals(Arrays.asList("string3"), d.getFieldValues("bar_s"));
    assertEquals(Arrays.asList("string4", "string5"), d.getFieldValues("yak_t"));
    // test optimizations (and force test of defaults)
    SolrInputField special = null;
    // test something that's definitely a SortedSet
    special = new SolrInputField("foo_s");
    special.setValue(new TreeSet<>(Arrays.asList("ggg", "first", "last", "hhh")));
    d = processAdd("last-value", doc(f("id", "1111"), special));
    assertNotNull(d);
    assertEquals("last", d.getFieldValue("foo_s"));
    // test something that's definitely a List
    special = new SolrInputField("foo_s");
    special.setValue(Arrays.asList("first", "ggg", "hhh", "last"));
    d = processAdd("last-value", doc(f("id", "1111"), special));
    assertNotNull(d);
    assertEquals("last", d.getFieldValue("foo_s"));
    // test something that is definitely not a List or SortedSet 
    // (ie: get default behavior of Collection using iterator)
    special = new SolrInputField("foo_s");
    special.setValue(new LinkedHashSet<>(Arrays.asList("first", "ggg", "hhh", "last")));
    d = processAdd("last-value", doc(f("id", "1111"), special));
    assertNotNull(d);
    assertEquals("last", d.getFieldValue("foo_s"));
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrInputField(org.apache.solr.common.SolrInputField)

Example 18 with SolrInputField

use of org.apache.solr.common.SolrInputField in project lucene-solr by apache.

the class SolrTestCaseJ4 method json.

/**
   * Appends to the <code>out</code> array with JSON from the <code>doc</code>.
   * Doesn't currently handle boosts, but does recursively handle child documents
   */
public static void json(SolrInputDocument doc, CharArr out) {
    try {
        out.append('{');
        boolean firstField = true;
        for (SolrInputField sfield : doc) {
            if (firstField)
                firstField = false;
            else
                out.append(',');
            JSONUtil.writeString(sfield.getName(), 0, sfield.getName().length(), out);
            out.append(':');
            if (sfield.getValueCount() > 1) {
                out.append('[');
                boolean firstVal = true;
                for (Object val : sfield) {
                    if (firstVal)
                        firstVal = false;
                    else
                        out.append(',');
                    out.append(JSONUtil.toJSON(val));
                }
                out.append(']');
            } else {
                out.append(JSONUtil.toJSON(sfield.getValue()));
            }
        }
        boolean firstChildDoc = true;
        if (doc.hasChildDocuments()) {
            out.append(",\"_childDocuments_\": [");
            List<SolrInputDocument> childDocuments = doc.getChildDocuments();
            for (SolrInputDocument childDocument : childDocuments) {
                if (firstChildDoc)
                    firstChildDoc = false;
                else
                    out.append(',');
                json(childDocument, out);
            }
            out.append(']');
        }
        out.append('}');
    } catch (IOException e) {
    // should never happen
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrInputField(org.apache.solr.common.SolrInputField) IOException(java.io.IOException)

Example 19 with SolrInputField

use of org.apache.solr.common.SolrInputField in project lucene-solr by apache.

the class JavaBinCodec method writeSolrInputDocument.

public void writeSolrInputDocument(SolrInputDocument sdoc) throws IOException {
    List<SolrInputDocument> children = sdoc.getChildDocuments();
    int sz = sdoc.size() + (children == null ? 0 : children.size());
    writeTag(SOLRINPUTDOC, sz);
    // document boost
    writeFloat(1f);
    for (SolrInputField inputField : sdoc.values()) {
        writeExternString(inputField.getName());
        writeVal(inputField.getValue());
    }
    if (children != null) {
        for (SolrInputDocument child : children) {
            writeSolrInputDocument(child);
        }
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrInputField(org.apache.solr.common.SolrInputField)

Example 20 with SolrInputField

use of org.apache.solr.common.SolrInputField in project lucene-solr by apache.

the class TestCloudDeleteByQuery method f.

public static SolrInputField f(String fieldName, Object... values) {
    SolrInputField f = new SolrInputField(fieldName);
    f.setValue(values);
    return f;
}
Also used : SolrInputField(org.apache.solr.common.SolrInputField)

Aggregations

SolrInputField (org.apache.solr.common.SolrInputField)45 SolrInputDocument (org.apache.solr.common.SolrInputDocument)25 AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)14 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)12 ContentStreamBase (org.apache.solr.common.util.ContentStreamBase)11 JsonLoader (org.apache.solr.handler.loader.JsonLoader)11 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)11 BufferingRequestProcessor (org.apache.solr.update.processor.BufferingRequestProcessor)11 Test (org.junit.Test)8 SolrException (org.apache.solr.common.SolrException)7 SchemaField (org.apache.solr.schema.SchemaField)7 Collection (java.util.Collection)4 Map (java.util.Map)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Matcher (java.util.regex.Matcher)3 IndexSchema (org.apache.solr.schema.IndexSchema)3 DeleteUpdateCommand (org.apache.solr.update.DeleteUpdateCommand)3 HashMap (java.util.HashMap)2 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)2