Skip to content
Browse files

spring-elasticsearch-21 _settings are not applied when no mapping is …

…defined fix #21
  • Loading branch information...
1 parent 3b1dfbf commit b2f6e225d2667ee87616355b5b4063a70b1e4c35 @dadoonet committed Jan 23, 2013
View
36 src/main/java/fr/pilato/spring/elasticsearch/ElasticsearchAbstractClientFactoryBean.java
@@ -407,9 +407,16 @@ private void computeMappings() {
// We should ignore _settings.json files (as they are not really mappings)
// We should also ignore _template dir
- if (!relPath.endsWith(indexSettingsFileName) && !relPath.startsWith(templateDir)) {
+ if (!relPath.startsWith(templateDir)) {
// We must remove the .json extension
- relPath = relPath.substring(0, relPath.lastIndexOf(".json"));
+
+ // Issue #21: If there are only _settings.json and no mapping
+ // we should manage it also
+ if (!relPath.endsWith(indexSettingsFileName)) {
+ relPath = relPath.substring(0, relPath.lastIndexOf(".json"));
+ } else {
+ relPath = relPath.substring(0, relPath.lastIndexOf(indexSettingsFileName));
+ }
autoMappings.add(relPath);
if (logger.isDebugEnabled()) {
@@ -445,18 +452,19 @@ private void initMappings() throws Exception {
String indexmapping = mappings[i];
String[] indexmappingsplitted = indexmapping.split("/");
String index = indexmappingsplitted[0];
- String mapping = indexmappingsplitted[1];
-
- if (index == null) throw new Exception("Can not read index in [" + indexmapping +
- "]. Check that mappings contains only indexname/mappingname elements.");
- if (mapping == null) throw new Exception("Can not read mapping in [" + indexmapping +
- "]. Check that mappings contains only indexname/mappingname elements.");
-
- // We add the mapping in the collection of its index
- if (!indexes.containsKey(index)) {
- indexes.put(index, new ArrayList<String>());
- }
- indexes.get(index).add(mapping);
+
+ if (index == null) throw new Exception("Can not read index in [" + indexmapping +
+ "]. Check that mappings contains only indexname/mappingname elements.");
+
+ // We add the mapping in the collection of its index
+ if (!indexes.containsKey(index)) {
+ indexes.put(index, new ArrayList<String>());
+ }
+
+ if (indexmappingsplitted.length > 1) {
+ String mapping = indexmappingsplitted[1];
+ indexes.get(index).add(mapping);
+ }
}
// Let's initialize indexes and mappings if needed
View
58 src/test/java/fr/pilato/spring/elasticsearch/xml/ElasticsearchSettingsNoMapping21Test.java
@@ -0,0 +1,58 @@
+/*
+ * Licensed to David Pilato (the "Author") under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. Author 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 fr.pilato.spring.elasticsearch.xml;
+
+import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
+import org.elasticsearch.client.Client;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+
+public class ElasticsearchSettingsNoMapping21Test {
+ static protected ConfigurableApplicationContext ctx;
+
+ @BeforeClass
+ static public void setup() {
+ ctx = new ClassPathXmlApplicationContext("/fr/pilato/spring/elasticsearch/xml/es-settings21-test-context.xml");
+ }
+
+ @AfterClass
+ static public void tearDown() {
+ if (ctx != null) {
+ ctx.close();
+ }
+ }
+
+ @Test
+ public void test_transport_client() {
+ Client client = ctx.getBean("esClient", Client.class);
+ assertNotNull("Client must not be null...", client);
+
+ // We should have an existing index here
+ IndicesExistsResponse ier = client.admin().indices().prepareExists("twitter").execute().actionGet();
+ assertTrue(ier.exists());
+ }
+}
View
2 src/test/resources/es_issue_21/twitter/_settings.json
@@ -0,0 +1,2 @@
+{
+}
View
31 src/test/resources/fr/pilato/spring/elasticsearch/xml/es-settings21-test-context.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Licensed to David Pilato (the "Author") under one or more contributor
+ license agreements. See the NOTICE file distributed with this work for additional
+ information regarding copyright ownership. Author 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. -->
+
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:context="http://www.springframework.org/schema/context"
+ xmlns:util="http://www.springframework.org/schema/util"
+ xmlns:elasticsearch="http://www.pilato.fr/schema/elasticsearch"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+ http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
+ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
+ http://www.pilato.fr/schema/elasticsearch http://www.pilato.fr/schema/elasticsearch/elasticsearch-0.1.xsd">
+
+ <elasticsearch:node id="esNode" settingsFile="fr/pilato/spring/elasticsearch/xml/esnode-transport.properties" />
+
+ <bean id="esClient"
+ class="fr.pilato.spring.elasticsearch.ElasticsearchClientFactoryBean" >
+ <property name="node" ref="esNode" />
+ <property name="classpathRoot" value="/es_issue_21" />
+ </bean>
+
+</beans>

0 comments on commit b2f6e22

Please sign in to comment.
Something went wrong with that request. Please try again.