Apache Lucene open-source search software
SOLR-6937 In schemaless mode ,replace spaces and special characters with underscore
Noble Paul684c7375d9a2
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex d9dcd20732a..7630b13aa38 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -202,7 +202,7 @@ New Features(hossman)* SOLR-6485: ReplicationHandler should have an option to throttle the speed of- replication (Varun Thacker, NOble Paul)+ replication (Varun Thacker, Noble Paul)* SOLR-6543: Give HttpSolrClient the ability to send PUT requests (Gregory Chanan)@@ -305,6 +305,8 @@ New Features* SOLR-6581: Efficient DocValues support and numeric collapse field implementationsfor Collapse and Expand (Joel Bernstein)++* SOLR-6937: In schemaless mode ,replace spaces and special characters with underscore (Noble Paul)Bug Fixes----------------------diff --git a/solr/core/src/java/org/apache/solr/update/processor/FieldNameMutatingUpdateProcessorFactory.java b/solr/core/src/java/org/apache/solr/update/processor/FieldNameMutatingUpdateProcessorFactory.javanew file mode 100644index 00000000000..373558db69b--- /dev/null+++ b/solr/core/src/java/org/apache/solr/update/processor/FieldNameMutatingUpdateProcessorFactory.java@@ -0,0 +1,103 @@+package org.apache.solr.update.processor;++/*+ * 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.+ */+++import java.io.IOException;+import java.util.ArrayList;+import java.util.Collection;+import java.util.regex.Matcher;+import java.util.regex.Pattern;++import org.apache.solr.common.SolrException;+import org.apache.solr.common.SolrInputDocument;+import org.apache.solr.common.SolrInputField;+import org.apache.solr.common.util.NamedList;+import org.apache.solr.request.SolrQueryRequest;+import org.apache.solr.response.SolrQueryResponse;+import org.apache.solr.update.AddUpdateCommand;+import org.apache.solr.update.DeleteUpdateCommand;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++/**+ * <p>+ * In the FieldNameMutatingUpdateProcessorFactory configured below,+ * fields names will be mutated if the name contains space.+ * Use multiple instances of this processor for multiple replacements+ * </p>+ * <pre class="prettyprint">+ * <processor class="solr.FieldNameMutatingUpdateProcessorFactory">+ * <str name="pattern ">\s</str>+ * <str name="replacement">_</str>+ * </processor></pre>+ */++public class FieldNameMutatingUpdateProcessorFactory extends UpdateRequestProcessorFactory{+ public static final Logger log = LoggerFactory.getLogger(FieldNameMutatingUpdateProcessorFactory.class);++ private String sourcePattern, replacement;+ private Pattern pattern;+++ @Override+ public UpdateRequestProcessor getInstance(SolrQueryRequest req, SolrQueryResponse rsp, UpdateRequestProcessor next) {+ return new UpdateRequestProcessor(next) {+ @Override+ public void processAdd(AddUpdateCommand cmd) throws IOException {+ final SolrInputDocument doc = cmd.getSolrInputDocument();+ final Collection<String> fieldNames+ = new ArrayList<>(doc.getFieldNames());++ for (final String fname : fieldNames) {+ Matcher matcher = pattern.matcher(fname);+ if(matcher.find() ){+ String newFieldName = matcher.replaceAll(replacement);+ if(!newFieldName.equals(fname)){+ SolrInputField old = doc.remove(fname);+ old.setName(newFieldName);+ doc.put(newFieldName, old);+ }+ }+ }++ super.processAdd(cmd);+ }++ @Override+ public void processDelete(DeleteUpdateCommand cmd) throws IOException {+ super.processDelete(cmd);+ }+ };+ }++ @Override+ public void init(NamedList args) {+ sourcePattern = (String) args.get("pattern");+ replacement = (String) args.get("replacement");+ if(sourcePattern ==null || replacement == null) {+ throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,"'pattern' and 'replacement' are required values");+ }+ try {+ pattern = Pattern.compile(sourcePattern);+ } catch (Exception e) {+ throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,"invalid pattern "+ sourcePattern );+ }+ super.init(args);+ }+}diff --git a/solr/server/solr/configsets/data_driven_schema_configs/conf/solrconfig.xml b/solr/server/solr/configsets/data_driven_schema_configs/conf/solrconfig.xmlindex feddc5d6d07..81ad6c918eb 100755--- a/solr/server/solr/configsets/data_driven_schema_configs/conf/solrconfig.xml+++ b/solr/server/solr/configsets/data_driven_schema_configs/conf/solrconfig.xml@@ -1378,6 +1378,10 @@<processor class="solr.LogUpdateProcessorFactory"/><processor class="solr.DistributedUpdateProcessorFactory"/><processor class="solr.RemoveBlankFieldUpdateProcessorFactory"/>+ <processor class="solr.FieldNameMutatingUpdateProcessorFactory">+ <str name="pattern">[^\w-]</str>+ <str name="replacement">_</str>+ </processor><processor class="solr.ParseBooleanFieldUpdateProcessorFactory"/><processor class="solr.ParseLongFieldUpdateProcessorFactory"/><processor class="solr.ParseDoubleFieldUpdateProcessorFactory"/>diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTestsBase.java b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTestsBase.javaindex 0ee63d18f08..d6bb4ac2b32 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTestsBase.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTestsBase.java@@ -258,12 +258,14 @@ abstract public class SolrExampleTestsBase extends SolrJettyTestBase {assertEquals(10, cnt.get());}- protected void assertNumFound(String query, int num)+ protected QueryResponse assertNumFound(String query, int num)throws SolrServerException, IOException {QueryResponse rsp = getSolrClient().query(new SolrQuery(query));if (num != rsp.getResults().getNumFound()) {fail("expected: " + num + " but had: " + rsp.getResults().getNumFound()+ " :: " + rsp.getResults());}+ return rsp;+}}diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrSchemalessExampleTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrSchemalessExampleTest.javaindex 49879e7b705..6e6465982ab 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrSchemalessExampleTest.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrSchemalessExampleTest.java@@ -25,6 +25,8 @@ import org.apache.http.entity.InputStreamEntity;import org.apache.solr.client.solrj.impl.BinaryRequestWriter;import org.apache.solr.client.solrj.impl.BinaryResponseParser;import org.apache.solr.client.solrj.impl.HttpSolrClient;+import org.apache.solr.client.solrj.response.QueryResponse;+import org.apache.solr.common.SolrDocument;import org.apache.solr.util.ExternalPaths;import org.junit.BeforeClass;import org.junit.Test;@@ -34,7 +36,11 @@ import org.slf4j.LoggerFactory;import java.io.ByteArrayInputStream;import java.io.File;import java.io.OutputStreamWriter;+import java.util.Arrays;+import java.util.HashSet;+import java.util.List;import java.util.Properties;+import java.util.Set;public class SolrSchemalessExampleTest extends SolrExampleTestsBase {private static Logger log = LoggerFactory.getLogger(SolrSchemalessExampleTest.class);@@ -83,6 +89,44 @@ public class SolrSchemalessExampleTest extends SolrExampleTestsBase {assertNumFound("*:*", 2);}+ @Test+ public void testFieldMutating() throws Exception {+ HttpSolrClient client = (HttpSolrClient) getSolrClient();+ client.deleteByQuery("*:*");+ client.commit();+ assertNumFound("*:*", 0); // make sure it got in+ // two docs, one with uniqueKey, another without it+ String json = "{\"name one\": \"name\"} " ++ "{\"name two\" : \"name\"}" ++ "{\"first-second\" : \"name\"}" ++ "{\"x+y\" : \"name\"}" ++ "{\"p%q\" : \"name\"}" ++ "{\"a&b\" : \"name\"}"+ ;+ HttpClient httpClient = client.getHttpClient();+ HttpPost post = new HttpPost(client.getBaseURL() + "/update/json/docs");+ post.setHeader("Content-Type", "application/json");+ post.setEntity(new InputStreamEntity(new ByteArrayInputStream(json.getBytes("UTF-8")), -1));+ HttpResponse response = httpClient.execute(post);+ assertEquals(200, response.getStatusLine().getStatusCode());+ client.commit();+ List<String> expected = Arrays.asList(+ "name_one",+ "name__two",+ "first-second",+ "a_b",+ "p_q",+ "x_y");+ HashSet set = new HashSet();+ QueryResponse rsp = assertNumFound("*:*", expected.size());+ for (SolrDocument doc : rsp.getResults()) set.addAll(doc.getFieldNames());+ for (String s : expected) {+ assertTrue(s+" not created "+ rsp ,set.contains(s) );+ }++ }++@Overridepublic SolrClient createNewSolrClient() {