Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uid#createTypeUids to accept a collection of ids rather than a list #11263

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/main/java/org/elasticsearch/index/mapper/Uid.java
Expand Up @@ -130,20 +130,19 @@ public static BytesRef createUidAsBytes(BytesRef type, BytesRef id, BytesRefBuil
return spare.get();
}

public static BytesRef[] createTypeUids(Collection<String> types, Object ids) {
return createTypeUids(types, Collections.singletonList(ids));
public static BytesRef[] createUidsForTypesAndId(Collection<String> types, Object id) {
return createUidsForTypesAndIds(types, Collections.singletonList(id));
}

public static BytesRef[] createTypeUids(Collection<String> types, List<? extends Object> ids) {
final int numIds = ids.size();
public static BytesRef[] createUidsForTypesAndIds(Collection<String> types, Collection<?> ids) {
BytesRef[] uids = new BytesRef[types.size() * ids.size()];
BytesRefBuilder typeBytes = new BytesRefBuilder();
BytesRefBuilder idBytes = new BytesRefBuilder();
int index = 0;
for (String type : types) {
typeBytes.copyChars(type);
for (int i = 0; i < numIds; i++, index++) {
uids[index] = Uid.createUidAsBytes(typeBytes.get(), BytesRefs.toBytesRef(ids.get(i), idBytes));
for (Object id : ids) {
uids[index++] = Uid.createUidAsBytes(typeBytes.get(), BytesRefs.toBytesRef(id, idBytes));
}
}
return uids;
Expand Down
Expand Up @@ -187,7 +187,7 @@ public Query termQuery(Object value, @Nullable QueryParseContext context) {
if (fieldType.indexOptions() != IndexOptions.NONE || context == null) {
return super.termQuery(value, context);
}
final BytesRef[] uids = Uid.createTypeUids(context.queryTypes(), value);
final BytesRef[] uids = Uid.createUidsForTypesAndId(context.queryTypes(), value);
if (uids.length == 1) {
return new TermQuery(new Term(UidFieldMapper.NAME, uids[0]));
} else {
Expand All @@ -200,7 +200,7 @@ public Query termsQuery(List values, @Nullable QueryParseContext context) {
if (fieldType.indexOptions() != IndexOptions.NONE || context == null) {
return super.termsQuery(values, context);
}
return new TermsQuery(UidFieldMapper.NAME, Uid.createTypeUids(context.queryTypes(), values));
return new TermsQuery(UidFieldMapper.NAME, Uid.createUidsForTypesAndIds(context.queryTypes(), values));
}

@Override
Expand Down
Expand Up @@ -121,7 +121,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
types = parseContext.mapperService().types();
}

TermsQuery query = new TermsQuery(UidFieldMapper.NAME, Uid.createTypeUids(types, ids));
TermsQuery query = new TermsQuery(UidFieldMapper.NAME, Uid.createUidsForTypesAndIds(types, ids));
query.setBoost(boost);
if (queryName != null) {
parseContext.addNamedQuery(queryName, query);
Expand Down