Skip to content

Commit

Permalink
Internal: deduplicate field names returned by simpleMatchToFullName
Browse files Browse the repository at this point in the history
… & `simpleMatchToIndexNames` in FieldMappersLookup

Relates to #10916
Closes #11377
  • Loading branch information
javanna committed May 28, 2015
1 parent d32a80f commit 2f57ae9
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 53 deletions.
Expand Up @@ -28,7 +28,6 @@

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -98,11 +97,11 @@ public FieldMapper getMapper(String field) {
return fieldMappers.get(field);
}

List<String> simpleMatchToIndexNames(String pattern) {
Collection<String> simpleMatchToIndexNames(String pattern) {
return fieldMappers.simpleMatchToIndexNames(pattern);
}

public List<String> simpleMatchToFullName(String pattern) {
public Collection<String> simpleMatchToFullName(String pattern) {
return fieldMappers.simpleMatchToFullName(pattern);
}

Expand Down
Expand Up @@ -19,15 +19,15 @@

package org.elasticsearch.index.mapper;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.collect.CopyOnWriteHashMap;
import org.elasticsearch.common.regex.Regex;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
* A class that holds a map of field mappers from name, index name, and full name.
Expand Down Expand Up @@ -114,8 +114,8 @@ public FieldMapper get(String field) {
/**
* Returns a list of the index names of a simple match regex like pattern against full name and index name.
*/
public List<String> simpleMatchToIndexNames(String pattern) {
List<String> fields = Lists.newArrayList();
public Collection<String> simpleMatchToIndexNames(String pattern) {
Set<String> fields = Sets.newHashSet();
for (FieldMapper fieldMapper : this) {
if (Regex.simpleMatch(pattern, fieldMapper.names().fullName())) {
fields.add(fieldMapper.names().indexName());
Expand All @@ -129,8 +129,8 @@ public List<String> simpleMatchToIndexNames(String pattern) {
/**
* Returns a list of the full names of a simple match regex like pattern against full name and index name.
*/
public List<String> simpleMatchToFullName(String pattern) {
List<String> fields = Lists.newArrayList();
public Collection<String> simpleMatchToFullName(String pattern) {
Set<String> fields = Sets.newHashSet();
for (FieldMapper fieldMapper : this) {
if (Regex.simpleMatch(pattern, fieldMapper.names().fullName())) {
fields.add(fieldMapper.names().fullName());
Expand Down
Expand Up @@ -481,14 +481,14 @@ public FieldMapper fullName(String fullName) {
* 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.
*/
public List<String> simpleMatchToIndexNames(String pattern) {
public Collection<String> simpleMatchToIndexNames(String pattern) {
return simpleMatchToIndexNames(pattern, null);
}
/**
* Returns all the fields that match the given pattern, with an optional narrowing
* based on a list of types.
*/
public List<String> simpleMatchToIndexNames(String pattern, @Nullable String[] types) {
public Collection<String> simpleMatchToIndexNames(String pattern, @Nullable String[] types) {
if (Regex.isSimpleMatchPattern(pattern) == false) {
// no wildcards
return ImmutableList.of(pattern);
Expand Down
Expand Up @@ -19,11 +19,7 @@

package org.elasticsearch.index.query;

import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermRangeQuery;
import org.apache.lucene.search.*;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.xcontent.XContentParser;
Expand All @@ -32,7 +28,7 @@
import org.elasticsearch.index.mapper.internal.FieldNamesFieldMapper;

import java.io.IOException;
import java.util.List;
import java.util.Collection;

/**
*
Expand Down Expand Up @@ -89,7 +85,7 @@ public static Query newFilter(QueryParseContext parseContext, String fieldPatter
fieldPattern = fieldPattern + ".*";
}

List<String> fields = parseContext.simpleMatchToIndexNames(fieldPattern);
Collection<String> fields = parseContext.simpleMatchToIndexNames(fieldPattern);
if (fields.isEmpty()) {
// no fields exists, so we should not match anything
return Queries.newMatchNoDocsQuery();
Expand Down
Expand Up @@ -19,11 +19,7 @@

package org.elasticsearch.index.query;

import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermRangeQuery;
import org.apache.lucene.search.*;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.xcontent.XContentParser;
Expand All @@ -32,7 +28,7 @@
import org.elasticsearch.index.mapper.internal.FieldNamesFieldMapper;

import java.io.IOException;
import java.util.List;
import java.util.Collection;

/**
*
Expand Down Expand Up @@ -100,7 +96,7 @@ public static Query newFilter(QueryParseContext parseContext, String fieldPatter
fieldPattern = fieldPattern + ".*";
}

List<String> fields = parseContext.simpleMatchToIndexNames(fieldPattern);
Collection<String> fields = parseContext.simpleMatchToIndexNames(fieldPattern);
if (fields.isEmpty()) {
if (existence) {
// if we ask for existence of fields, and we found none, then we should match on all
Expand Down
16 changes: 3 additions & 13 deletions src/main/java/org/elasticsearch/index/query/QueryParseContext.java
Expand Up @@ -21,7 +21,6 @@

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.queryparser.classic.MapperQueryParser;
import org.apache.lucene.queryparser.classic.QueryParserSettings;
Expand All @@ -38,11 +37,7 @@
import org.elasticsearch.index.Index;
import org.elasticsearch.index.analysis.AnalysisService;
import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.mapper.ContentPath;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.Mapper;
import org.elasticsearch.index.mapper.MapperBuilders;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.*;
import org.elasticsearch.index.mapper.core.StringFieldMapper;
import org.elasticsearch.index.query.support.NestedScope;
import org.elasticsearch.index.similarity.SimilarityService;
Expand All @@ -52,12 +47,7 @@
import org.elasticsearch.search.lookup.SearchLookup;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

/**
*
Expand Down Expand Up @@ -281,7 +271,7 @@ public Query parseInnerFilter(String queryName) throws IOException, QueryParsing
}
}

public List<String> simpleMatchToIndexNames(String pattern) {
public Collection<String> simpleMatchToIndexNames(String pattern) {
return indexQueryParser.mapperService.simpleMatchToIndexNames(pattern, getTypes());
}

Expand Down
Expand Up @@ -34,7 +34,7 @@
import org.elasticsearch.search.internal.InternalSearchHit;
import org.elasticsearch.search.internal.SearchContext;

import java.util.List;
import java.util.Collection;
import java.util.Map;

import static com.google.common.collect.Maps.newHashMap;
Expand Down Expand Up @@ -77,7 +77,7 @@ public boolean hitExecutionNeeded(SearchContext context) {
public void hitExecute(SearchContext context, HitContext hitContext) {
Map<String, HighlightField> highlightFields = newHashMap();
for (SearchContextHighlight.Field field : context.highlight().fields()) {
List<String> fieldNamesToHighlight;
Collection<String> fieldNamesToHighlight;
if (Regex.isSimpleMatchPattern(field.field())) {
DocumentMapper documentMapper = context.mapperService().documentMapper(hitContext.hit().type());
fieldNamesToHighlight = documentMapper.mappers().simpleMatchToFullName(field.field());
Expand Down
Expand Up @@ -30,6 +30,7 @@
import org.elasticsearch.test.ElasticsearchTestCase;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

Expand All @@ -39,7 +40,7 @@ public void testEmpty() {
FieldMappersLookup lookup = new FieldMappersLookup();
assertNull(lookup.fullName("foo"));
assertNull(lookup.indexName("foo"));
List<String> names = lookup.simpleMatchToFullName("foo");
Collection<String> names = lookup.simpleMatchToFullName("foo");
assertNotNull(names);
assertTrue(names.isEmpty());
names = lookup.simpleMatchToFullName("foo");
Expand Down Expand Up @@ -105,7 +106,7 @@ public void testSimpleMatchIndexNames() {
FakeFieldMapper f2 = new FakeFieldMapper("bar", "boo");
FieldMappersLookup lookup = new FieldMappersLookup();
lookup = lookup.copyAndAddAll(newList(f1, f2));
List<String> names = lookup.simpleMatchToIndexNames("b*");
Collection<String> names = lookup.simpleMatchToIndexNames("b*");
assertTrue(names.contains("baz"));
assertTrue(names.contains("boo"));
}
Expand All @@ -115,7 +116,7 @@ public void testSimpleMatchFullNames() {
FakeFieldMapper f2 = new FakeFieldMapper("bar", "boo");
FieldMappersLookup lookup = new FieldMappersLookup();
lookup = lookup.copyAndAddAll(newList(f1, f2));
List<String> names = lookup.simpleMatchToFullName("b*");
Collection<String> names = lookup.simpleMatchToFullName("b*");
assertTrue(names.contains("foo"));
assertTrue(names.contains("bar"));
}
Expand Down
Expand Up @@ -145,15 +145,7 @@
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -906,7 +898,7 @@ public void run() {
DocumentMapper documentMapper = indexService.mapperService().documentMapper(type);
assertThat("document mapper doesn't exists on " + node, documentMapper, notNullValue());
for (String fieldName : fieldNames) {
List<String> matches = documentMapper.mappers().simpleMatchToFullName(fieldName);
Collection<String> matches = documentMapper.mappers().simpleMatchToFullName(fieldName);
assertThat("field " + fieldName + " doesn't exists on " + node, matches, Matchers.not(emptyIterable()));
}
}
Expand Down

0 comments on commit 2f57ae9

Please sign in to comment.