From f8a08a46ac4bb34f9df23a22baae2021cbe0b541 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Sun, 30 Dec 2012 17:31:13 -0800 Subject: [PATCH] cleanup more calls to Term with String value --- .../explain/TransportExplainAction.java | 2 +- .../index/get/ShardGetService.java | 4 ++-- .../index/mapper/FieldMapper.java | 4 ++-- .../org/elasticsearch/index/mapper/Uid.java | 6 +++++- .../mapper/core/AbstractFieldMapper.java | 4 ++-- .../index/mapper/core/BooleanFieldMapper.java | 2 +- .../index/mapper/internal/IdFieldMapper.java | 20 +++++++++---------- .../mapper/internal/ParentFieldMapper.java | 9 --------- .../index/query/PrefixFilterParser.java | 7 ++++--- .../index/query/PrefixQueryParser.java | 7 ++++--- .../index/query/TermQueryParser.java | 9 +++++---- .../indices/ttl/IndicesTTLService.java | 2 +- .../fetch/version/VersionFetchSubPhase.java | 2 +- 13 files changed, 38 insertions(+), 40 deletions(-) diff --git a/src/main/java/org/elasticsearch/action/explain/TransportExplainAction.java b/src/main/java/org/elasticsearch/action/explain/TransportExplainAction.java index 09cf71a7825..6df3d126609 100644 --- a/src/main/java/org/elasticsearch/action/explain/TransportExplainAction.java +++ b/src/main/java/org/elasticsearch/action/explain/TransportExplainAction.java @@ -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); diff --git a/src/main/java/org/elasticsearch/index/get/ShardGetService.java b/src/main/java/org/elasticsearch/index/get/ShardGetService.java index d169034305e..2d13fbf7409 100644 --- a/src/main/java/org/elasticsearch/index/get/ShardGetService.java +++ b/src/main/java/org/elasticsearch/index/get/ShardGetService.java @@ -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); diff --git a/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java index a00370445b0..94696cca607 100644 --- a/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java @@ -179,9 +179,9 @@ public interface FieldMapper { 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); diff --git a/src/main/java/org/elasticsearch/index/mapper/Uid.java b/src/main/java/org/elasticsearch/index/mapper/Uid.java index 1d080b8e693..a619a2b00a5 100644 --- a/src/main/java/org/elasticsearch/index/mapper/Uid.java +++ b/src/main/java/org/elasticsearch/index/mapper/Uid.java @@ -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) { diff --git a/src/main/java/org/elasticsearch/index/mapper/core/AbstractFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/core/AbstractFieldMapper.java index 370eeaf8961..d27cebffae2 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/AbstractFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/AbstractFieldMapper.java @@ -448,7 +448,7 @@ public abstract class AbstractFieldMapper implements FieldMapper, 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 implements FieldMapper, 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))); } diff --git a/src/main/java/org/elasticsearch/index/mapper/core/BooleanFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/core/BooleanFieldMapper.java index be13a07d522..37aac7933fb 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/BooleanFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/BooleanFieldMapper.java @@ -218,7 +218,7 @@ public class BooleanFieldMapper extends AbstractFieldMapper { if (nullValue == null) { return null; } - return new TermFilter(names().createIndexNameTerm(nullValue ? "T" : "F")); + return new TermFilter(names().createIndexNameTerm(nullValue ? Values.TRUE : Values.FALSE)); } @Override diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/IdFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/IdFieldMapper.java index e840957df5b..e619f49611c 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/IdFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/IdFieldMapper.java @@ -185,13 +185,13 @@ public class IdFieldMapper extends AbstractFieldMapper 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 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 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 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 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 implements Intern } Collection 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 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 implements Intern } Collection 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; } diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java index 3b1605ad421..6f0cb9e1e43 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java @@ -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 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; diff --git a/src/main/java/org/elasticsearch/index/query/PrefixFilterParser.java b/src/main/java/org/elasticsearch/index/query/PrefixFilterParser.java index 85aa17e15ba..f8646744cf1 100644 --- a/src/main/java/org/elasticsearch/index/query/PrefixFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/PrefixFilterParser.java @@ -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) { diff --git a/src/main/java/org/elasticsearch/index/query/PrefixQueryParser.java b/src/main/java/org/elasticsearch/index/query/PrefixQueryParser.java index 412cc66b372..db273a47a10 100644 --- a/src/main/java/org/elasticsearch/index/query/PrefixQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/PrefixQueryParser.java @@ -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); } diff --git a/src/main/java/org/elasticsearch/index/query/TermQueryParser.java b/src/main/java/org/elasticsearch/index/query/TermQueryParser.java index 94c2596e86b..97845044a62 100644 --- a/src/main/java/org/elasticsearch/index/query/TermQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/TermQueryParser.java @@ -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); diff --git a/src/main/java/org/elasticsearch/indices/ttl/IndicesTTLService.java b/src/main/java/org/elasticsearch/indices/ttl/IndicesTTLService.java index fd137645612..a2531b6c06a 100644 --- a/src/main/java/org/elasticsearch/indices/ttl/IndicesTTLService.java +++ b/src/main/java/org/elasticsearch/indices/ttl/IndicesTTLService.java @@ -241,7 +241,7 @@ public class IndicesTTLService extends AbstractLifecycleComponent