field name lookup: return List instead of Set for names matching a pattern

The returned sets are only used for iterating. Therefore we might
as well return a list since this guaratees order.

This is the same effect as in
https://github.com/elasticsearch/elasticsearch/pull/7698
The test SimpleIndexQueryParserTests#testQueryStringFieldsMatch
failed on openjdk 1.7.0_65 with
<jdk.map.althashing.threshold>0</jdk.map.althashing.threshold>

closes #7709
This commit is contained in:
Britta Weber 2014-09-12 15:36:15 +02:00
parent 7feb742a9b
commit 526b464025
8 changed files with 24 additions and 20 deletions

View File

@ -101,11 +101,11 @@ public class DocumentFieldMappers implements Iterable<FieldMapper> {
return fieldMappers.fullName(fullName); return fieldMappers.fullName(fullName);
} }
public Set<String> simpleMatchToIndexNames(String pattern) { public List<String> simpleMatchToIndexNames(String pattern) {
return fieldMappers.simpleMatchToIndexNames(pattern); return fieldMappers.simpleMatchToIndexNames(pattern);
} }
public Set<String> simpleMatchToFullName(String pattern) { public List<String> simpleMatchToFullName(String pattern) {
return fieldMappers.simpleMatchToFullName(pattern); return fieldMappers.simpleMatchToFullName(pattern);
} }

View File

@ -189,10 +189,10 @@ public class FieldMappersLookup implements Iterable<FieldMapper> {
} }
/** /**
* Returns a set of the index names of a simple match regex like pattern against full name, name and index name. * Returns a list of the index names of a simple match regex like pattern against full name, name and index name.
*/ */
public Set<String> simpleMatchToIndexNames(String pattern) { public List<String> simpleMatchToIndexNames(String pattern) {
Set<String> fields = Sets.newHashSet(); List<String> fields = Lists.newArrayList();
for (FieldMapper fieldMapper : mappers) { for (FieldMapper fieldMapper : mappers) {
if (Regex.simpleMatch(pattern, fieldMapper.names().fullName())) { if (Regex.simpleMatch(pattern, fieldMapper.names().fullName())) {
fields.add(fieldMapper.names().indexName()); fields.add(fieldMapper.names().indexName());
@ -206,10 +206,10 @@ public class FieldMappersLookup implements Iterable<FieldMapper> {
} }
/** /**
* Returns a set of the full names of a simple match regex like pattern against full name, name and index name. * Returns a list of the full names of a simple match regex like pattern against full name, name and index name.
*/ */
public Set<String> simpleMatchToFullName(String pattern) { public List<String> simpleMatchToFullName(String pattern) {
Set<String> fields = Sets.newHashSet(); List<String> fields = Lists.newArrayList();
for (FieldMapper fieldMapper : mappers) { for (FieldMapper fieldMapper : mappers) {
if (Regex.simpleMatch(pattern, fieldMapper.names().fullName())) { if (Regex.simpleMatch(pattern, fieldMapper.names().fullName())) {
fields.add(fieldMapper.names().fullName()); fields.add(fieldMapper.names().fullName());

View File

@ -612,7 +612,7 @@ public class MapperService extends AbstractIndexComponent {
* Returns all the fields that match the given pattern, with an optional narrowing * Returns all the fields that match the given pattern, with an optional narrowing
* based on a list of types. * based on a list of types.
*/ */
public Set<String> simpleMatchToIndexNames(String pattern, @Nullable String[] types) { public List<String> simpleMatchToIndexNames(String pattern, @Nullable String[] types) {
if (types == null || types.length == 0) { if (types == null || types.length == 0) {
return simpleMatchToIndexNames(pattern); return simpleMatchToIndexNames(pattern);
} }
@ -620,9 +620,9 @@ public class MapperService extends AbstractIndexComponent {
return simpleMatchToIndexNames(pattern); return simpleMatchToIndexNames(pattern);
} }
if (!Regex.isSimpleMatchPattern(pattern)) { if (!Regex.isSimpleMatchPattern(pattern)) {
return ImmutableSet.of(pattern); return ImmutableList.of(pattern);
} }
Set<String> fields = Sets.newHashSet(); List<String> fields = Lists.newArrayList();
for (String type : types) { for (String type : types) {
DocumentMapper possibleDocMapper = mappers.get(type); DocumentMapper possibleDocMapper = mappers.get(type);
if (possibleDocMapper != null) { if (possibleDocMapper != null) {
@ -638,16 +638,16 @@ public class MapperService extends AbstractIndexComponent {
* Returns all the fields that match the given pattern. If the pattern is prefixed with a type * Returns all the fields that match the given pattern. If the pattern is prefixed with a type
* then the fields will be returned with a type prefix. * then the fields will be returned with a type prefix.
*/ */
public Set<String> simpleMatchToIndexNames(String pattern) { public List<String> simpleMatchToIndexNames(String pattern) {
if (!Regex.isSimpleMatchPattern(pattern)) { if (!Regex.isSimpleMatchPattern(pattern)) {
return ImmutableSet.of(pattern); return ImmutableList.of(pattern);
} }
int dotIndex = pattern.indexOf('.'); int dotIndex = pattern.indexOf('.');
if (dotIndex != -1) { if (dotIndex != -1) {
String possibleType = pattern.substring(0, dotIndex); String possibleType = pattern.substring(0, dotIndex);
DocumentMapper possibleDocMapper = mappers.get(possibleType); DocumentMapper possibleDocMapper = mappers.get(possibleType);
if (possibleDocMapper != null) { if (possibleDocMapper != null) {
Set<String> typedFields = Sets.newHashSet(); List<String> typedFields = Lists.newArrayList();
for (String indexName : possibleDocMapper.mappers().simpleMatchToIndexNames(pattern)) { for (String indexName : possibleDocMapper.mappers().simpleMatchToIndexNames(pattern)) {
typedFields.add(possibleType + "." + indexName); typedFields.add(possibleType + "." + indexName);
} }

View File

@ -32,6 +32,7 @@ import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.internal.FieldNamesFieldMapper; import org.elasticsearch.index.mapper.internal.FieldNamesFieldMapper;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import java.util.Set; import java.util.Set;
import static org.elasticsearch.index.query.support.QueryParsers.wrapSmartNameFilter; import static org.elasticsearch.index.query.support.QueryParsers.wrapSmartNameFilter;
@ -91,7 +92,7 @@ public class ExistsFilterParser implements FilterParser {
fieldPattern = fieldPattern + ".*"; fieldPattern = fieldPattern + ".*";
} }
Set<String> fields = parseContext.simpleMatchToIndexNames(fieldPattern); List<String> fields = parseContext.simpleMatchToIndexNames(fieldPattern);
if (fields.isEmpty()) { if (fields.isEmpty()) {
// no fields exists, so we should not match anything // no fields exists, so we should not match anything
return Queries.MATCH_NO_FILTER; return Queries.MATCH_NO_FILTER;

View File

@ -33,6 +33,7 @@ import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.internal.FieldNamesFieldMapper; import org.elasticsearch.index.mapper.internal.FieldNamesFieldMapper;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import java.util.Set; import java.util.Set;
import static org.elasticsearch.index.query.support.QueryParsers.wrapSmartNameFilter; import static org.elasticsearch.index.query.support.QueryParsers.wrapSmartNameFilter;
@ -103,7 +104,7 @@ public class MissingFilterParser implements FilterParser {
fieldPattern = fieldPattern + ".*"; fieldPattern = fieldPattern + ".*";
} }
Set<String> fields = parseContext.simpleMatchToIndexNames(fieldPattern); List<String> fields = parseContext.simpleMatchToIndexNames(fieldPattern);
if (fields.isEmpty()) { if (fields.isEmpty()) {
if (existence) { if (existence) {
// if we ask for existence of fields, and we found none, then we should match on all // if we ask for existence of fields, and we found none, then we should match on all

View File

@ -327,7 +327,7 @@ public class QueryParseContext {
return smartMapper.names().indexName(); return smartMapper.names().indexName();
} }
public Set<String> simpleMatchToIndexNames(String pattern) { public List<String> simpleMatchToIndexNames(String pattern) {
return indexQueryParser.mapperService.simpleMatchToIndexNames(pattern, getTypes()); return indexQueryParser.mapperService.simpleMatchToIndexNames(pattern, getTypes());
} }

View File

@ -19,6 +19,7 @@
package org.elasticsearch.search.highlight; package org.elasticsearch.search.highlight;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import org.apache.lucene.index.FieldInfo; import org.apache.lucene.index.FieldInfo;
@ -37,6 +38,7 @@ import org.elasticsearch.search.fetch.FetchSubPhase;
import org.elasticsearch.search.internal.InternalSearchHit; import org.elasticsearch.search.internal.InternalSearchHit;
import org.elasticsearch.search.internal.SearchContext; import org.elasticsearch.search.internal.SearchContext;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -78,12 +80,12 @@ public class HighlightPhase extends AbstractComponent implements FetchSubPhase {
public void hitExecute(SearchContext context, HitContext hitContext) throws ElasticsearchException { public void hitExecute(SearchContext context, HitContext hitContext) throws ElasticsearchException {
Map<String, HighlightField> highlightFields = newHashMap(); Map<String, HighlightField> highlightFields = newHashMap();
for (SearchContextHighlight.Field field : context.highlight().fields()) { for (SearchContextHighlight.Field field : context.highlight().fields()) {
Set<String> fieldNamesToHighlight; List<String> fieldNamesToHighlight;
if (Regex.isSimpleMatchPattern(field.field())) { if (Regex.isSimpleMatchPattern(field.field())) {
DocumentMapper documentMapper = context.mapperService().documentMapper(hitContext.hit().type()); DocumentMapper documentMapper = context.mapperService().documentMapper(hitContext.hit().type());
fieldNamesToHighlight = documentMapper.mappers().simpleMatchToFullName(field.field()); fieldNamesToHighlight = documentMapper.mappers().simpleMatchToFullName(field.field());
} else { } else {
fieldNamesToHighlight = ImmutableSet.of(field.field()); fieldNamesToHighlight = ImmutableList.of(field.field());
} }
if (context.highlight().forceSource(field)) { if (context.highlight().forceSource(field)) {

View File

@ -853,7 +853,7 @@ public abstract class ElasticsearchIntegrationTest extends ElasticsearchTestCase
DocumentMapper documentMapper = indexService.mapperService().documentMapper(type); DocumentMapper documentMapper = indexService.mapperService().documentMapper(type);
assertThat("document mapper doesn't exists on " + node, documentMapper, notNullValue()); assertThat("document mapper doesn't exists on " + node, documentMapper, notNullValue());
for (String fieldName : fieldNames) { for (String fieldName : fieldNames) {
Set<String> matches = documentMapper.mappers().simpleMatchToFullName(fieldName); List<String> matches = documentMapper.mappers().simpleMatchToFullName(fieldName);
assertThat("field " + fieldName + " doesn't exists on " + node, matches, Matchers.not(emptyIterable())); assertThat("field " + fieldName + " doesn't exists on " + node, matches, Matchers.not(emptyIterable()));
} }
} }