Search in sources :

Example 1 with RandomApproximationQuery

use of org.apache.lucene.search.RandomApproximationQuery in project elasticsearch by elastic.

the class QueryProfilerTests method testApproximations.

public void testApproximations() throws IOException {
    QueryProfiler profiler = new QueryProfiler();
    Engine.Searcher engineSearcher = new Engine.Searcher("test", new IndexSearcher(reader));
    // disable query caching since we want to test approximations, which won't
    // be exposed on a cached entry
    ContextIndexSearcher searcher = new ContextIndexSearcher(engineSearcher, null, MAYBE_CACHE_POLICY);
    searcher.setProfiler(profiler);
    Query query = new RandomApproximationQuery(new TermQuery(new Term("foo", "bar")), random());
    searcher.count(query);
    List<ProfileResult> results = profiler.getTree();
    assertEquals(1, results.size());
    Map<String, Long> breakdown = results.get(0).getTimeBreakdown();
    assertThat(breakdown.get(QueryTimingType.CREATE_WEIGHT.toString()).longValue(), greaterThan(0L));
    assertThat(breakdown.get(QueryTimingType.BUILD_SCORER.toString()).longValue(), greaterThan(0L));
    assertThat(breakdown.get(QueryTimingType.NEXT_DOC.toString()).longValue(), greaterThan(0L));
    assertThat(breakdown.get(QueryTimingType.ADVANCE.toString()).longValue(), equalTo(0L));
    assertThat(breakdown.get(QueryTimingType.SCORE.toString()).longValue(), equalTo(0L));
    assertThat(breakdown.get(QueryTimingType.MATCH.toString()).longValue(), greaterThan(0L));
    assertThat(breakdown.get(QueryTimingType.CREATE_WEIGHT.toString() + "_count").longValue(), greaterThan(0L));
    assertThat(breakdown.get(QueryTimingType.BUILD_SCORER.toString() + "_count").longValue(), greaterThan(0L));
    assertThat(breakdown.get(QueryTimingType.NEXT_DOC.toString() + "_count").longValue(), greaterThan(0L));
    assertThat(breakdown.get(QueryTimingType.ADVANCE.toString() + "_count").longValue(), equalTo(0L));
    assertThat(breakdown.get(QueryTimingType.SCORE.toString() + "_count").longValue(), equalTo(0L));
    assertThat(breakdown.get(QueryTimingType.MATCH.toString() + "_count").longValue(), greaterThan(0L));
    long rewriteTime = profiler.getRewriteTime();
    assertThat(rewriteTime, greaterThan(0L));
}
Also used : ContextIndexSearcher(org.elasticsearch.search.internal.ContextIndexSearcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) RandomApproximationQuery(org.apache.lucene.search.RandomApproximationQuery) TermQuery(org.apache.lucene.search.TermQuery) ContextIndexSearcher(org.elasticsearch.search.internal.ContextIndexSearcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) RandomApproximationQuery(org.apache.lucene.search.RandomApproximationQuery) ContextIndexSearcher(org.elasticsearch.search.internal.ContextIndexSearcher) Term(org.apache.lucene.index.Term) ProfileResult(org.elasticsearch.search.profile.ProfileResult) Engine(org.elasticsearch.index.engine.Engine)

Example 2 with RandomApproximationQuery

use of org.apache.lucene.search.RandomApproximationQuery in project elasticsearch by elastic.

the class FunctionScoreTests method testPropagatesApproximations.

public void testPropagatesApproximations() throws IOException {
    Query query = new RandomApproximationQuery(new MatchAllDocsQuery(), random());
    IndexSearcher searcher = newSearcher(reader);
    // otherwise we could get a cached entry that does not have approximations
    searcher.setQueryCache(null);
    FunctionScoreQuery fsq = new FunctionScoreQuery(query, null, null, null, Float.POSITIVE_INFINITY);
    for (boolean needsScores : new boolean[] { true, false }) {
        Weight weight = searcher.createWeight(fsq, needsScores);
        Scorer scorer = weight.scorer(reader.leaves().get(0));
        assertNotNull(scorer.twoPhaseIterator());
    }
    FiltersFunctionScoreQuery ffsq = new FiltersFunctionScoreQuery(query, ScoreMode.SUM, new FilterFunction[0], Float.POSITIVE_INFINITY, null, CombineFunction.MULTIPLY);
    for (boolean needsScores : new boolean[] { true, false }) {
        Weight weight = searcher.createWeight(ffsq, needsScores);
        Scorer scorer = weight.scorer(reader.leaves().get(0));
        assertNotNull(scorer.twoPhaseIterator());
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) RandomApproximationQuery(org.apache.lucene.search.RandomApproximationQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) TermQuery(org.apache.lucene.search.TermQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) RandomApproximationQuery(org.apache.lucene.search.RandomApproximationQuery) Scorer(org.apache.lucene.search.Scorer) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Weight(org.apache.lucene.search.Weight)

Example 3 with RandomApproximationQuery

use of org.apache.lucene.search.RandomApproximationQuery in project elasticsearch by elastic.

the class FunctionScoreEquivalenceTests method testTwoPhaseMinScore.

public void testTwoPhaseMinScore() throws Exception {
    Term term = randomTerm();
    Query query = new TermQuery(term);
    Float minScore = random().nextFloat();
    FunctionScoreQuery fsq1 = new FunctionScoreQuery(query, null, minScore, null, Float.POSITIVE_INFINITY);
    FunctionScoreQuery fsq2 = new FunctionScoreQuery(new RandomApproximationQuery(query, random()), null, minScore, null, Float.POSITIVE_INFINITY);
    assertSameScores(fsq1, fsq2);
    FiltersFunctionScoreQuery ffsq1 = new FiltersFunctionScoreQuery(query, ScoreMode.SUM, new FilterFunction[0], Float.POSITIVE_INFINITY, minScore, CombineFunction.MULTIPLY);
    FiltersFunctionScoreQuery ffsq2 = new FiltersFunctionScoreQuery(query, ScoreMode.SUM, new FilterFunction[0], Float.POSITIVE_INFINITY, minScore, CombineFunction.MULTIPLY);
    assertSameScores(ffsq1, ffsq2);
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) RandomApproximationQuery(org.apache.lucene.search.RandomApproximationQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) RandomApproximationQuery(org.apache.lucene.search.RandomApproximationQuery) Term(org.apache.lucene.index.Term)

Example 4 with RandomApproximationQuery

use of org.apache.lucene.search.RandomApproximationQuery in project lucene-solr by apache.

the class TestQueryWrapperFilter method testQueryWrapperFilterPropagatesApproximations.

public void testQueryWrapperFilterPropagatesApproximations() throws IOException {
    Directory dir = newDirectory();
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    doc.add(new StringField("foo", "bar", Store.NO));
    writer.addDocument(doc);
    writer.commit();
    final IndexReader reader = writer.getReader();
    writer.close();
    final IndexSearcher searcher = new IndexSearcher(reader);
    // to still have approximations
    searcher.setQueryCache(null);
    final Query query = new QueryWrapperFilter(new RandomApproximationQuery(new TermQuery(new Term("foo", "bar")), random()));
    final Weight weight = searcher.createNormalizedWeight(query, random().nextBoolean());
    final Scorer scorer = weight.scorer(reader.leaves().get(0));
    assertNotNull(scorer.twoPhaseIterator());
    reader.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) RandomApproximationQuery(org.apache.lucene.search.RandomApproximationQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) RandomApproximationQuery(org.apache.lucene.search.RandomApproximationQuery) Scorer(org.apache.lucene.search.Scorer) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) Weight(org.apache.lucene.search.Weight) StringField(org.apache.lucene.document.StringField) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Aggregations

Query (org.apache.lucene.search.Query)4 RandomApproximationQuery (org.apache.lucene.search.RandomApproximationQuery)4 TermQuery (org.apache.lucene.search.TermQuery)4 Term (org.apache.lucene.index.Term)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3 Scorer (org.apache.lucene.search.Scorer)2 Weight (org.apache.lucene.search.Weight)2 FiltersFunctionScoreQuery (org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery)2 FunctionScoreQuery (org.elasticsearch.common.lucene.search.function.FunctionScoreQuery)2 Document (org.apache.lucene.document.Document)1 StringField (org.apache.lucene.document.StringField)1 IndexReader (org.apache.lucene.index.IndexReader)1 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)1 Directory (org.apache.lucene.store.Directory)1 Engine (org.elasticsearch.index.engine.Engine)1 ContextIndexSearcher (org.elasticsearch.search.internal.ContextIndexSearcher)1