Merge branch 'master' into feature/search-request-refactoring

This commit is contained in:
Colin Goodheart-Smithe 2015-10-13 08:18:17 +01:00
commit ca76712de6
6 changed files with 175 additions and 14 deletions

View File

@ -48,7 +48,6 @@ public class AnalysisService extends AbstractIndexComponent implements Closeable
private final Map<String, CharFilterFactory> charFilters;
private final Map<String, TokenFilterFactory> tokenFilters;
private final NamedAnalyzer defaultAnalyzer;
private final NamedAnalyzer defaultIndexAnalyzer;
private final NamedAnalyzer defaultSearchAnalyzer;
private final NamedAnalyzer defaultSearchQuoteAnalyzer;
@ -263,12 +262,20 @@ public class AnalysisService extends AbstractIndexComponent implements Closeable
}
}
defaultAnalyzer = analyzers.get("default");
NamedAnalyzer defaultAnalyzer = analyzers.get("default");
if (defaultAnalyzer == null) {
throw new IllegalArgumentException("no default analyzer configured");
}
defaultIndexAnalyzer = analyzers.containsKey("default_index") ? analyzers.get("default_index") : analyzers.get("default");
defaultSearchAnalyzer = analyzers.containsKey("default_search") ? analyzers.get("default_search") : analyzers.get("default");
if (analyzers.containsKey("default_index")) {
final Version createdVersion = Version.indexCreated(indexSettings);
if (createdVersion.onOrAfter(Version.V_3_0_0)) {
throw new IllegalArgumentException("setting [index.analysis.analyzer.default_index] is not supported anymore, use [index.analysis.analyzer.default] instead for index [" + index.getName() + "]");
} else {
deprecationLogger.deprecated("setting [index.analysis.analyzer.default_index] is deprecated, use [index.analysis.analyzer.default] instead for index [{}]", index.getName());
}
}
defaultIndexAnalyzer = analyzers.containsKey("default_index") ? analyzers.get("default_index") : defaultAnalyzer;
defaultSearchAnalyzer = analyzers.containsKey("default_search") ? analyzers.get("default_search") : defaultAnalyzer;
defaultSearchQuoteAnalyzer = analyzers.containsKey("default_search_quote") ? analyzers.get("default_search_quote") : defaultSearchAnalyzer;
for (Map.Entry<String, NamedAnalyzer> analyzer : analyzers.entrySet()) {
@ -299,10 +306,6 @@ public class AnalysisService extends AbstractIndexComponent implements Closeable
return analyzers.get(name);
}
public NamedAnalyzer defaultAnalyzer() {
return defaultAnalyzer;
}
public NamedAnalyzer defaultIndexAnalyzer() {
return defaultIndexAnalyzer;
}

View File

@ -0,0 +1,124 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch 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.elasticsearch.index.analysis;
import org.apache.lucene.analysis.en.EnglishAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.Index;
import org.elasticsearch.indices.analysis.IndicesAnalysisService;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.VersionUtils;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static org.hamcrest.Matchers.instanceOf;
public class AnalysisServiceTests extends ESTestCase {
private static AnalyzerProviderFactory analyzerProvider(final String name) {
return new AnalyzerProviderFactory() {
@Override
public AnalyzerProvider create(String name, Settings settings) {
return new PreBuiltAnalyzerProvider(name, AnalyzerScope.INDEX, new EnglishAnalyzer());
}
};
}
public void testDefaultAnalyzers() {
Version version = VersionUtils.randomVersion(getRandom());
Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
IndicesAnalysisService indicesAnalysisService = new IndicesAnalysisService(settings);
AnalysisService analysisService = new AnalysisService(new Index("index"), settings, indicesAnalysisService,
Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap());
assertThat(analysisService.defaultIndexAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class));
assertThat(analysisService.defaultSearchAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class));
assertThat(analysisService.defaultSearchQuoteAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class));
}
public void testOverrideDefaultAnalyzer() {
Version version = VersionUtils.randomVersion(getRandom());
Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
IndicesAnalysisService indicesAnalysisService = new IndicesAnalysisService(settings);
AnalysisService analysisService = new AnalysisService(new Index("index"), settings, indicesAnalysisService,
Collections.singletonMap("default", analyzerProvider("default")),
Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap());
assertThat(analysisService.defaultIndexAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class));
assertThat(analysisService.defaultSearchAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class));
assertThat(analysisService.defaultSearchQuoteAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class));
}
public void testOverrideDefaultIndexAnalyzer() {
Version version = VersionUtils.randomVersionBetween(getRandom(), Version.V_3_0_0, Version.CURRENT);
Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
IndicesAnalysisService indicesAnalysisService = new IndicesAnalysisService(settings);
try {
AnalysisService analysisService = new AnalysisService(new Index("index"), settings, indicesAnalysisService,
Collections.singletonMap("default_index", new PreBuiltAnalyzerProviderFactory("default_index", AnalyzerScope.INDEX, new EnglishAnalyzer())),
Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap());
fail("Expected ISE");
} catch (IllegalArgumentException e) {
// expected
assertTrue(e.getMessage().contains("[index.analysis.analyzer.default_index] is not supported"));
}
}
public void testBackCompatOverrideDefaultIndexAnalyzer() {
Version version = VersionUtils.randomVersionBetween(getRandom(), VersionUtils.getFirstVersion(), VersionUtils.getPreviousVersion(Version.V_3_0_0));
Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
IndicesAnalysisService indicesAnalysisService = new IndicesAnalysisService(settings);
AnalysisService analysisService = new AnalysisService(new Index("index"), settings, indicesAnalysisService,
Collections.singletonMap("default_index", analyzerProvider("default_index")),
Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap());
assertThat(analysisService.defaultIndexAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class));
assertThat(analysisService.defaultSearchAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class));
assertThat(analysisService.defaultSearchQuoteAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class));
}
public void testOverrideDefaultSearchAnalyzer() {
Version version = VersionUtils.randomVersion(getRandom());
Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
IndicesAnalysisService indicesAnalysisService = new IndicesAnalysisService(settings);
AnalysisService analysisService = new AnalysisService(new Index("index"), settings, indicesAnalysisService,
Collections.singletonMap("default_search", analyzerProvider("default_search")),
Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap());
assertThat(analysisService.defaultIndexAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class));
assertThat(analysisService.defaultSearchAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class));
assertThat(analysisService.defaultSearchQuoteAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class));
}
public void testBackCompatOverrideDefaultIndexAndSearchAnalyzer() {
Version version = VersionUtils.randomVersionBetween(getRandom(), VersionUtils.getFirstVersion(), VersionUtils.getPreviousVersion(Version.V_3_0_0));
Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
IndicesAnalysisService indicesAnalysisService = new IndicesAnalysisService(settings);
Map<String, AnalyzerProviderFactory> analyzers = new HashMap<>();
analyzers.put("default_index", analyzerProvider("default_index"));
analyzers.put("default_search", analyzerProvider("default_search"));
AnalysisService analysisService = new AnalysisService(new Index("index"), settings, indicesAnalysisService,
analyzers, Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap());
assertThat(analysisService.defaultIndexAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class));
assertThat(analysisService.defaultSearchAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class));
assertThat(analysisService.defaultSearchQuoteAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class));
}
}

View File

@ -20,6 +20,7 @@
package org.elasticsearch.index.query;
import com.carrotsearch.randomizedtesting.generators.CodepointSetGenerator;
import com.fasterxml.jackson.core.io.JsonStringEncoder;
import org.apache.lucene.search.Query;
import org.elasticsearch.Version;
@ -552,7 +553,13 @@ public abstract class AbstractQueryTestCase<QB extends AbstractQueryBuilder<QB>>
Object value;
switch (fieldName) {
case STRING_FIELD_NAME:
value = rarely() ? randomUnicodeOfLength(10) : randomAsciiOfLengthBetween(1, 10); // unicode in 10% cases
if (rarely()) {
// unicode in 10% cases
JsonStringEncoder encoder = JsonStringEncoder.getInstance();
value = new String(encoder.quoteAsString(randomUnicodeOfLength(10)));
} else {
value = randomAsciiOfLengthBetween(1, 10);
}
break;
case INT_FIELD_NAME:
value = randomIntBetween(0, 10);

View File

@ -19,6 +19,8 @@
package org.elasticsearch.index.query;
import com.fasterxml.jackson.core.io.JsonStringEncoder;
import org.junit.Test;
import java.util.HashMap;
@ -45,7 +47,8 @@ public abstract class AbstractTermQueryTestCase<QB extends BaseTermQueryBuilder<
value = randomAsciiOfLengthBetween(1, 10);
} else {
// generate unicode string in 10% of cases
value = randomUnicodeOfLength(10);
JsonStringEncoder encoder = JsonStringEncoder.getInstance();
value = new String(encoder.quoteAsString(randomUnicodeOfLength(10)));
}
break;
case 2:
@ -99,7 +102,13 @@ public abstract class AbstractTermQueryTestCase<QB extends BaseTermQueryBuilder<
QB tempQuery = createTestQueryBuilder();
QB testQuery = createQueryBuilder(tempQuery.fieldName(), tempQuery.value());
boolean isString = testQuery.value() instanceof String;
String value = (isString ? "\"" : "") + testQuery.value() + (isString ? "\"" : "");
Object value;
if (isString) {
JsonStringEncoder encoder = JsonStringEncoder.getInstance();
value = "\"" + new String(encoder.quoteAsString((String) testQuery.value())) + "\"";
} else {
value = testQuery.value();
}
String contentString = "{\n" +
" \"" + testQuery.getName() + "\" : {\n" +
" \"" + testQuery.fieldName() + "\" : " + value + "\n" +

View File

@ -23,10 +23,22 @@ defined, defaults are used. There is an option to define which analyzers
will be used by default when none can be derived.
The `default` logical name allows one to configure an analyzer that will
be used both for indexing and for searching APIs. The `default_index`
be used both for indexing and for searching APIs. The `default_search`
logical name can be used to configure a default analyzer that will be
used just when indexing, and the `default_search` can be used to
configure a default analyzer that will be used just when searching.
used just when searching (the `default` analyzer would still be used for
indexing).
For instance, the following settings could be used to perform exact matching
only by default:
[source,js]
--------------------------------------------------
index :
analysis :
analyzer :
default :
tokenizer : keyword
--------------------------------------------------
[float]
[[aliasing-analyzers]]

View File

@ -91,6 +91,12 @@ is needed then the `max_children` option shouldn't be defined at all on the `has
=== Settings changes ===
==== Analysis settings
The `index.analysis.analyzer.default_index` analyzer is not supported anymore.
If you wish to change the analyzer to use for indexing, change the
`index.analysis.analyzer.default` analyzer instead.
==== Ping timeout settings
Previously, there were three settings for the ping timeout: `discovery.zen.initial_ping_timeout`,