use of org.apache.lucene.queries.function.valuesource.BytesRefFieldSource in project lucene-solr by apache.
the class StatsCollectorSupplierFactory method buildFieldSource.
/**
* Builds a value source for a given field, making sure that the field fits a given source type.
* @param schema the schema
* @param expressionString The name of the field to build a Field Source from.
* @param sourceType FIELD_TYPE for any type of field, NUMBER_TYPE for numeric fields,
* DATE_TYPE for date fields and STRING_TYPE for string fields.
* @return a value source
*/
private static ValueSource buildFieldSource(IndexSchema schema, String expressionString, int sourceType) {
SchemaField sf;
try {
sf = schema.getField(expressionString);
} catch (SolrException e) {
throw new SolrException(ErrorCode.BAD_REQUEST, "The field " + expressionString + " does not exist.", e);
}
FieldType type = sf.getType();
if (type instanceof TrieIntField) {
if (sourceType != NUMBER_TYPE && sourceType != FIELD_TYPE) {
return null;
}
return new IntFieldSource(expressionString) {
public String description() {
return field;
}
};
} else if (type instanceof TrieLongField) {
if (sourceType != NUMBER_TYPE && sourceType != FIELD_TYPE) {
return null;
}
return new LongFieldSource(expressionString) {
public String description() {
return field;
}
};
} else if (type instanceof TrieFloatField) {
if (sourceType != NUMBER_TYPE && sourceType != FIELD_TYPE) {
return null;
}
return new FloatFieldSource(expressionString) {
public String description() {
return field;
}
};
} else if (type instanceof TrieDoubleField) {
if (sourceType != NUMBER_TYPE && sourceType != FIELD_TYPE) {
return null;
}
return new DoubleFieldSource(expressionString) {
public String description() {
return field;
}
};
} else if (type instanceof TrieDateField) {
if (sourceType != DATE_TYPE && sourceType != FIELD_TYPE) {
return null;
}
return new DateFieldSource(expressionString) {
public String description() {
return field;
}
};
} else if (type instanceof StrField) {
if (sourceType != STRING_TYPE && sourceType != FIELD_TYPE) {
return null;
}
return new BytesRefFieldSource(expressionString) {
public String description() {
return field;
}
};
}
throw new SolrException(ErrorCode.BAD_REQUEST, type.toString() + " is not a supported field type in Solr Analytics.");
}
use of org.apache.lucene.queries.function.valuesource.BytesRefFieldSource in project lucene-solr by apache.
the class TestValueSources method testIf.
public void testIf() throws Exception {
ValueSource vs = new IfFunction(new BytesRefFieldSource("id"), // match
new ConstValueSource(1.0f), new ConstValueSource(2.0f));
assertHits(new FunctionQuery(vs), new float[] { 1f, 1f });
assertAllExist(vs);
// true just if a test value exists...
vs = new IfFunction(new LiteralValueSource("false"), // match
new ConstValueSource(1.0f), new ConstValueSource(2.0f));
assertHits(new FunctionQuery(vs), new float[] { 1f, 1f });
assertAllExist(vs);
// false value if tests value does not exist
vs = new IfFunction(BOGUS_FLOAT_VS, new ConstValueSource(1.0f), // match
new ConstValueSource(2.0f));
assertHits(new FunctionQuery(vs), new float[] { 2F, 2F });
assertAllExist(vs);
// final value may still not exist
vs = new IfFunction(new BytesRefFieldSource("id"), // match
BOGUS_FLOAT_VS, new ConstValueSource(1.0f));
assertNoneExist(vs);
}
use of org.apache.lucene.queries.function.valuesource.BytesRefFieldSource in project lucene-solr by apache.
the class DistinctValuesCollectorTest method createDistinctCountCollector.
@SuppressWarnings({ "unchecked", "rawtypes" })
private <T extends Comparable<Object>, R extends Comparable<Object>> DistinctValuesCollector<T, R> createDistinctCountCollector(FirstPassGroupingCollector<T> firstPassGroupingCollector, String countField) throws IOException {
Collection<SearchGroup<T>> searchGroups = firstPassGroupingCollector.getTopGroups(0, false);
GroupSelector<T> selector = firstPassGroupingCollector.getGroupSelector();
if (ValueSourceGroupSelector.class.isAssignableFrom(selector.getClass())) {
GroupSelector gs = new ValueSourceGroupSelector(new BytesRefFieldSource(countField), new HashMap<>());
return new DistinctValuesCollector<>(selector, searchGroups, gs);
} else {
GroupSelector ts = new TermGroupSelector(countField);
return new DistinctValuesCollector<>(selector, searchGroups, ts);
}
}
use of org.apache.lucene.queries.function.valuesource.BytesRefFieldSource in project lucene-solr by apache.
the class GroupingSearchTest method createRandomGroupingSearch.
private GroupingSearch createRandomGroupingSearch(String groupField, Sort groupSort, int docsInGroup, boolean canUseIDV) {
GroupingSearch groupingSearch;
if (random().nextBoolean()) {
ValueSource vs = new BytesRefFieldSource(groupField);
groupingSearch = new GroupingSearch(vs, new HashMap<>());
} else {
groupingSearch = new GroupingSearch(groupField);
}
groupingSearch.setGroupSort(groupSort);
groupingSearch.setGroupDocsLimit(docsInGroup);
if (random().nextBoolean()) {
groupingSearch.setCachingInMB(4.0, true);
}
return groupingSearch;
}
use of org.apache.lucene.queries.function.valuesource.BytesRefFieldSource in project lucene-solr by apache.
the class TestGrouping method createSecondPassCollector.
// Basically converts searchGroups from MutableValue to BytesRef if grouping by ValueSource
@SuppressWarnings("unchecked")
private TopGroupsCollector<?> createSecondPassCollector(FirstPassGroupingCollector<?> firstPassGroupingCollector, String groupField, Collection<SearchGroup<BytesRef>> searchGroups, Sort groupSort, Sort sortWithinGroup, int maxDocsPerGroup, boolean getScores, boolean getMaxScores, boolean fillSortFields) throws IOException {
if (firstPassGroupingCollector.getGroupSelector().getClass().isAssignableFrom(TermGroupSelector.class)) {
GroupSelector<BytesRef> selector = (GroupSelector<BytesRef>) firstPassGroupingCollector.getGroupSelector();
return new TopGroupsCollector<>(selector, searchGroups, groupSort, sortWithinGroup, maxDocsPerGroup, getScores, getMaxScores, fillSortFields);
} else {
ValueSource vs = new BytesRefFieldSource(groupField);
List<SearchGroup<MutableValue>> mvalSearchGroups = new ArrayList<>(searchGroups.size());
for (SearchGroup<BytesRef> mergedTopGroup : searchGroups) {
SearchGroup<MutableValue> sg = new SearchGroup<>();
MutableValueStr groupValue = new MutableValueStr();
if (mergedTopGroup.groupValue != null) {
groupValue.value.copyBytes(mergedTopGroup.groupValue);
} else {
groupValue.exists = false;
}
sg.groupValue = groupValue;
sg.sortValues = mergedTopGroup.sortValues;
mvalSearchGroups.add(sg);
}
ValueSourceGroupSelector selector = new ValueSourceGroupSelector(vs, new HashMap<>());
return new TopGroupsCollector<>(selector, mvalSearchGroups, groupSort, sortWithinGroup, maxDocsPerGroup, getScores, getMaxScores, fillSortFields);
}
}
Aggregations