Search in sources :

Example 1 with FloatFieldSource

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

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

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

the class TestValueSources method testFloat.

public void testFloat() throws Exception {
    ValueSource vs = new FloatFieldSource("float");
    assertHits(new FunctionQuery(vs), new float[] { 5.2f, 9.3f });
    assertAllExist(vs);
    assertNoneExist(BOGUS_FLOAT_VS);
}
Also used : FloatFieldSource(org.apache.lucene.queries.function.valuesource.FloatFieldSource) MultiValuedFloatFieldSource(org.apache.lucene.queries.function.valuesource.MultiValuedFloatFieldSource) 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 FloatFieldSource

use of org.apache.lucene.queries.function.valuesource.FloatFieldSource in project ddf by codice.

the class GeoNamesQueryLuceneIndex method createQuery.

protected Query createQuery(final String queryString) throws ParseException {
    final StandardAnalyzer standardAnalyzer = new StandardAnalyzer();
    final QueryParser nameQueryParser = new QueryParser(GeoNamesLuceneConstants.NAME_FIELD, standardAnalyzer);
    nameQueryParser.setEnablePositionIncrements(false);
    /* For the name, we construct a query searching for exactly the query string (the phrase
        query), a query searching for all the terms in the query string (the AND query), and a
        query searching for any of the terms in the query string (the OR query). We take the
        maximum of the scores generated by these three queries and use that as the score for the
        name. */
    // Surround with quotes so Lucene looks for the words in the query as a phrase.
    // Phrase query gets the biggest boost - 3.2 was obtained after some experimentation.
    final Query phraseNameQuery = new BoostQuery(nameQueryParser.parse("\"" + queryString + "\""), 3.2f);
    // By default, QueryParser uses OR to separate terms.
    // We give OR queries the lowest boost because they're not as good as phrase matches or
    // AND matches - 1 (the default boost value) was obtained after some experimentation.
    final Query orNameQuery = nameQueryParser.parse(queryString);
    nameQueryParser.setDefaultOperator(QueryParser.AND_OPERATOR);
    // We give AND queries the second-biggest boost because they're better than OR matches but
    // not as good as phrase matches - 2 was obtained after some experimentation.
    final Query andNameQuery = new BoostQuery(nameQueryParser.parse(queryString), 2f);
    final List<Query> nameQueryList = Arrays.asList(phraseNameQuery, orNameQuery, andNameQuery);
    // This query will score each document by the maximum of the three sub-queries.
    final Query nameQuery = new DisjunctionMaxQuery(nameQueryList, 0);
    final QueryParser alternateNamesQueryParser = new QueryParser(GeoNamesLuceneConstants.ALTERNATE_NAMES_FIELD, standardAnalyzer);
    // For the alternate names, we perform an AND query and an OR query, both of which are
    // boosted less than the name query because the alternate names are generally not as
    // important.
    // The OR query gets a lower boost - 0.5 was obtained after some experimentation.
    final Query orAlternateNamesQuery = new BoostQuery(alternateNamesQueryParser.parse(queryString), 0.5f);
    alternateNamesQueryParser.setDefaultOperator(QueryParser.AND_OPERATOR);
    // The AND query gets a higher boost - 1 (the default boost value) was obtained after some
    // experimentation.
    final Query andAlternateNamesQuery = alternateNamesQueryParser.parse(queryString);
    final List<Query> alternateNamesQueryList = Arrays.asList(orAlternateNamesQuery, andAlternateNamesQuery);
    // This query will score each document by the maximum of the two sub-queries.
    final Query alternateNamesQuery = new DisjunctionMaxQuery(alternateNamesQueryList, 0);
    final List<Query> queryList = Arrays.asList(nameQuery, alternateNamesQuery);
    // This query will score each document by the sum of the two sub-queries, since both the
    // name and the alternate names are important.
    // The boost values ensure that how well the query matches the name has a bigger impact on
    // the final score than how well it matches the alternate names.
    final DisjunctionMaxQuery disjunctionMaxQuery = new DisjunctionMaxQuery(queryList, 1.0f);
    // This is the boost we calculated at index time, and it is applied in the CustomScoreQuery.
    final FunctionQuery boostQuery = new FunctionQuery(new FloatFieldSource(GeoNamesLuceneConstants.BOOST_FIELD));
    return new CustomScoreQuery(disjunctionMaxQuery, boostQuery);
}
Also used : QueryParser(org.apache.lucene.queryparser.classic.QueryParser) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) FloatFieldSource(org.apache.lucene.queries.function.valuesource.FloatFieldSource) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 5 with FloatFieldSource

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

the class TestValueSources method testRangeMap.

public void testRangeMap() throws Exception {
    assertHits(new FunctionQuery(new RangeMapFloatFunction(new FloatFieldSource("float"), 5, 6, 1, 0f)), new float[] { 1f, 0f });
    assertHits(new FunctionQuery(new RangeMapFloatFunction(new FloatFieldSource("float"), 5, 6, new SumFloatFunction(new ValueSource[] { new ConstValueSource(1f), new ConstValueSource(2f) }), new ConstValueSource(11f))), new float[] { 3f, 11f });
// TODO: what *should* the rules be for exist() ?
// ((source exists && source in range && target exists) OR (source not in range && default exists)) ?
}
Also used : RangeMapFloatFunction(org.apache.lucene.queries.function.valuesource.RangeMapFloatFunction) FloatFieldSource(org.apache.lucene.queries.function.valuesource.FloatFieldSource) MultiValuedFloatFieldSource(org.apache.lucene.queries.function.valuesource.MultiValuedFloatFieldSource) 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) SumFloatFunction(org.apache.lucene.queries.function.valuesource.SumFloatFunction) DoubleConstValueSource(org.apache.lucene.queries.function.valuesource.DoubleConstValueSource) ConstValueSource(org.apache.lucene.queries.function.valuesource.ConstValueSource)

Aggregations

FloatFieldSource (org.apache.lucene.queries.function.valuesource.FloatFieldSource)6 DoubleConstValueSource (org.apache.lucene.queries.function.valuesource.DoubleConstValueSource)3 DoubleFieldSource (org.apache.lucene.queries.function.valuesource.DoubleFieldSource)3 IntFieldSource (org.apache.lucene.queries.function.valuesource.IntFieldSource)3 LongFieldSource (org.apache.lucene.queries.function.valuesource.LongFieldSource)3 ConstValueSource (org.apache.lucene.queries.function.valuesource.ConstValueSource)2 DocFreqValueSource (org.apache.lucene.queries.function.valuesource.DocFreqValueSource)2 IDFValueSource (org.apache.lucene.queries.function.valuesource.IDFValueSource)2 JoinDocFreqValueSource (org.apache.lucene.queries.function.valuesource.JoinDocFreqValueSource)2 LiteralValueSource (org.apache.lucene.queries.function.valuesource.LiteralValueSource)2 MaxDocValueSource (org.apache.lucene.queries.function.valuesource.MaxDocValueSource)2 MultiValuedFloatFieldSource (org.apache.lucene.queries.function.valuesource.MultiValuedFloatFieldSource)2 NormValueSource (org.apache.lucene.queries.function.valuesource.NormValueSource)2 NumDocsValueSource (org.apache.lucene.queries.function.valuesource.NumDocsValueSource)2 QueryValueSource (org.apache.lucene.queries.function.valuesource.QueryValueSource)2 SumTotalTermFreqValueSource (org.apache.lucene.queries.function.valuesource.SumTotalTermFreqValueSource)2 TFValueSource (org.apache.lucene.queries.function.valuesource.TFValueSource)2 TermFreqValueSource (org.apache.lucene.queries.function.valuesource.TermFreqValueSource)2 TotalTermFreqValueSource (org.apache.lucene.queries.function.valuesource.TotalTermFreqValueSource)2 DateFieldSource (org.apache.solr.analytics.util.valuesource.DateFieldSource)2