Search in sources :

Example 1 with BytesRefFieldSource

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.");
}
Also used : TrieDoubleField(org.apache.solr.schema.TrieDoubleField) StrField(org.apache.solr.schema.StrField) LongFieldSource(org.apache.lucene.queries.function.valuesource.LongFieldSource) TrieIntField(org.apache.solr.schema.TrieIntField) TrieDateField(org.apache.solr.schema.TrieDateField) BytesRefFieldSource(org.apache.lucene.queries.function.valuesource.BytesRefFieldSource) FieldType(org.apache.solr.schema.FieldType) SchemaField(org.apache.solr.schema.SchemaField) DoubleFieldSource(org.apache.lucene.queries.function.valuesource.DoubleFieldSource) IntFieldSource(org.apache.lucene.queries.function.valuesource.IntFieldSource) TrieLongField(org.apache.solr.schema.TrieLongField) FloatFieldSource(org.apache.lucene.queries.function.valuesource.FloatFieldSource) DateFieldSource(org.apache.solr.analytics.util.valuesource.DateFieldSource) SolrException(org.apache.solr.common.SolrException) TrieFloatField(org.apache.solr.schema.TrieFloatField)

Example 2 with BytesRefFieldSource

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);
}
Also used : SumTotalTermFreqValueSource(org.apache.lucene.queries.function.valuesource.SumTotalTermFreqValueSource) DoubleConstValueSource(org.apache.lucene.queries.function.valuesource.DoubleConstValueSource) ConstValueSource(org.apache.lucene.queries.function.valuesource.ConstValueSource) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource) DocFreqValueSource(org.apache.lucene.queries.function.valuesource.DocFreqValueSource) NormValueSource(org.apache.lucene.queries.function.valuesource.NormValueSource) NumDocsValueSource(org.apache.lucene.queries.function.valuesource.NumDocsValueSource) MaxDocValueSource(org.apache.lucene.queries.function.valuesource.MaxDocValueSource) JoinDocFreqValueSource(org.apache.lucene.queries.function.valuesource.JoinDocFreqValueSource) LiteralValueSource(org.apache.lucene.queries.function.valuesource.LiteralValueSource) TotalTermFreqValueSource(org.apache.lucene.queries.function.valuesource.TotalTermFreqValueSource) IDFValueSource(org.apache.lucene.queries.function.valuesource.IDFValueSource) TermFreqValueSource(org.apache.lucene.queries.function.valuesource.TermFreqValueSource) TFValueSource(org.apache.lucene.queries.function.valuesource.TFValueSource) LiteralValueSource(org.apache.lucene.queries.function.valuesource.LiteralValueSource) IfFunction(org.apache.lucene.queries.function.valuesource.IfFunction) BytesRefFieldSource(org.apache.lucene.queries.function.valuesource.BytesRefFieldSource) DoubleConstValueSource(org.apache.lucene.queries.function.valuesource.DoubleConstValueSource) ConstValueSource(org.apache.lucene.queries.function.valuesource.ConstValueSource)

Example 3 with BytesRefFieldSource

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);
    }
}
Also used : BytesRefFieldSource(org.apache.lucene.queries.function.valuesource.BytesRefFieldSource)

Example 4 with BytesRefFieldSource

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;
}
Also used : HashMap(java.util.HashMap) ValueSource(org.apache.lucene.queries.function.ValueSource) BytesRefFieldSource(org.apache.lucene.queries.function.valuesource.BytesRefFieldSource)

Example 5 with BytesRefFieldSource

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);
    }
}
Also used : ArrayList(java.util.ArrayList) MutableValue(org.apache.lucene.util.mutable.MutableValue) BytesRefFieldSource(org.apache.lucene.queries.function.valuesource.BytesRefFieldSource) ValueSource(org.apache.lucene.queries.function.ValueSource) MutableValueStr(org.apache.lucene.util.mutable.MutableValueStr) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

BytesRefFieldSource (org.apache.lucene.queries.function.valuesource.BytesRefFieldSource)6 ValueSource (org.apache.lucene.queries.function.ValueSource)2 LongFieldSource (org.apache.lucene.queries.function.valuesource.LongFieldSource)2 BytesRef (org.apache.lucene.util.BytesRef)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)1 BinaryDocValuesField (org.apache.lucene.document.BinaryDocValuesField)1 Document (org.apache.lucene.document.Document)1 Field (org.apache.lucene.document.Field)1 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)1 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)1 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)1 ConstValueSource (org.apache.lucene.queries.function.valuesource.ConstValueSource)1 DocFreqValueSource (org.apache.lucene.queries.function.valuesource.DocFreqValueSource)1 DoubleConstValueSource (org.apache.lucene.queries.function.valuesource.DoubleConstValueSource)1