Search in sources :

Example 26 with SolrInputField

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

the class AtomicUpdateDocumentMerger method merge.

/**
   * Merges the fromDoc into the toDoc using the atomic update syntax.
   * 
   * @param fromDoc SolrInputDocument which will merged into the toDoc
   * @param toDoc the final SolrInputDocument that will be mutated with the values from the fromDoc atomic commands
   * @return toDoc with mutated values
   */
public SolrInputDocument merge(final SolrInputDocument fromDoc, SolrInputDocument toDoc) {
    for (SolrInputField sif : fromDoc.values()) {
        Object val = sif.getValue();
        if (val instanceof Map) {
            for (Entry<String, Object> entry : ((Map<String, Object>) val).entrySet()) {
                String key = entry.getKey();
                Object fieldVal = entry.getValue();
                boolean updateField = false;
                switch(key) {
                    case "add":
                        updateField = true;
                        doAdd(toDoc, sif, fieldVal);
                        break;
                    case "set":
                        updateField = true;
                        doSet(toDoc, sif, fieldVal);
                        break;
                    case "remove":
                        updateField = true;
                        doRemove(toDoc, sif, fieldVal);
                        break;
                    case "removeregex":
                        updateField = true;
                        doRemoveRegex(toDoc, sif, fieldVal);
                        break;
                    case "inc":
                        updateField = true;
                        doInc(toDoc, sif, fieldVal);
                        break;
                    default:
                        //Perhaps throw an error here instead?
                        log.warn("Unknown operation for the an atomic update, operation ignored: " + key);
                        break;
                }
                // validate that the field being modified is not the id field.
                if (updateField && idField.getName().equals(sif.getName())) {
                    throw new SolrException(ErrorCode.BAD_REQUEST, "Invalid update of id field: " + sif);
                }
            }
        } else {
            // normal fields are treated as a "set"
            toDoc.put(sif.getName(), sif);
        }
    }
    return toDoc;
}
Also used : SolrInputField(org.apache.solr.common.SolrInputField) Map(java.util.Map) SolrException(org.apache.solr.common.SolrException)

Example 27 with SolrInputField

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

the class FieldValueSubsetUpdateProcessorFactory method getInstance.

@Override
public final UpdateRequestProcessor getInstance(SolrQueryRequest req, SolrQueryResponse rsp, UpdateRequestProcessor next) {
    return mutator(getSelector(), next, src -> {
        if (src.getValueCount() <= 1)
            return src;
        SolrInputField result = new SolrInputField(src.getName());
        result.setValue(pickSubset(src.getValues()));
        return result;
    });
}
Also used : SolrInputField(org.apache.solr.common.SolrInputField)

Example 28 with SolrInputField

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

the class SolrTestCaseJ4 method assertSolrInputFieldEquals.

public boolean assertSolrInputFieldEquals(Object expected, Object actual) {
    if (!(expected instanceof SolrInputField) || !(actual instanceof SolrInputField)) {
        return false;
    }
    if (expected == actual) {
        return true;
    }
    SolrInputField sif1 = (SolrInputField) expected;
    SolrInputField sif2 = (SolrInputField) actual;
    if (!sif1.getName().equals(sif2.getName())) {
        return false;
    }
    if (!sif1.getValue().equals(sif2.getValue())) {
        return false;
    }
    return true;
}
Also used : SolrInputField(org.apache.solr.common.SolrInputField)

Example 29 with SolrInputField

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

the class DocumentAnalysisRequestHandlerTest method testResolveAnalysisRequest.

/**
   * Tests the {@link DocumentAnalysisRequestHandler#resolveAnalysisRequest(org.apache.solr.request.SolrQueryRequest)}
   */
@Test
public void testResolveAnalysisRequest() throws Exception {
    String docsInput = "<docs>" + "<doc>" + "<field name=\"id\">1</field>" + "<field name=\"whitetok\">The Whitetok</field>" + "<field name=\"text\">The Text</field>" + "</doc>" + "</docs>";
    final ContentStream cs = new ContentStreamBase.StringStream(docsInput);
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.add("analysis.query", "The Query String");
    params.add("analysis.showmatch", "true");
    SolrQueryRequest req = new SolrQueryRequestBase(h.getCore(), params) {

        @Override
        public Iterable<ContentStream> getContentStreams() {
            return Collections.singleton(cs);
        }
    };
    DocumentAnalysisRequest request = handler.resolveAnalysisRequest(req);
    assertNotNull(request);
    assertTrue(request.isShowMatch());
    assertNotNull(request.getQuery());
    assertEquals("The Query String", request.getQuery());
    List<SolrInputDocument> documents = request.getDocuments();
    assertNotNull(documents);
    assertEquals(1, documents.size());
    SolrInputDocument document = documents.get(0);
    SolrInputField field = document.getField("id");
    assertNotNull(field);
    assertEquals("1", field.getFirstValue());
    field = document.getField("whitetok");
    assertNotNull(field);
    assertEquals("The Whitetok", field.getFirstValue());
    field = document.getField("text");
    assertNotNull(field);
    assertEquals("The Text", field.getFirstValue());
    req.close();
}
Also used : ContentStream(org.apache.solr.common.util.ContentStream) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrInputField(org.apache.solr.common.SolrInputField) SolrQueryRequestBase(org.apache.solr.request.SolrQueryRequestBase) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) DocumentAnalysisRequest(org.apache.solr.client.solrj.request.DocumentAnalysisRequest) Test(org.junit.Test)

Example 30 with SolrInputField

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

the class ClientUtils method writeXML.

//------------------------------------------------------------------------
//------------------------------------------------------------------------
public static void writeXML(SolrInputDocument doc, Writer writer) throws IOException {
    writer.write("<doc>");
    for (SolrInputField field : doc) {
        String name = field.getName();
        for (Object v : field) {
            String update = null;
            if (v instanceof Map) {
                // currently only supports a single value
                for (Entry<Object, Object> entry : ((Map<Object, Object>) v).entrySet()) {
                    update = entry.getKey().toString();
                    v = entry.getValue();
                    if (v instanceof Collection) {
                        Collection values = (Collection) v;
                        for (Object value : values) {
                            writeVal(writer, name, value, update);
                        }
                    } else {
                        writeVal(writer, name, v, update);
                    }
                }
            } else {
                writeVal(writer, name, v, update);
            }
        }
    }
    if (doc.hasChildDocuments()) {
        for (SolrInputDocument childDocument : doc.getChildDocuments()) {
            writeXML(childDocument, writer);
        }
    }
    writer.write("</doc>");
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrInputField(org.apache.solr.common.SolrInputField) Collection(java.util.Collection) Map(java.util.Map)

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