cleanup more calls to Term with String value

This commit is contained in:
Shay Banon 2012-12-30 17:31:13 -08:00
parent 1c93c8dfb8
commit f8a08a46ac
13 changed files with 38 additions and 40 deletions

View File

@ -86,7 +86,7 @@ public class TransportExplainAction extends TransportShardSingleOperationAction<
protected ExplainResponse shardOperation(ExplainRequest request, int shardId) throws ElasticSearchException {
IndexService indexService = indicesService.indexService(request.index());
IndexShard indexShard = indexService.shardSafe(shardId);
Term uidTerm = new Term(UidFieldMapper.NAME, Uid.createUid(request.type(), request.id()));
Term uidTerm = new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(request.type(), request.id()));
Engine.GetResult result = indexShard.get(new Engine.Get(false, uidTerm));
if (!result.exists()) {
return new ExplainResponse(false);

View File

@ -143,7 +143,7 @@ public class ShardGetService extends AbstractIndexShardComponent {
Engine.GetResult get = null;
if (type == null || type.equals("_all")) {
for (String typeX : mapperService.types()) {
get = indexShard.get(new Engine.Get(realtime, new Term(UidFieldMapper.NAME, Uid.createUid(typeX, id))).loadSource(loadSource));
get = indexShard.get(new Engine.Get(realtime, new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(typeX, id))).loadSource(loadSource));
if (get.exists()) {
type = typeX;
break;
@ -159,7 +159,7 @@ public class ShardGetService extends AbstractIndexShardComponent {
return new GetResult(shardId.index().name(), type, id, -1, false, null, null);
}
} else {
get = indexShard.get(new Engine.Get(realtime, new Term(UidFieldMapper.NAME, Uid.createUid(type, id))).loadSource(loadSource));
get = indexShard.get(new Engine.Get(realtime, new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(type, id))).loadSource(loadSource));
if (!get.exists()) {
get.release();
return new GetResult(shardId.index().name(), type, id, -1, false, null, null);

View File

@ -179,9 +179,9 @@ public interface FieldMapper<T> {
Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions, boolean transpositions);
Query prefixQuery(String value, @Nullable MultiTermQuery.RewriteMethod method, @Nullable QueryParseContext context);
Query prefixQuery(Object value, @Nullable MultiTermQuery.RewriteMethod method, @Nullable QueryParseContext context);
Filter prefixFilter(String value, @Nullable QueryParseContext context);
Filter prefixFilter(Object value, @Nullable QueryParseContext context);
Query regexpQuery(String value, int flags, @Nullable MultiTermQuery.RewriteMethod method, @Nullable QueryParseContext context);

View File

@ -70,7 +70,11 @@ public final class Uid {
@Override
public String toString() {
return type + DELIMITER + id;
return createUid(type, id);
}
public BytesRef toBytesRef() {
return createUidAsBytes(type, id);
}
public static String typePrefix(String type) {

View File

@ -448,7 +448,7 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
}
@Override
public Query prefixQuery(String value, @Nullable MultiTermQuery.RewriteMethod method, @Nullable QueryParseContext context) {
public Query prefixQuery(Object value, @Nullable MultiTermQuery.RewriteMethod method, @Nullable QueryParseContext context) {
PrefixQuery query = new PrefixQuery(names().createIndexNameTerm(indexedValueForSearch(value)));
if (method != null) {
query.setRewriteMethod(method);
@ -457,7 +457,7 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
}
@Override
public Filter prefixFilter(String value, @Nullable QueryParseContext context) {
public Filter prefixFilter(Object value, @Nullable QueryParseContext context) {
return new PrefixFilter(names().createIndexNameTerm(indexedValueForSearch(value)));
}

View File

@ -218,7 +218,7 @@ public class BooleanFieldMapper extends AbstractFieldMapper<Boolean> {
if (nullValue == null) {
return null;
}
return new TermFilter(names().createIndexNameTerm(nullValue ? "T" : "F"));
return new TermFilter(names().createIndexNameTerm(nullValue ? Values.TRUE : Values.FALSE));
}
@Override

View File

@ -185,13 +185,13 @@ public class IdFieldMapper extends AbstractFieldMapper<String> implements Intern
}
@Override
public Query prefixQuery(String value, @Nullable MultiTermQuery.RewriteMethod method, @Nullable QueryParseContext context) {
public Query prefixQuery(Object value, @Nullable MultiTermQuery.RewriteMethod method, @Nullable QueryParseContext context) {
if (fieldType.indexed() || context == null) {
return super.prefixQuery(value, method, context);
}
Collection<String> queryTypes = context.queryTypes();
if (queryTypes.size() == 1) {
PrefixQuery prefixQuery = new PrefixQuery(new Term(UidFieldMapper.NAME, Uid.createUid(Iterables.getFirst(queryTypes, null), value)));
PrefixQuery prefixQuery = new PrefixQuery(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(Iterables.getFirst(queryTypes, null), BytesRefs.toBytesRef(value))));
if (method != null) {
prefixQuery.setRewriteMethod(method);
}
@ -199,7 +199,7 @@ public class IdFieldMapper extends AbstractFieldMapper<String> implements Intern
}
BooleanQuery query = new BooleanQuery();
for (String queryType : queryTypes) {
PrefixQuery prefixQuery = new PrefixQuery(new Term(UidFieldMapper.NAME, Uid.createUid(queryType, value)));
PrefixQuery prefixQuery = new PrefixQuery(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(queryType, BytesRefs.toBytesRef(value))));
if (method != null) {
prefixQuery.setRewriteMethod(method);
}
@ -209,17 +209,17 @@ public class IdFieldMapper extends AbstractFieldMapper<String> implements Intern
}
@Override
public Filter prefixFilter(String value, @Nullable QueryParseContext context) {
public Filter prefixFilter(Object value, @Nullable QueryParseContext context) {
if (fieldType.indexed() || context == null) {
return super.prefixFilter(value, context);
}
Collection<String> queryTypes = context.queryTypes();
if (queryTypes.size() == 1) {
return new PrefixFilter(new Term(UidFieldMapper.NAME, Uid.createUid(Iterables.getFirst(queryTypes, null), value)));
return new PrefixFilter(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(Iterables.getFirst(queryTypes, null), BytesRefs.toBytesRef(value))));
}
XBooleanFilter filter = new XBooleanFilter();
for (String queryType : queryTypes) {
filter.add(new PrefixFilter(new Term(UidFieldMapper.NAME, Uid.createUid(queryType, value))), BooleanClause.Occur.SHOULD);
filter.add(new PrefixFilter(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(queryType, BytesRefs.toBytesRef(value)))), BooleanClause.Occur.SHOULD);
}
return filter;
}
@ -231,7 +231,7 @@ public class IdFieldMapper extends AbstractFieldMapper<String> implements Intern
}
Collection<String> queryTypes = context.queryTypes();
if (queryTypes.size() == 1) {
RegexpQuery regexpQuery = new RegexpQuery(new Term(UidFieldMapper.NAME, Uid.createUid(Iterables.getFirst(queryTypes, null), value)), flags);
RegexpQuery regexpQuery = new RegexpQuery(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(Iterables.getFirst(queryTypes, null), value)), flags);
if (method != null) {
regexpQuery.setRewriteMethod(method);
}
@ -239,7 +239,7 @@ public class IdFieldMapper extends AbstractFieldMapper<String> implements Intern
}
BooleanQuery query = new BooleanQuery();
for (String queryType : queryTypes) {
RegexpQuery regexpQuery = new RegexpQuery(new Term(UidFieldMapper.NAME, Uid.createUid(queryType, value)), flags);
RegexpQuery regexpQuery = new RegexpQuery(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(queryType, value)), flags);
if (method != null) {
regexpQuery.setRewriteMethod(method);
}
@ -254,11 +254,11 @@ public class IdFieldMapper extends AbstractFieldMapper<String> implements Intern
}
Collection<String> queryTypes = context.queryTypes();
if (queryTypes.size() == 1) {
return new RegexpFilter(new Term(UidFieldMapper.NAME, Uid.createUid(Iterables.getFirst(queryTypes, null), value)), flags);
return new RegexpFilter(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(Iterables.getFirst(queryTypes, null), value)), flags);
}
XBooleanFilter filter = new XBooleanFilter();
for (String queryType : queryTypes) {
filter.add(new RegexpFilter(new Term(UidFieldMapper.NAME, Uid.createUid(queryType, value)), flags), BooleanClause.Occur.SHOULD);
filter.add(new RegexpFilter(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(queryType, value)), flags), BooleanClause.Occur.SHOULD);
}
return filter;
}

View File

@ -22,7 +22,6 @@ package org.elasticsearch.index.mapper.internal;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.Query;
@ -266,14 +265,6 @@ public class ParentFieldMapper extends AbstractFieldMapper<Uid> implements Inter
return true;
}
public Term term(String type, String id) {
return term(Uid.createUid(type, id));
}
public Term term(String uid) {
return names().createIndexNameTerm(uid);
}
@Override
protected String contentType() {
return CONTENT_TYPE;

View File

@ -23,6 +23,7 @@ import org.apache.lucene.index.Term;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.PrefixFilter;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.cache.filter.support.CacheKeyFilter;
import org.elasticsearch.index.mapper.MapperService;
@ -54,7 +55,7 @@ public class PrefixFilterParser implements FilterParser {
boolean cache = true;
CacheKeyFilter.Key cacheKey = null;
String fieldName = null;
String value = null;
Object value = null;
String filterName = null;
String currentFieldName = null;
@ -71,7 +72,7 @@ public class PrefixFilterParser implements FilterParser {
cacheKey = new CacheKeyFilter.Key(parser.text());
} else {
fieldName = currentFieldName;
value = parser.text();
value = parser.objectBytes();
}
}
}
@ -96,7 +97,7 @@ public class PrefixFilterParser implements FilterParser {
}
}
if (filter == null) {
filter = new PrefixFilter(new Term(fieldName, value));
filter = new PrefixFilter(new Term(fieldName, BytesRefs.toBytesRef(value)));
}
if (cache) {

View File

@ -24,6 +24,7 @@ import org.apache.lucene.search.MultiTermQuery;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.query.support.QueryParsers;
@ -59,7 +60,7 @@ public class PrefixQueryParser implements QueryParser {
String fieldName = parser.currentName();
String rewriteMethod = null;
String value = null;
Object value = null;
float boost = 1.0f;
token = parser.nextToken();
if (token == XContentParser.Token.START_OBJECT) {
@ -69,7 +70,7 @@ public class PrefixQueryParser implements QueryParser {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if ("prefix".equals(currentFieldName)) {
value = parser.text();
value = parser.objectBytes();
} else if ("value".equals(currentFieldName)) {
value = parser.text();
} else if ("boost".equals(currentFieldName)) {
@ -108,7 +109,7 @@ public class PrefixQueryParser implements QueryParser {
}
}
if (query == null) {
PrefixQuery prefixQuery = new PrefixQuery(new Term(fieldName, value));
PrefixQuery prefixQuery = new PrefixQuery(new Term(fieldName, BytesRefs.toBytesRef(value)));
if (method != null) {
prefixQuery.setRewriteMethod(method);
}

View File

@ -23,6 +23,7 @@ import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.MapperService;
@ -56,7 +57,7 @@ public class TermQueryParser implements QueryParser {
}
String fieldName = parser.currentName();
String value = null;
Object value = null;
float boost = 1.0f;
token = parser.nextToken();
if (token == XContentParser.Token.START_OBJECT) {
@ -66,9 +67,9 @@ public class TermQueryParser implements QueryParser {
currentFieldName = parser.currentName();
} else {
if ("term".equals(currentFieldName)) {
value = parser.text();
value = parser.objectBytes();
} else if ("value".equals(currentFieldName)) {
value = parser.text();
value = parser.objectBytes();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else {
@ -102,7 +103,7 @@ public class TermQueryParser implements QueryParser {
}
}
if (query == null) {
query = new TermQuery(new Term(fieldName, value));
query = new TermQuery(new Term(fieldName, BytesRefs.toBytesRef(value)));
}
query.setBoost(boost);
return wrapSmartNameQuery(query, smartNameFieldMappers, parseContext);

View File

@ -241,7 +241,7 @@ public class IndicesTTLService extends AbstractLifecycleComponent<IndicesTTLServ
UidAndRoutingFieldsVisitor fieldsVisitor = new UidAndRoutingFieldsVisitor();
context.reader().document(doc, fieldsVisitor);
Uid uid = fieldsVisitor.uid();
long version = UidField.loadVersion(context, new Term(UidFieldMapper.NAME, uid.toString()));
long version = UidField.loadVersion(context, new Term(UidFieldMapper.NAME, uid.toBytesRef()));
docsToPurge.add(new DocToPurge(uid.type(), uid.id(), version, fieldsVisitor.routing()));
} catch (Exception e) {
logger.trace("failed to collect doc", e);

View File

@ -62,7 +62,7 @@ public class VersionFetchSubPhase implements FetchSubPhase {
// the case below...
long version = UidField.loadVersion(
hitContext.readerContext(),
new Term(UidFieldMapper.NAME, hitContext.fieldVisitor().uid().toString())
new Term(UidFieldMapper.NAME, hitContext.fieldVisitor().uid().toBytesRef())
);
if (version < 0) {
version = -1;