Skip to content

Commit 22a29cf

Browse files
committed
refactor inner classes to upper level
1 parent cf2671e commit 22a29cf

File tree

4 files changed

+211
-178
lines changed

4 files changed

+211
-178
lines changed

server/src/internalClusterTest/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterSearchIT.java

Lines changed: 2 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,9 @@
1313
import org.apache.lucene.analysis.TokenFilter;
1414
import org.apache.lucene.analysis.TokenStream;
1515
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
16-
import org.apache.lucene.index.IndexReader;
17-
import org.apache.lucene.index.TermsEnum;
18-
import org.apache.lucene.search.MatchAllDocsQuery;
19-
import org.apache.lucene.search.MatchNoDocsQuery;
20-
import org.apache.lucene.search.Query;
2116
import org.apache.lucene.search.join.ScoreMode;
2217
import org.apache.lucene.tests.analysis.MockAnalyzer;
2318
import org.apache.lucene.tests.analysis.MockTokenizer;
24-
import org.apache.lucene.util.BytesRef;
2519
import org.elasticsearch.action.index.IndexRequestBuilder;
2620
import org.elasticsearch.action.search.SearchRequest;
2721
import org.elasticsearch.action.search.SearchRequestBuilder;
@@ -30,7 +24,6 @@
3024
import org.elasticsearch.common.Strings;
3125
import org.elasticsearch.common.geo.GeoPoint;
3226
import org.elasticsearch.common.lucene.Lucene;
33-
import org.elasticsearch.common.regex.Regex;
3427
import org.elasticsearch.common.settings.Settings;
3528
import org.elasticsearch.common.settings.Settings.Builder;
3629
import org.elasticsearch.common.time.DateFormatter;
@@ -39,36 +32,24 @@
3932
import org.elasticsearch.index.analysis.AnalyzerProvider;
4033
import org.elasticsearch.index.analysis.NamedAnalyzer;
4134
import org.elasticsearch.index.analysis.PreConfiguredTokenFilter;
42-
import org.elasticsearch.index.fielddata.FieldData;
43-
import org.elasticsearch.index.fielddata.FieldDataContext;
44-
import org.elasticsearch.index.fielddata.IndexFieldData;
45-
import org.elasticsearch.index.fielddata.plain.ConstantIndexFieldData;
46-
import org.elasticsearch.index.mapper.ConstantFieldType;
4735
import org.elasticsearch.index.mapper.DocumentParserContext;
4836
import org.elasticsearch.index.mapper.FieldMapper;
49-
import org.elasticsearch.index.mapper.KeywordFieldMapper;
50-
import org.elasticsearch.index.mapper.Mapper;
5137
import org.elasticsearch.index.mapper.MapperBuilderContext;
5238
import org.elasticsearch.index.mapper.MapperParsingException;
53-
import org.elasticsearch.index.mapper.ValueFetcher;
5439
import org.elasticsearch.index.query.AbstractQueryBuilder;
5540
import org.elasticsearch.index.query.CombinedFieldsQueryBuilder;
5641
import org.elasticsearch.index.query.IdsQueryBuilder;
5742
import org.elasticsearch.index.query.MatchQueryBuilder;
5843
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
5944
import org.elasticsearch.index.query.QueryBuilder;
6045
import org.elasticsearch.index.query.QueryBuilders;
61-
import org.elasticsearch.index.query.SearchExecutionContext;
6246
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
6347
import org.elasticsearch.index.query.functionscore.RandomScoreFunctionBuilder;
6448
import org.elasticsearch.indices.analysis.AnalysisModule;
6549
import org.elasticsearch.plugins.AnalysisPlugin;
66-
import org.elasticsearch.plugins.MapperPlugin;
6750
import org.elasticsearch.plugins.Plugin;
6851
import org.elasticsearch.rest.RestStatus;
69-
import org.elasticsearch.script.field.KeywordDocValuesField;
7052
import org.elasticsearch.search.SearchHit;
71-
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
7253
import org.elasticsearch.search.builder.SearchSourceBuilder;
7354
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.BoundaryScannerType;
7455
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field;
@@ -3564,7 +3545,7 @@ public Map<String, AnalysisModule.AnalysisProvider<AnalyzerProvider<? extends An
35643545
return singletonMap("mock_whitespace", (indexSettings, environment, name, settings) -> {
35653546
return new AbstractIndexAnalyzerProvider<Analyzer>(name, settings) {
35663547

3567-
final MockAnalyzer instance = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false);
3548+
MockAnalyzer instance = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false);
35683549

35693550
@Override
35703551
public Analyzer get() {
@@ -3613,168 +3594,12 @@ public void testConstantKeywordFieldHighlighting() throws IOException {
36133594
// )
36143595
.get();
36153596
assertNoFailures(search);
3616-
System.out.println(search.getHits());
3597+
36173598
assertThat(
36183599

36193600
search.getHits().getAt(0).getHighlightFields().get("message").getFragments()[0].toString(),
36203601
equalTo("<em>some</em> text")
36213602
);
36223603
}
36233604

3624-
public static class FakeConstantFieldType extends ConstantFieldType {
3625-
3626-
public static final String CONTENT_TYPE = "constant_keyword";
3627-
public final String value;
3628-
3629-
public FakeConstantFieldType(String name, String value, Map<String, String> meta) {
3630-
super(name, meta);
3631-
this.value = value;
3632-
}
3633-
3634-
public String value() {
3635-
return value;
3636-
}
3637-
3638-
@Override
3639-
public String typeName() {
3640-
return CONTENT_TYPE;
3641-
}
3642-
3643-
@Override
3644-
public String familyTypeName() {
3645-
return KeywordFieldMapper.CONTENT_TYPE;
3646-
}
3647-
3648-
@Override
3649-
public IndexFieldData.Builder fielddataBuilder(FieldDataContext fieldDataContext) {
3650-
return new ConstantIndexFieldData.Builder(
3651-
value,
3652-
name(),
3653-
CoreValuesSourceType.KEYWORD,
3654-
(dv, n) -> new KeywordDocValuesField(FieldData.toString(dv), n)
3655-
);
3656-
}
3657-
3658-
@Override
3659-
public ValueFetcher valueFetcher(SearchExecutionContext context, String format) {
3660-
if (format != null) {
3661-
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
3662-
}
3663-
3664-
return value == null ? ValueFetcher.EMPTY : ValueFetcher.singleton(value);
3665-
}
3666-
3667-
@Override
3668-
public Object valueForDisplay(Object value) {
3669-
if (value == null) {
3670-
return null;
3671-
}
3672-
BytesRef binaryValue = (BytesRef) value;
3673-
return binaryValue.utf8ToString();
3674-
}
3675-
3676-
@Override
3677-
public TermsEnum getTerms(IndexReader reader, String prefix, boolean caseInsensitive, String searchAfter) {
3678-
if (value == null) {
3679-
return TermsEnum.EMPTY;
3680-
}
3681-
boolean matches = caseInsensitive
3682-
? value.toLowerCase(Locale.ROOT).startsWith(prefix.toLowerCase(Locale.ROOT))
3683-
: value.startsWith(prefix);
3684-
if (matches == false) {
3685-
return TermsEnum.EMPTY;
3686-
}
3687-
if (searchAfter != null) {
3688-
if (searchAfter.compareTo(value) >= 0) {
3689-
// The constant value is before the searchAfter value so must be ignored
3690-
return TermsEnum.EMPTY;
3691-
}
3692-
}
3693-
return TermsEnum.EMPTY;
3694-
}
3695-
3696-
@Override
3697-
protected boolean matches(String pattern, boolean caseInsensitive, SearchExecutionContext context) {
3698-
if (value == null) {
3699-
return false;
3700-
}
3701-
return Regex.simpleMatch(pattern, value, caseInsensitive);
3702-
}
3703-
3704-
@Override
3705-
public Query existsQuery(SearchExecutionContext context) {
3706-
return value != null ? new MatchAllDocsQuery() : new MatchNoDocsQuery();
3707-
}
3708-
}
3709-
3710-
static class FakeConstantFieldMapper extends FieldMapper {
3711-
@Override
3712-
public FakeConstantFieldType fieldType() {
3713-
return (FakeConstantFieldType) super.fieldType();
3714-
}
3715-
3716-
final String indexedValue;
3717-
public static final TypeParser PARSER = new TypeParser((n, c) -> new FakeConstantFieldMapper.Builder(n));
3718-
3719-
private static FakeConstantFieldMapper toType(FieldMapper in) {
3720-
return (FakeConstantFieldMapper) in;
3721-
}
3722-
3723-
public static class Builder extends FieldMapper.Builder {
3724-
3725-
// This is defined as updateable because it can be updated once, from [null] to any value,
3726-
// by a dynamic mapping update. Once it has been set, however, the value cannot be changed.
3727-
private final Parameter<String> value = new Parameter<>("value", true, () -> null, (n, c, o) -> {
3728-
if (o instanceof Number == false && o instanceof CharSequence == false) {
3729-
throw new MapperParsingException(
3730-
"Property [value] on field [" + n + "] must be a number or a string, but got [" + o + "]"
3731-
);
3732-
}
3733-
return o.toString();
3734-
}, m -> toType(m).fieldType().value, XContentBuilder::field, Objects::toString);
3735-
private final Parameter<Map<String, String>> meta = Parameter.metaParam();
3736-
3737-
protected Builder(String name) {
3738-
super(name);
3739-
value.setSerializerCheck((id, ic, v) -> v != null);
3740-
value.setMergeValidator((previous, current, c) -> previous == null || Objects.equals(previous, current));
3741-
}
3742-
3743-
@Override
3744-
protected Parameter<?>[] getParameters() {
3745-
return new Parameter<?>[] { value, meta };
3746-
}
3747-
3748-
@Override
3749-
public FakeConstantFieldMapper build(MapperBuilderContext context) {
3750-
return new FakeConstantFieldMapper(
3751-
name,
3752-
new FakeConstantFieldType(context.buildFullName(name), value.getValue(), meta.getValue())
3753-
);
3754-
}
3755-
}
3756-
3757-
FakeConstantFieldMapper(String indexedValue, FakeConstantFieldType fieldType) {
3758-
super(fieldType.name(), fieldType, MultiFields.empty(), CopyTo.empty());
3759-
this.indexedValue = indexedValue;
3760-
}
3761-
3762-
@Override
3763-
public Map<String, NamedAnalyzer> indexAnalyzers() {
3764-
return Map.of(mappedFieldType.name(), Lucene.KEYWORD_ANALYZER);
3765-
}
3766-
3767-
@Override
3768-
protected void parseCreateField(DocumentParserContext context) {}
3769-
3770-
@Override
3771-
protected String contentType() {
3772-
return FakeConstantFieldType.CONTENT_TYPE;
3773-
}
3774-
3775-
@Override
3776-
public FakeConstantFieldMapper.Builder getMergeBuilder() {
3777-
return new FakeConstantFieldMapper.Builder(mappedFieldType.name());
3778-
}
3779-
}
37803605
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.search.fetch.subphase.highlight;
10+
11+
import org.elasticsearch.common.lucene.Lucene;
12+
import org.elasticsearch.index.analysis.NamedAnalyzer;
13+
import org.elasticsearch.index.mapper.DocumentParserContext;
14+
import org.elasticsearch.index.mapper.FieldMapper;
15+
import org.elasticsearch.index.mapper.MapperBuilderContext;
16+
import org.elasticsearch.index.mapper.MapperParsingException;
17+
import org.elasticsearch.xcontent.XContentBuilder;
18+
19+
import java.util.Map;
20+
import java.util.Objects;
21+
22+
class MockConstantFieldMapper extends FieldMapper {
23+
@Override
24+
public MockConstantFieldType fieldType() {
25+
return (MockConstantFieldType) super.fieldType();
26+
}
27+
28+
final String indexedValue;
29+
public static final TypeParser PARSER = new TypeParser((n, c) -> new Builder(n));
30+
31+
private static MockConstantFieldMapper toType(FieldMapper in) {
32+
return (MockConstantFieldMapper) in;
33+
}
34+
35+
public static class Builder extends FieldMapper.Builder {
36+
37+
// This is defined as updateable because it can be updated once, from [null] to any value,
38+
// by a dynamic mapping update. Once it has been set, however, the value cannot be changed.
39+
private final Parameter<String> value = new Parameter<>("value", true, () -> null, (n, c, o) -> {
40+
if (o instanceof Number == false && o instanceof CharSequence == false) {
41+
throw new MapperParsingException(
42+
"Property [value] on field [" + n + "] must be a number or a string, but got [" + o + "]"
43+
);
44+
}
45+
return o.toString();
46+
}, m -> toType(m).fieldType().value, XContentBuilder::field, Objects::toString);
47+
private final Parameter<Map<String, String>> meta = Parameter.metaParam();
48+
49+
protected Builder(String name) {
50+
super(name);
51+
value.setSerializerCheck((id, ic, v) -> v != null);
52+
value.setMergeValidator((previous, current, c) -> previous == null || Objects.equals(previous, current));
53+
}
54+
55+
@Override
56+
protected Parameter<?>[] getParameters() {
57+
return new Parameter<?>[]{value, meta};
58+
}
59+
60+
@Override
61+
public MockConstantFieldMapper build(MapperBuilderContext context) {
62+
return new MockConstantFieldMapper(
63+
name,
64+
new MockConstantFieldType(context.buildFullName(name), value.getValue(), meta.getValue())
65+
);
66+
}
67+
}
68+
69+
MockConstantFieldMapper(String indexedValue, MockConstantFieldType fieldType) {
70+
super(fieldType.name(), fieldType, MultiFields.empty(), CopyTo.empty());
71+
this.indexedValue = indexedValue;
72+
}
73+
74+
@Override
75+
public Map<String, NamedAnalyzer> indexAnalyzers() {
76+
return Map.of(mappedFieldType.name(), Lucene.KEYWORD_ANALYZER);
77+
}
78+
79+
@Override
80+
protected void parseCreateField(DocumentParserContext context) {
81+
}
82+
83+
@Override
84+
protected String contentType() {
85+
return MockConstantFieldType.CONTENT_TYPE;
86+
}
87+
88+
@Override
89+
public Builder getMergeBuilder() {
90+
return new Builder(mappedFieldType.name());
91+
}
92+
}

0 commit comments

Comments
 (0)