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;
}
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;
});
}
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;
}
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();
}
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>");
}
Aggregations