Search in sources :

Example 1 with LongFieldSource

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

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

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

the class TestTaxonomyFacetSumValueSource method testWithScore.

public void testWithScore() throws Exception {
    Directory indexDir = newDirectory();
    Directory taxoDir = newDirectory();
    DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
    IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));
    FacetsConfig config = new FacetsConfig();
    for (int i = 0; i < 4; i++) {
        Document doc = new Document();
        doc.add(new NumericDocValuesField("price", (i + 1)));
        doc.add(new FacetField("a", Integer.toString(i % 2)));
        iw.addDocument(config.build(taxoWriter, doc));
    }
    DirectoryReader r = DirectoryReader.open(iw);
    DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);
    FacetsCollector fc = new FacetsCollector(true);
    // score documents by their 'price' field - makes asserting the correct counts for the categories easier
    Query q = new FunctionQuery(new LongFieldSource("price"));
    FacetsCollector.search(newSearcher(r), q, 10, fc);
    Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, fc, DoubleValuesSource.SCORES);
    assertEquals("dim=a path=[] value=10.0 childCount=2\n  1 (6.0)\n  0 (4.0)\n", facets.getTopChildren(10, "a").toString());
    iw.close();
    IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir);
}
Also used : FacetsConfig(org.apache.lucene.facet.FacetsConfig) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BoostQuery(org.apache.lucene.search.BoostQuery) Facets(org.apache.lucene.facet.Facets) DirectoryReader(org.apache.lucene.index.DirectoryReader) LongFieldSource(org.apache.lucene.queries.function.valuesource.LongFieldSource) FacetField(org.apache.lucene.facet.FacetField) Document(org.apache.lucene.document.Document) FacetsCollector(org.apache.lucene.facet.FacetsCollector) DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IndexWriter(org.apache.lucene.index.IndexWriter) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)

Example 4 with LongFieldSource

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

the class TestDocValuesFieldSources method test.

@SuppressWarnings("fallthrough")
public void test(DocValuesType type) throws IOException {
    Directory d = newDirectory();
    IndexWriterConfig iwConfig = newIndexWriterConfig(new MockAnalyzer(random()));
    final int nDocs = atLeast(50);
    final Field id = new NumericDocValuesField("id", 0);
    final Field f;
    switch(type) {
        case BINARY:
            f = new BinaryDocValuesField("dv", new BytesRef());
            break;
        case SORTED:
            f = new SortedDocValuesField("dv", new BytesRef());
            break;
        case NUMERIC:
            f = new NumericDocValuesField("dv", 0);
            break;
        case SORTED_NUMERIC:
            f = new SortedNumericDocValuesField("dv", 0);
            break;
        default:
            throw new AssertionError();
    }
    Document document = new Document();
    document.add(id);
    document.add(f);
    final Object[] vals = new Object[nDocs];
    RandomIndexWriter iw = new RandomIndexWriter(random(), d, iwConfig);
    for (int i = 0; i < nDocs; ++i) {
        id.setLongValue(i);
        switch(type) {
            case SORTED:
            case BINARY:
                do {
                    vals[i] = TestUtil.randomSimpleString(random(), 20);
                } while (((String) vals[i]).isEmpty());
                f.setBytesValue(new BytesRef((String) vals[i]));
                break;
            case NUMERIC:
            case SORTED_NUMERIC:
                // keep it an int
                final int bitsPerValue = RandomNumbers.randomIntBetween(random(), 1, 31);
                vals[i] = (long) random().nextInt((int) PackedInts.maxValue(bitsPerValue));
                f.setLongValue((Long) vals[i]);
                break;
        }
        iw.addDocument(document);
        if (random().nextBoolean() && i % 10 == 9) {
            iw.commit();
        }
    }
    iw.close();
    DirectoryReader rd = DirectoryReader.open(d);
    for (LeafReaderContext leave : rd.leaves()) {
        final FunctionValues ids = new LongFieldSource("id").getValues(null, leave);
        final ValueSource vs;
        switch(type) {
            case BINARY:
            case SORTED:
                vs = new BytesRefFieldSource("dv");
                break;
            case NUMERIC:
                vs = new LongFieldSource("dv");
                break;
            case SORTED_NUMERIC:
                // Since we are not indexing multiple values, MIN and MAX should work the same way
                vs = random().nextBoolean() ? new MultiValuedLongFieldSource("dv", Type.MIN) : new MultiValuedLongFieldSource("dv", Type.MAX);
                break;
            default:
                throw new AssertionError();
        }
        final FunctionValues values = vs.getValues(null, leave);
        BytesRefBuilder bytes = new BytesRefBuilder();
        for (int i = 0; i < leave.reader().maxDoc(); ++i) {
            assertTrue(values.exists(i));
            if (vs instanceof BytesRefFieldSource) {
                assertTrue(values.objectVal(i) instanceof String);
            } else if (vs instanceof LongFieldSource) {
                assertTrue(values.objectVal(i) instanceof Long);
                assertTrue(values.bytesVal(i, bytes));
            } else {
                throw new AssertionError();
            }
            Object expected = vals[ids.intVal(i)];
            switch(type) {
                case SORTED:
                    // no exception
                    values.ordVal(i);
                    assertTrue(values.numOrd() >= 1);
                // fall-through
                case BINARY:
                    assertEquals(expected, values.objectVal(i));
                    assertEquals(expected, values.strVal(i));
                    assertEquals(expected, values.objectVal(i));
                    assertEquals(expected, values.strVal(i));
                    assertTrue(values.bytesVal(i, bytes));
                    assertEquals(new BytesRef((String) expected), bytes.get());
                    break;
                case NUMERIC:
                case SORTED_NUMERIC:
                    assertEquals(((Number) expected).longValue(), values.longVal(i));
                    break;
            }
        }
    }
    rd.close();
    d.close();
}
Also used : BytesRefFieldSource(org.apache.lucene.queries.function.valuesource.BytesRefFieldSource) Document(org.apache.lucene.document.Document) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) Field(org.apache.lucene.document.Field) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory) MultiValuedLongFieldSource(org.apache.lucene.queries.function.valuesource.MultiValuedLongFieldSource) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) DirectoryReader(org.apache.lucene.index.DirectoryReader) LongFieldSource(org.apache.lucene.queries.function.valuesource.LongFieldSource) MultiValuedLongFieldSource(org.apache.lucene.queries.function.valuesource.MultiValuedLongFieldSource) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 5 with LongFieldSource

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

the class TestValueSources method testLong.

public void testLong() throws Exception {
    ValueSource vs = new LongFieldSource("long");
    assertHits(new FunctionQuery(vs), new float[] { 4343f, 1954f });
    assertAllExist(vs);
    assertNoneExist(BOGUS_LONG_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) LongFieldSource(org.apache.lucene.queries.function.valuesource.LongFieldSource) MultiValuedLongFieldSource(org.apache.lucene.queries.function.valuesource.MultiValuedLongFieldSource)

Aggregations

LongFieldSource (org.apache.lucene.queries.function.valuesource.LongFieldSource)6 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)3 DoubleFieldSource (org.apache.lucene.queries.function.valuesource.DoubleFieldSource)3 FloatFieldSource (org.apache.lucene.queries.function.valuesource.FloatFieldSource)3 IntFieldSource (org.apache.lucene.queries.function.valuesource.IntFieldSource)3 Directory (org.apache.lucene.store.Directory)3 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)2 Document (org.apache.lucene.document.Document)2 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)2 DirectoryReader (org.apache.lucene.index.DirectoryReader)2 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)2 BytesRefFieldSource (org.apache.lucene.queries.function.valuesource.BytesRefFieldSource)2 DoubleConstValueSource (org.apache.lucene.queries.function.valuesource.DoubleConstValueSource)2 MultiValuedLongFieldSource (org.apache.lucene.queries.function.valuesource.MultiValuedLongFieldSource)2 BinaryDocValuesField (org.apache.lucene.document.BinaryDocValuesField)1 Field (org.apache.lucene.document.Field)1 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)1 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)1 FacetField (org.apache.lucene.facet.FacetField)1 Facets (org.apache.lucene.facet.Facets)1