/** * {@inheritDoc} */ @Override public NamedList transform(List<Command> data) throws IOException { NamedList<NamedList> result = new NamedList<>(); for (Command command : data) { final NamedList<Object> commandResult = new NamedList<>(); if (SearchGroupsFieldCommand.class.isInstance(command)) { SearchGroupsFieldCommand fieldCommand = (SearchGroupsFieldCommand) command; Pair<Integer, Collection<SearchGroup<BytesRef>>> pair = fieldCommand.result(); Integer groupedCount = pair.getA(); Collection<SearchGroup<BytesRef>> searchGroups = pair.getB(); if (searchGroups != null) { commandResult.add("topGroups", serializeSearchGroup(searchGroups, fieldCommand.getGroupSort())); } if (groupedCount != null) { commandResult.add("groupCount", groupedCount); } } else { continue; } result.add(command.getKey(), commandResult); } return result; }
private NamedList serializeSearchGroup(Collection<SearchGroup<BytesRef>> data, Sort groupSort) { NamedList<Object[]> result = new NamedList<>(); for (SearchGroup<BytesRef> searchGroup : data) { Object[] convertedSortValues = new Object[searchGroup.sortValues.length]; for (int i = 0; i < searchGroup.sortValues.length; i++) { Object sortValue = searchGroup.sortValues[i]; SchemaField field = groupSort.getSort()[i].getField() != null ? searcher.getSchema().getFieldOrNull(groupSort.getSort()[i].getField()) : null; if (field != null) { FieldType fieldType = field.getType(); if (sortValue != null) { sortValue = fieldType.marshalSortValue(sortValue); } } convertedSortValues[i] = sortValue; } String groupValue = searchGroup.groupValue != null ? searchGroup.groupValue.utf8ToString() : null; result.add(groupValue, convertedSortValues); } return result; }
@Override public Pair<Integer, Collection<SearchGroup<BytesRef>>> result() { final Collection<SearchGroup<BytesRef>> topGroups; if (topNGroups > 0) { topGroups = firstPassGroupingCollector.getTopGroups(0, true); } else { topGroups = Collections.emptyList(); } final Integer groupCount; if (includeGroupCount) { groupCount = allGroupsCollector.getGroupCount(); } else { groupCount = null; } return new Pair<>(groupCount, topGroups); }
/** * {@inheritDoc} */ @Override public NamedList transform(List<Command> data) throws IOException { NamedList<NamedList> result = new NamedList<NamedList>(); for (Command command : data) { final NamedList<Object> commandResult = new NamedList<Object>(); if (SearchGroupsFieldCommand.class.isInstance(command)) { SearchGroupsFieldCommand fieldCommand = (SearchGroupsFieldCommand) command; Pair<Integer, Collection<SearchGroup<BytesRef>>> pair = fieldCommand.result(); Integer groupedCount = pair.getA(); Collection<SearchGroup<BytesRef>> searchGroups = pair.getB(); if (searchGroups != null) { commandResult.add("topGroups", serializeSearchGroup(searchGroups, fieldCommand.getGroupSort())); } if (groupedCount != null) { commandResult.add("groupCount", groupedCount); } } else { continue; } result.add(command.getKey(), commandResult); } return result; }
/** * {@inheritDoc} */ @Override public Map<String, Pair<Integer, Collection<SearchGroup<BytesRef>>>> transformToNative(NamedList<NamedList> shardResponse, Sort groupSort, Sort sortWithinGroup, String shard) { Map<String, Pair<Integer, Collection<SearchGroup<BytesRef>>>> result = new HashMap<String, Pair<Integer, Collection<SearchGroup<BytesRef>>>>(); for (Map.Entry<String, NamedList> command : shardResponse) { List<SearchGroup<BytesRef>> searchGroups = new ArrayList<SearchGroup<BytesRef>>(); NamedList topGroupsAndGroupCount = command.getValue(); @SuppressWarnings("unchecked") NamedList<List<Comparable>> rawSearchGroups = (NamedList<List<Comparable>>) topGroupsAndGroupCount.get("topGroups"); if (rawSearchGroups != null) { for (Map.Entry<String, List<Comparable>> rawSearchGroup : rawSearchGroups){ SearchGroup<BytesRef> searchGroup = new SearchGroup<BytesRef>(); searchGroup.groupValue = rawSearchGroup.getKey() != null ? new BytesRef(rawSearchGroup.getKey()) : null; searchGroup.sortValues = rawSearchGroup.getValue().toArray(new Comparable[rawSearchGroup.getValue().size()]); searchGroups.add(searchGroup); } } Integer groupCount = (Integer) topGroupsAndGroupCount.get("groupCount"); result.put(command.getKey(), new Pair<Integer, Collection<SearchGroup<BytesRef>>>(groupCount, searchGroups)); } return result; }
@Override public Pair<Integer, Collection<SearchGroup<BytesRef>>> result() { final Collection<SearchGroup<BytesRef>> topGroups; if (topNGroups > 0) { topGroups = firstPassGroupingCollector.getTopGroups(0, true); } else { topGroups = Collections.emptyList(); } final Integer groupCount; if (includeGroupCount) { groupCount = allGroupsCollector.getGroupCount(); } else { groupCount = null; } return new Pair<Integer, Collection<SearchGroup<BytesRef>>>(groupCount, topGroups); }
private NamedList serializeSearchGroup(Collection<SearchGroup<BytesRef>> data, Sort groupSort) { NamedList<Object[]> result = new NamedList<Object[]>(); for (SearchGroup<BytesRef> searchGroup : data) { Object[] convertedSortValues = new Object[searchGroup.sortValues.length]; for (int i = 0; i < searchGroup.sortValues.length; i++) { Object sortValue = searchGroup.sortValues[i]; SchemaField field = groupSort.getSort()[i].getField() != null ? searcher.getSchema().getFieldOrNull(groupSort.getSort()[i].getField()) : null; if (field != null) { FieldType fieldType = field.getType(); if (sortValue != null) { sortValue = fieldType.marshalSortValue(sortValue); } } convertedSortValues[i] = sortValue; } String groupValue = searchGroup.groupValue != null ? searchGroup.groupValue.utf8ToString() : null; result.add(groupValue, convertedSortValues); } return result; }
public FunctionDistinctValuesCollector(Map<?, ?> vsContext, ValueSource groupSource, ValueSource countSource, Collection<SearchGroup<MutableValue>> groups) { this.vsContext = vsContext; this.groupSource = groupSource; this.countSource = countSource; groupMap = new LinkedHashMap<>(); for (SearchGroup<MutableValue> group : groups) { groupMap.put(group.groupValue, new GroupCount(group.groupValue)); } }
@SuppressWarnings({"unchecked"}) public TermSecondPassGroupingCollector(String groupField, Collection<SearchGroup<BytesRef>> groups, Sort groupSort, Sort withinGroupSort, int maxDocsPerGroup, boolean getScores, boolean getMaxScores, boolean fillSortFields) throws IOException { super(groups, groupSort, withinGroupSort, maxDocsPerGroup, getScores, getMaxScores, fillSortFields); ordSet = new SentinelIntSet(groupMap.size(), -2); this.groupField = groupField; groupDocs = (SearchGroupDocs<BytesRef>[]) new SearchGroupDocs[ordSet.keys.length]; }
/** * Constructs {@link TermDistinctValuesCollector} instance. * * @param groupField The field to group by * @param countField The field to count distinct values for * @param groups The top N groups, collected during the first phase search */ public TermDistinctValuesCollector(String groupField, String countField, Collection<SearchGroup<BytesRef>> groups) { this.groupField = groupField; this.countField = countField; this.groups = new ArrayList<>(groups.size()); for (SearchGroup<BytesRef> group : groups) { this.groups.add(new GroupCount(group.groupValue)); } ordSet = new SentinelIntSet(groups.size(), -2); groupCounts = new GroupCount[ordSet.keys.length]; }
/** * {@inheritDoc} */ @Override public Map<String, Pair<Integer, Collection<SearchGroup<BytesRef>>>> transformToNative(NamedList<NamedList> shardResponse, Sort groupSort, Sort sortWithinGroup, String shard) { Map<String, Pair<Integer, Collection<SearchGroup<BytesRef>>>> result = new HashMap<>(); for (Map.Entry<String, NamedList> command : shardResponse) { List<SearchGroup<BytesRef>> searchGroups = new ArrayList<>(); NamedList topGroupsAndGroupCount = command.getValue(); @SuppressWarnings("unchecked") NamedList<List<Comparable>> rawSearchGroups = (NamedList<List<Comparable>>) topGroupsAndGroupCount.get("topGroups"); if (rawSearchGroups != null) { for (Map.Entry<String, List<Comparable>> rawSearchGroup : rawSearchGroups){ SearchGroup<BytesRef> searchGroup = new SearchGroup<>(); searchGroup.groupValue = rawSearchGroup.getKey() != null ? new BytesRef(rawSearchGroup.getKey()) : null; searchGroup.sortValues = rawSearchGroup.getValue().toArray(new Comparable[rawSearchGroup.getValue().size()]); for (int i = 0; i < searchGroup.sortValues.length; i++) { SchemaField field = groupSort.getSort()[i].getField() != null ? searcher.getSchema().getFieldOrNull(groupSort.getSort()[i].getField()) : null; if (field != null) { FieldType fieldType = field.getType(); if (searchGroup.sortValues[i] != null) { searchGroup.sortValues[i] = fieldType.unmarshalSortValue(searchGroup.sortValues[i]); } } } searchGroups.add(searchGroup); } } Integer groupCount = (Integer) topGroupsAndGroupCount.get("groupCount"); result.put(command.getKey(), new Pair<Integer, Collection<SearchGroup<BytesRef>>>(groupCount, searchGroups)); } return result; }
private ShardRequest[] createRequestForSpecificShards(ResponseBuilder rb) { // Determine all unique shards to query for TopGroups Set<String> uniqueShards = new HashSet<>(); for (String command : rb.searchGroupToShards.keySet()) { Map<SearchGroup<BytesRef>, Set<String>> groupsToShard = rb.searchGroupToShards.get(command); for (Set<String> shards : groupsToShard.values()) { uniqueShards.addAll(shards); } } return createRequest(rb, uniqueShards.toArray(new String[uniqueShards.size()])); }
private TopGroupsFieldCommand(SchemaField field, Sort groupSort, Sort sortWithinGroup, Collection<SearchGroup<BytesRef>> firstPhaseGroups, int maxDocPerGroup, boolean needScores, boolean needMaxScore) { this.field = field; this.groupSort = groupSort; this.sortWithinGroup = sortWithinGroup; this.firstPhaseGroups = firstPhaseGroups; this.maxDocPerGroup = maxDocPerGroup; this.needScores = needScores; this.needMaxScore = needMaxScore; }
public FunctionDistinctValuesCollector(Map<?, ?> vsContext, ValueSource groupSource, ValueSource countSource, Collection<SearchGroup<MutableValue>> groups) { this.vsContext = vsContext; this.groupSource = groupSource; this.countSource = countSource; groupMap = new LinkedHashMap<MutableValue, GroupCount>(); for (SearchGroup<MutableValue> group : groups) { groupMap.put(group.groupValue, new GroupCount(group.groupValue)); } }
@SuppressWarnings({"unchecked", "rawtypes"}) public TermSecondPassGroupingCollector(String groupField, Collection<SearchGroup<BytesRef>> groups, Sort groupSort, Sort withinGroupSort, int maxDocsPerGroup, boolean getScores, boolean getMaxScores, boolean fillSortFields) throws IOException { super(groups, groupSort, withinGroupSort, maxDocsPerGroup, getScores, getMaxScores, fillSortFields); ordSet = new SentinelIntSet(groupMap.size(), -2); this.groupField = groupField; groupDocs = (SearchGroupDocs<BytesRef>[]) new SearchGroupDocs[ordSet.keys.length]; }
/** * Constructs {@link TermDistinctValuesCollector} instance. * * @param groupField The field to group by * @param countField The field to count distinct values for * @param groups The top N groups, collected during the first phase search */ public TermDistinctValuesCollector(String groupField, String countField, Collection<SearchGroup<BytesRef>> groups) { this.groupField = groupField; this.countField = countField; this.groups = new ArrayList<GroupCount>(groups.size()); for (SearchGroup<BytesRef> group : groups) { this.groups.add(new GroupCount(group.groupValue)); } ordSet = new SentinelIntSet(groups.size(), -2); groupCounts = new GroupCount[ordSet.keys.length]; }
private NamedList serializeSearchGroup(Collection<SearchGroup<BytesRef>> data, Sort groupSort) { NamedList<Comparable[]> result = new NamedList<Comparable[]>(); CharsRef spare = new CharsRef(); for (SearchGroup<BytesRef> searchGroup : data) { Comparable[] convertedSortValues = new Comparable[searchGroup.sortValues.length]; for (int i = 0; i < searchGroup.sortValues.length; i++) { Comparable sortValue = (Comparable) searchGroup.sortValues[i]; SchemaField field = groupSort.getSort()[i].getField() != null ? searcher.getSchema().getFieldOrNull(groupSort.getSort()[i].getField()) : null; if (field != null) { FieldType fieldType = field.getType(); if (sortValue instanceof BytesRef) { UnicodeUtil.UTF8toUTF16((BytesRef)sortValue, spare); String indexedValue = spare.toString(); sortValue = (Comparable) fieldType.toObject(field.createField(fieldType.indexedToReadable(indexedValue), 1.0f)); } else if (sortValue instanceof String) { sortValue = (Comparable) fieldType.toObject(field.createField(fieldType.indexedToReadable((String) sortValue), 1.0f)); } } convertedSortValues[i] = sortValue; } String groupValue = searchGroup.groupValue != null ? searchGroup.groupValue.utf8ToString() : null; result.add(groupValue, convertedSortValues); } return result; }
private ShardRequest[] createRequestForSpecificShards(ResponseBuilder rb) { // Determine all unique shards to query for TopGroups Set<String> uniqueShards = new HashSet<String>(); for (String command : rb.searchGroupToShards.keySet()) { Map<SearchGroup<BytesRef>, Set<String>> groupsToShard = rb.searchGroupToShards.get(command); for (Set<String> shards : groupsToShard.values()) { uniqueShards.addAll(shards); } } return createRequest(rb, uniqueShards.toArray(new String[uniqueShards.size()])); }
/** * {@inheritDoc} */ @Override public Map<String, Pair<Integer, Collection<SearchGroup<BytesRef>>>> transformToNative(NamedList<NamedList> shardResponse, Sort groupSort, Sort sortWithinGroup, String shard) { Map<String, Pair<Integer, Collection<SearchGroup<BytesRef>>>> result = new HashMap<String, Pair<Integer, Collection<SearchGroup<BytesRef>>>>(); for (Map.Entry<String, NamedList> command : shardResponse) { List<SearchGroup<BytesRef>> searchGroups = new ArrayList<SearchGroup<BytesRef>>(); NamedList topGroupsAndGroupCount = command.getValue(); @SuppressWarnings("unchecked") NamedList<List<Comparable>> rawSearchGroups = (NamedList<List<Comparable>>) topGroupsAndGroupCount.get("topGroups"); if (rawSearchGroups != null) { for (Map.Entry<String, List<Comparable>> rawSearchGroup : rawSearchGroups){ SearchGroup<BytesRef> searchGroup = new SearchGroup<BytesRef>(); searchGroup.groupValue = rawSearchGroup.getKey() != null ? new BytesRef(rawSearchGroup.getKey()) : null; searchGroup.sortValues = rawSearchGroup.getValue().toArray(new Comparable[rawSearchGroup.getValue().size()]); for (int i = 0; i < searchGroup.sortValues.length; i++) { SchemaField field = groupSort.getSort()[i].getField() != null ? searcher.getSchema().getFieldOrNull(groupSort.getSort()[i].getField()) : null; if (field != null) { FieldType fieldType = field.getType(); if (searchGroup.sortValues[i] != null) { searchGroup.sortValues[i] = fieldType.unmarshalSortValue(searchGroup.sortValues[i]); } } } searchGroups.add(searchGroup); } } Integer groupCount = (Integer) topGroupsAndGroupCount.get("groupCount"); result.put(command.getKey(), new Pair<Integer, Collection<SearchGroup<BytesRef>>>(groupCount, searchGroups)); } return result; }