org.apache.solr.update.processor.ConcatFieldUpdateProcessorFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of solr-core Show documentation
Show all versions of solr-core Show documentation
Apache Solr (module: core)
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.update.processor;
import static org.apache.solr.update.processor.FieldMutatingUpdateProcessor.mutator;
import java.util.stream.Collectors;
import org.apache.solr.common.SolrInputField;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.core.SolrCore;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.schema.FieldType;
import org.apache.solr.schema.IndexSchema;
import org.apache.solr.schema.SchemaField;
import org.apache.solr.schema.StrField;
import org.apache.solr.schema.TextField;
/**
* Concatenates multiple values for fields matching the specified conditions using a configurable
* delimiter
which defaults to ",
".
*
* By default, this processor concatenates the values for any field name which according to the
* schema is multiValued="false"
and uses TextField
or StrField
*
*
*
For example, in the configuration below, any "single valued" string and text field which is
* found to contain multiple values except for the primary_author
field will be
* concatenated using the string ";
" as a delimiter. For the primary_author
*
field, the multiple values will be left alone for
* FirstFieldValueUpdateProcessorFactory
to deal with.
*
*
* <processor class="solr.ConcatFieldUpdateProcessorFactory">
* <str name="delimiter">; </str>
* <lst name="exclude">
* <str name="fieldName">primary_author</str>
* </lst>
* </processor>
* <processor class="solr.FirstFieldValueUpdateProcessorFactory">
* <str name="fieldName">primary_author</str>
* </processor>
*
* @since 4.0.0
*/
public final class ConcatFieldUpdateProcessorFactory extends FieldMutatingUpdateProcessorFactory {
String delimiter = ", ";
@Override
public void init(NamedList args) {
Object d = args.remove("delimiter");
if (null != d) delimiter = d.toString();
super.init(args);
}
@Override
public 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(
src.getValues().stream().map(String::valueOf).collect(Collectors.joining(delimiter)));
return result;
});
}
@Override
public FieldMutatingUpdateProcessor.FieldNameSelector getDefaultSelector(final SolrCore core) {
return fieldName -> {
final IndexSchema schema = core.getLatestSchema();
// first check type since it should be fastest
FieldType type = schema.getFieldTypeNoEx(fieldName);
if (null == type) return false;
if (!(TextField.class.isInstance(type) || StrField.class.isInstance(type))) {
return false;
}
// only ask for SchemaField if we passed the type check.
SchemaField sf = schema.getFieldOrNull(fieldName);
// shouldn't be null since since type wasn't, but just in case
if (null == sf) return false;
return !sf.multiValued();
};
}
}