Search in sources :

Example 1 with DoubleFieldSource

use of org.apache.lucene.queries.function.valuesource.DoubleFieldSource 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 DoubleFieldSource

use of org.apache.lucene.queries.function.valuesource.DoubleFieldSource in project lucene-solr by apache.

the class TestFunctionQuerySort method testOptimizedFieldSourceFunctionSorting.

public void testOptimizedFieldSourceFunctionSorting() throws IOException {
    // index contents don't matter for this test.
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(null);
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir, iwc);
    IndexReader reader = writer.getReader();
    writer.close();
    IndexSearcher searcher = newSearcher(reader);
    final boolean reverse = random().nextBoolean();
    ValueSource vs;
    SortField sf, vssf;
    vs = new IntFieldSource("int_field");
    sf = new SortField("int_field", Type.INT, reverse);
    vssf = vs.getSortField(reverse);
    assertEquals(sf, vssf);
    sf = sf.rewrite(searcher);
    vssf = vssf.rewrite(searcher);
    assertEquals(sf, vssf);
    vs = new FloatFieldSource("float_field");
    sf = new SortField("float_field", Type.FLOAT, reverse);
    vssf = vs.getSortField(reverse);
    assertEquals(sf, vssf);
    sf = sf.rewrite(searcher);
    vssf = vssf.rewrite(searcher);
    assertEquals(sf, vssf);
    vs = new DoubleFieldSource("double_field");
    sf = new SortField("double_field", Type.DOUBLE, reverse);
    vssf = vs.getSortField(reverse);
    assertEquals(sf, vssf);
    sf = sf.rewrite(searcher);
    vssf = vssf.rewrite(searcher);
    assertEquals(sf, vssf);
    vs = new LongFieldSource("long_field");
    sf = new SortField("long_field", Type.LONG, reverse);
    vssf = vs.getSortField(reverse);
    assertEquals(sf, vssf);
    sf = sf.rewrite(searcher);
    vssf = vssf.rewrite(searcher);
    assertEquals(sf, vssf);
    reader.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) DoubleFieldSource(org.apache.lucene.queries.function.valuesource.DoubleFieldSource) IntFieldSource(org.apache.lucene.queries.function.valuesource.IntFieldSource) FloatFieldSource(org.apache.lucene.queries.function.valuesource.FloatFieldSource) DoubleConstValueSource(org.apache.lucene.queries.function.valuesource.DoubleConstValueSource) LongFieldSource(org.apache.lucene.queries.function.valuesource.LongFieldSource) IndexReader(org.apache.lucene.index.IndexReader) SortField(org.apache.lucene.search.SortField) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 3 with DoubleFieldSource

use of org.apache.lucene.queries.function.valuesource.DoubleFieldSource in project lucene-solr by apache.

the class TestValueSources method testDouble.

public void testDouble() throws Exception {
    ValueSource vs = new DoubleFieldSource("double");
    assertHits(new FunctionQuery(vs), new float[] { 3.63f, 5.65f });
    assertAllExist(vs);
    assertNoneExist(BOGUS_DOUBLE_VS);
}
Also used : MultiValuedDoubleFieldSource(org.apache.lucene.queries.function.valuesource.MultiValuedDoubleFieldSource) DoubleFieldSource(org.apache.lucene.queries.function.valuesource.DoubleFieldSource) 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)

Example 4 with DoubleFieldSource

use of org.apache.lucene.queries.function.valuesource.DoubleFieldSource in project lucene-solr by apache.

the class StatsCollectorSupplierFactory method buildFilterSource.

/**
   * Builds a default is missing source that wraps a given source. A missing value is required for all 
   * non-field value sources.
   * @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
   */
@SuppressWarnings("deprecation")
private static ValueSource buildFilterSource(IndexSchema schema, String expressionString, int sourceType) {
    String[] arguments = ExpressionFactory.getArguments(expressionString);
    if (arguments.length != 2) {
        throw new SolrException(ErrorCode.BAD_REQUEST, "Invalid arguments were given for \"" + AnalyticsParams.FILTER + "\".");
    }
    ValueSource delegateSource = buildSourceTree(schema, arguments[0], sourceType);
    if (delegateSource == null) {
        return null;
    }
    Object defaultObject;
    ValueSource src = delegateSource;
    if (delegateSource instanceof FilterFieldSource) {
        src = ((FilterFieldSource) delegateSource).getRootSource();
    }
    if (src instanceof IntFieldSource) {
        try {
            defaultObject = new Integer(arguments[1]);
        } catch (NumberFormatException e) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "The filter value " + arguments[1] + " cannot be converted into an integer.", e);
        }
    } else if (src instanceof DateFieldSource || src instanceof MultiDateFunction) {
        defaultObject = DateMathParser.parseMath(null, arguments[1]);
    } else if (src instanceof LongFieldSource) {
        try {
            defaultObject = new Long(arguments[1]);
        } catch (NumberFormatException e) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "The filter value " + arguments[1] + " cannot be converted into a long.", e);
        }
    } else if (src instanceof FloatFieldSource) {
        try {
            defaultObject = new Float(arguments[1]);
        } catch (NumberFormatException e) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "The filter value " + arguments[1] + " cannot be converted into a float.", e);
        }
    } else if (src instanceof DoubleFieldSource || src instanceof SingleDoubleFunction || src instanceof DualDoubleFunction || src instanceof MultiDoubleFunction) {
        try {
            defaultObject = new Double(arguments[1]);
        } catch (NumberFormatException e) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "The filter value " + arguments[1] + " cannot be converted into a double.", e);
        }
    } else {
        defaultObject = arguments[1];
    }
    return new FilterFieldSource(delegateSource, defaultObject);
}
Also used : LongFieldSource(org.apache.lucene.queries.function.valuesource.LongFieldSource) MultiDateFunction(org.apache.solr.analytics.util.valuesource.MultiDateFunction) SingleDoubleFunction(org.apache.solr.analytics.util.valuesource.SingleDoubleFunction) MultiDoubleFunction(org.apache.solr.analytics.util.valuesource.MultiDoubleFunction) DoubleFieldSource(org.apache.lucene.queries.function.valuesource.DoubleFieldSource) DualDoubleFunction(org.apache.solr.analytics.util.valuesource.DualDoubleFunction) FilterFieldSource(org.apache.solr.analytics.util.valuesource.FilterFieldSource) IntFieldSource(org.apache.lucene.queries.function.valuesource.IntFieldSource) FloatFieldSource(org.apache.lucene.queries.function.valuesource.FloatFieldSource) DateFieldSource(org.apache.solr.analytics.util.valuesource.DateFieldSource) ValueSource(org.apache.lucene.queries.function.ValueSource) SolrException(org.apache.solr.common.SolrException)

Aggregations

DoubleFieldSource (org.apache.lucene.queries.function.valuesource.DoubleFieldSource)4 FloatFieldSource (org.apache.lucene.queries.function.valuesource.FloatFieldSource)3 IntFieldSource (org.apache.lucene.queries.function.valuesource.IntFieldSource)3 LongFieldSource (org.apache.lucene.queries.function.valuesource.LongFieldSource)3 DoubleConstValueSource (org.apache.lucene.queries.function.valuesource.DoubleConstValueSource)2 DateFieldSource (org.apache.solr.analytics.util.valuesource.DateFieldSource)2 SolrException (org.apache.solr.common.SolrException)2 IndexReader (org.apache.lucene.index.IndexReader)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)1 ValueSource (org.apache.lucene.queries.function.ValueSource)1 BytesRefFieldSource (org.apache.lucene.queries.function.valuesource.BytesRefFieldSource)1 ConstValueSource (org.apache.lucene.queries.function.valuesource.ConstValueSource)1 DocFreqValueSource (org.apache.lucene.queries.function.valuesource.DocFreqValueSource)1 IDFValueSource (org.apache.lucene.queries.function.valuesource.IDFValueSource)1 JoinDocFreqValueSource (org.apache.lucene.queries.function.valuesource.JoinDocFreqValueSource)1 LiteralValueSource (org.apache.lucene.queries.function.valuesource.LiteralValueSource)1 MaxDocValueSource (org.apache.lucene.queries.function.valuesource.MaxDocValueSource)1 MultiValuedDoubleFieldSource (org.apache.lucene.queries.function.valuesource.MultiValuedDoubleFieldSource)1 NormValueSource (org.apache.lucene.queries.function.valuesource.NormValueSource)1