use of org.apache.solr.common.SolrInputField in project lucene-solr by apache.
the class LanguageIdentifierUpdateProcessor method process.
/**
* This is the main, testable process method called from processAdd()
* @param doc the SolrInputDocument to work on
* @return the modified SolrInputDocument
*/
protected SolrInputDocument process(SolrInputDocument doc) {
String docLang = null;
HashSet<String> docLangs = new HashSet<>();
String fallbackLang = getFallbackLang(doc, fallbackFields, fallbackValue);
if (langField == null || !doc.containsKey(langField) || (doc.containsKey(langField) && overwrite)) {
List<DetectedLanguage> languagelist = detectLanguage(doc);
docLang = resolveLanguage(languagelist, fallbackLang);
docLangs.add(docLang);
log.debug("Detected main document language from fields " + Arrays.toString(inputFields) + ": " + docLang);
if (doc.containsKey(langField) && overwrite) {
log.debug("Overwritten old value " + doc.getFieldValue(langField));
}
if (langField != null && langField.length() != 0) {
doc.setField(langField, docLang);
}
} else {
// langField is set, we sanity check it against whitelist and fallback
docLang = resolveLanguage((String) doc.getFieldValue(langField), fallbackLang);
docLangs.add(docLang);
log.debug("Field " + langField + " already contained value " + docLang + ", not overwriting.");
}
if (enableMapping) {
for (String fieldName : allMapFieldsSet) {
if (doc.containsKey(fieldName)) {
String fieldLang;
if (mapIndividual && mapIndividualFieldsSet.contains(fieldName)) {
List<DetectedLanguage> languagelist = detectLanguage(doc);
fieldLang = resolveLanguage(languagelist, docLang);
docLangs.add(fieldLang);
log.debug("Mapping field " + fieldName + " using individually detected language " + fieldLang);
} else {
fieldLang = docLang;
log.debug("Mapping field " + fieldName + " using document global language " + fieldLang);
}
String mappedOutputField = getMappedField(fieldName, fieldLang);
if (mappedOutputField != null) {
log.debug("Mapping field {} to {}", doc.getFieldValue(docIdField), fieldLang);
SolrInputField inField = doc.getField(fieldName);
doc.setField(mappedOutputField, inField.getValue());
if (!mapKeepOrig) {
log.debug("Removing old field {}", fieldName);
doc.removeField(fieldName);
}
} else {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid output field mapping for " + fieldName + " field and language: " + fieldLang);
}
}
}
}
// Set the languages field to an array of all detected languages
if (langsField != null && langsField.length() != 0) {
doc.setField(langsField, docLangs.toArray());
}
return doc;
}
use of org.apache.solr.common.SolrInputField in project lucene-solr by apache.
the class DocumentBuilder method toDocument.
/**
* Convert a SolrInputDocument to a lucene Document.
*
* This function should go elsewhere. This builds the Document without an
* extra Map<> checking for multiple values. For more discussion, see:
* http://www.nabble.com/Re%3A-svn-commit%3A-r547493---in--lucene-solr-trunk%3A-.--src-java-org-apache-solr-common--src-java-org-apache-solr-schema--src-java-org-apache-solr-update--src-test-org-apache-solr-common--tf3931539.html
*
* TODO: /!\ NOTE /!\ This semantics of this function are still in flux.
* Something somewhere needs to be able to fill up a SolrDocument from
* a lucene document - this is one place that may happen. It may also be
* moved to an independent function
*
* @since solr 1.3
*
* @param doc SolrInputDocument from which the document has to be built
* @param schema Schema instance
* @param forInPlaceUpdate Whether the output document would be used for an in-place update or not. When this is true,
* default fields values and copy fields targets are not populated.
* @return Built Lucene document
*/
public static Document toDocument(SolrInputDocument doc, IndexSchema schema, boolean forInPlaceUpdate) {
final SchemaField uniqueKeyField = schema.getUniqueKeyField();
final String uniqueKeyFieldName = null == uniqueKeyField ? null : uniqueKeyField.getName();
Document out = new Document();
Set<String> usedFields = Sets.newHashSet();
// Load fields from SolrDocument to Document
for (SolrInputField field : doc) {
String name = field.getName();
SchemaField sfield = schema.getFieldOrNull(name);
boolean used = false;
// Make sure it has the correct number
if (sfield != null && !sfield.multiValued() && field.getValueCount() > 1) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "ERROR: " + getID(doc, schema) + "multiple values encountered for non multiValued field " + sfield.getName() + ": " + field.getValue());
}
List<CopyField> copyFields = schema.getCopyFieldsList(name);
if (copyFields.size() == 0)
copyFields = null;
// load each field value
boolean hasField = false;
try {
for (Object v : field) {
if (v == null) {
continue;
}
hasField = true;
if (sfield != null) {
used = true;
addField(out, sfield, v, name.equals(uniqueKeyFieldName) ? false : forInPlaceUpdate);
// record the field as having a value
usedFields.add(sfield.getName());
}
// This could happen whether it is explicit or not.
if (copyFields != null) {
// and this is the uniqueKey field (because the uniqueKey can't change so no need to "update" the copyField).
if (!(forInPlaceUpdate && name.equals(uniqueKeyFieldName))) {
for (CopyField cf : copyFields) {
SchemaField destinationField = cf.getDestination();
final boolean destHasValues = usedFields.contains(destinationField.getName());
// check if the copy field is a multivalued or not
if (!destinationField.multiValued() && destHasValues) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "ERROR: " + getID(doc, schema) + "multiple values encountered for non multiValued copy field " + destinationField.getName() + ": " + v);
}
used = true;
// Perhaps trim the length of a copy field
Object val = v;
if (val instanceof String && cf.getMaxChars() > 0) {
val = cf.getLimitedValue((String) val);
}
addField(out, destinationField, val, destinationField.getName().equals(uniqueKeyFieldName) ? false : forInPlaceUpdate);
// record the field as having a value
usedFields.add(destinationField.getName());
}
}
}
}
} catch (SolrException ex) {
throw ex;
} catch (Exception ex) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "ERROR: " + getID(doc, schema) + "Error adding field '" + field.getName() + "'='" + field.getValue() + "' msg=" + ex.getMessage(), ex);
}
// make sure the field was used somehow...
if (!used && hasField) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "ERROR: " + getID(doc, schema) + "unknown field '" + name + "'");
}
}
// during the full indexing initially.
if (!forInPlaceUpdate) {
for (SchemaField field : schema.getRequiredFields()) {
if (out.getField(field.getName()) == null) {
if (field.getDefaultValue() != null) {
addField(out, field, field.getDefaultValue(), false);
} else {
String msg = getID(doc, schema) + "missing required field: " + field.getName();
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, msg);
}
}
}
}
if (!forInPlaceUpdate) {
moveLargestFieldLast(out);
}
return out;
}
use of org.apache.solr.common.SolrInputField in project lucene-solr by apache.
the class DocumentBuilderTest method testSolrInputFieldEquality.
public void testSolrInputFieldEquality() {
String randomString = TestUtil.randomSimpleString(random(), 10, 20);
int val = random().nextInt();
SolrInputField sif1 = new SolrInputField(randomString);
sif1.setValue(val);
SolrInputField sif2 = new SolrInputField(randomString);
sif2.setValue(val);
assertTrue(assertSolrInputFieldEquals(sif1, sif2));
sif2.setName("foo");
assertFalse(assertSolrInputFieldEquals(sif1, sif2));
}
use of org.apache.solr.common.SolrInputField in project lucene-solr by apache.
the class TestDocumentObjectBinder method testSimple.
public void testSimple() throws Exception {
DocumentObjectBinder binder = new DocumentObjectBinder();
XMLResponseParser parser = new XMLResponseParser();
NamedList<Object> nl = parser.processResponse(new StringReader(xml));
QueryResponse res = new QueryResponse(nl, null);
SolrDocumentList solDocList = res.getResults();
List<Item> l = binder.getBeans(Item.class, res.getResults());
assertEquals(solDocList.size(), l.size());
assertEquals(solDocList.get(0).getFieldValue("features"), l.get(0).features);
Item item = new Item();
item.id = "aaa";
item.categories = new String[] { "aaa", "bbb", "ccc" };
SolrInputDocument out = binder.toSolrInputDocument(item);
assertEquals(item.id, out.getFieldValue("id"));
SolrInputField catfield = out.getField("cat");
assertEquals(3, catfield.getValueCount());
List<String> catValues = (List<String>) catfield.getValue();
assertEquals("aaa", catValues.get(0));
assertEquals("bbb", catValues.get(1));
assertEquals("ccc", catValues.get(2));
}
use of org.apache.solr.common.SolrInputField in project lucene-solr by apache.
the class TestUpdateRequestCodec method testIteratable.
@Test
public void testIteratable() throws IOException {
final List<String> values = new ArrayList<>();
values.add("iterItem1");
values.add("iterItem2");
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.deleteByQuery("*:*");
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", 1);
doc.addField("desc", "one");
// imagine someone adding a custom Bean that implements Iterable
// but is not a Collection
doc.addField("iter", new Iterable<String>() {
@Override
public Iterator<String> iterator() {
return values.iterator();
}
});
doc.addField("desc", "1");
updateRequest.add(doc);
JavaBinUpdateRequestCodec codec = new JavaBinUpdateRequestCodec();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
codec.marshal(updateRequest, baos);
final List<SolrInputDocument> docs = new ArrayList<>();
JavaBinUpdateRequestCodec.StreamingUpdateHandler handler = new JavaBinUpdateRequestCodec.StreamingUpdateHandler() {
@Override
public void update(SolrInputDocument document, UpdateRequest req, Integer commitWithin, Boolean overwrite) {
Assert.assertNotNull(req.getParams());
docs.add(document);
}
};
UpdateRequest updateUnmarshalled = codec.unmarshal(new ByteArrayInputStream(baos.toByteArray()), handler);
for (SolrInputDocument document : docs) {
updateUnmarshalled.add(document);
}
SolrInputDocument outDoc = updateUnmarshalled.getDocuments().get(0);
SolrInputField iter = outDoc.getField("iter");
Assert.assertNotNull("iter field is null", iter);
Object iterVal = iter.getValue();
Assert.assertTrue("iterVal is not a Collection", iterVal instanceof Collection);
Assert.assertEquals("iterVal contents", values, iterVal);
}
Aggregations