Search in sources :

Example 1 with IntIntHashMap

use of com.carrotsearch.hppc.IntIntHashMap in project elasticsearch by elastic.

the class CombiIT method testMultipleAggsOnSameField_WithDifferentRequiredValueSourceType.

/**
     * Making sure that if there are multiple aggregations, working on the same field, yet require different
     * value source type, they can all still work. It used to fail as we used to cache the ValueSource by the
     * field name. If the cached value source was of type "bytes" and another aggregation on the field required to see
     * it as "numeric", it didn't work. Now we cache the Value Sources by a custom key (field name + ValueSource type)
     * so there's no conflict there.
     */
public void testMultipleAggsOnSameField_WithDifferentRequiredValueSourceType() throws Exception {
    createIndex("idx");
    IndexRequestBuilder[] builders = new IndexRequestBuilder[randomInt(30)];
    IntIntMap values = new IntIntHashMap();
    long missingValues = 0;
    for (int i = 0; i < builders.length; i++) {
        String name = "name_" + randomIntBetween(1, 10);
        if (rarely()) {
            missingValues++;
            builders[i] = client().prepareIndex("idx", "type").setSource(jsonBuilder().startObject().field("name", name).endObject());
        } else {
            int value = randomIntBetween(1, 10);
            values.put(value, values.getOrDefault(value, 0) + 1);
            builders[i] = client().prepareIndex("idx", "type").setSource(jsonBuilder().startObject().field("name", name).field("value", value).endObject());
        }
    }
    indexRandom(true, builders);
    ensureSearchable();
    SubAggCollectionMode aggCollectionMode = randomFrom(SubAggCollectionMode.values());
    SearchResponse response = client().prepareSearch("idx").addAggregation(missing("missing_values").field("value")).addAggregation(terms("values").field("value").collectMode(aggCollectionMode)).execute().actionGet();
    assertSearchResponse(response);
    Aggregations aggs = response.getAggregations();
    Missing missing = aggs.get("missing_values");
    assertNotNull(missing);
    assertThat(missing.getDocCount(), equalTo(missingValues));
    Terms terms = aggs.get("values");
    assertNotNull(terms);
    Collection<Terms.Bucket> buckets = terms.getBuckets();
    assertThat(buckets.size(), equalTo(values.size()));
    for (Terms.Bucket bucket : buckets) {
        values.remove(((Number) bucket.getKey()).intValue());
    }
    assertTrue(values.isEmpty());
}
Also used : Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) IntIntMap(com.carrotsearch.hppc.IntIntMap) IntIntHashMap(com.carrotsearch.hppc.IntIntHashMap) Missing(org.elasticsearch.search.aggregations.bucket.missing.Missing) SubAggCollectionMode(org.elasticsearch.search.aggregations.Aggregator.SubAggCollectionMode)

Example 2 with IntIntHashMap

use of com.carrotsearch.hppc.IntIntHashMap in project lucene-solr by apache.

the class ReRankCollector method topDocs.

public TopDocs topDocs(int start, int howMany) {
    try {
        TopDocs mainDocs = mainCollector.topDocs(0, Math.max(reRankDocs, length));
        if (mainDocs.totalHits == 0 || mainDocs.scoreDocs.length == 0) {
            return mainDocs;
        }
        ScoreDoc[] mainScoreDocs = mainDocs.scoreDocs;
        ScoreDoc[] reRankScoreDocs = new ScoreDoc[Math.min(mainScoreDocs.length, reRankDocs)];
        System.arraycopy(mainScoreDocs, 0, reRankScoreDocs, 0, reRankScoreDocs.length);
        mainDocs.scoreDocs = reRankScoreDocs;
        TopDocs rescoredDocs = reRankQueryRescorer.rescore(searcher, mainDocs, mainDocs.scoreDocs.length);
        //Lower howMany to return if we've collected fewer documents.
        howMany = Math.min(howMany, mainScoreDocs.length);
        if (boostedPriority != null) {
            SolrRequestInfo info = SolrRequestInfo.getRequestInfo();
            Map requestContext = null;
            if (info != null) {
                requestContext = info.getReq().getContext();
            }
            IntIntHashMap boostedDocs = QueryElevationComponent.getBoostDocs((SolrIndexSearcher) searcher, boostedPriority, requestContext);
            Arrays.sort(rescoredDocs.scoreDocs, new BoostedComp(boostedDocs, mainDocs.scoreDocs, rescoredDocs.getMaxScore()));
        }
        if (howMany == rescoredDocs.scoreDocs.length) {
            // Just return the rescoredDocs
            return rescoredDocs;
        } else if (howMany > rescoredDocs.scoreDocs.length) {
            //We need to return more then we've reRanked, so create the combined page.
            ScoreDoc[] scoreDocs = new ScoreDoc[howMany];
            //lay down the initial docs
            System.arraycopy(mainScoreDocs, 0, scoreDocs, 0, scoreDocs.length);
            //overlay the re-ranked docs.
            System.arraycopy(rescoredDocs.scoreDocs, 0, scoreDocs, 0, rescoredDocs.scoreDocs.length);
            rescoredDocs.scoreDocs = scoreDocs;
            return rescoredDocs;
        } else {
            //We've rescored more then we need to return.
            ScoreDoc[] scoreDocs = new ScoreDoc[howMany];
            System.arraycopy(rescoredDocs.scoreDocs, 0, scoreDocs, 0, howMany);
            rescoredDocs.scoreDocs = scoreDocs;
            return rescoredDocs;
        }
    } catch (Exception e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
    }
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) IntIntHashMap(com.carrotsearch.hppc.IntIntHashMap) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) IntIntHashMap(com.carrotsearch.hppc.IntIntHashMap) Map(java.util.Map) IntFloatHashMap(com.carrotsearch.hppc.IntFloatHashMap) IOException(java.io.IOException) SolrException(org.apache.solr.common.SolrException) SolrException(org.apache.solr.common.SolrException) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 3 with IntIntHashMap

use of com.carrotsearch.hppc.IntIntHashMap in project lucene-solr by apache.

the class QueryElevationComponent method getBoostDocs.

public static IntIntHashMap getBoostDocs(SolrIndexSearcher indexSearcher, Map<BytesRef, Integer> boosted, Map context) throws IOException {
    IntIntHashMap boostDocs = null;
    if (boosted != null) {
        //by another caller.
        if (context != null) {
            boostDocs = (IntIntHashMap) context.get(BOOSTED_DOCIDS);
        }
        if (boostDocs != null) {
            return boostDocs;
        }
        //Not in the context yet so load it.
        SchemaField idField = indexSearcher.getSchema().getUniqueKeyField();
        String fieldName = idField.getName();
        HashSet<BytesRef> localBoosts = new HashSet<>(boosted.size() * 2);
        Iterator<BytesRef> boostedIt = boosted.keySet().iterator();
        while (boostedIt.hasNext()) {
            localBoosts.add(boostedIt.next());
        }
        boostDocs = new IntIntHashMap(boosted.size());
        List<LeafReaderContext> leaves = indexSearcher.getTopReaderContext().leaves();
        PostingsEnum postingsEnum = null;
        for (LeafReaderContext leaf : leaves) {
            LeafReader reader = leaf.reader();
            int docBase = leaf.docBase;
            Bits liveDocs = reader.getLiveDocs();
            Terms terms = reader.terms(fieldName);
            TermsEnum termsEnum = terms.iterator();
            Iterator<BytesRef> it = localBoosts.iterator();
            while (it.hasNext()) {
                BytesRef ref = it.next();
                if (termsEnum.seekExact(ref)) {
                    postingsEnum = termsEnum.postings(postingsEnum);
                    int doc = postingsEnum.nextDoc();
                    while (doc != PostingsEnum.NO_MORE_DOCS && liveDocs != null && liveDocs.get(doc) == false) {
                        doc = postingsEnum.nextDoc();
                    }
                    if (doc != PostingsEnum.NO_MORE_DOCS) {
                        //Found the document.
                        int p = boosted.get(ref);
                        boostDocs.put(doc + docBase, p);
                        it.remove();
                    }
                }
            }
        }
    }
    if (context != null) {
        context.put(BOOSTED_DOCIDS, boostDocs);
    }
    return boostDocs;
}
Also used : LeafReader(org.apache.lucene.index.LeafReader) Terms(org.apache.lucene.index.Terms) TermsEnum(org.apache.lucene.index.TermsEnum) SchemaField(org.apache.solr.schema.SchemaField) IntIntHashMap(com.carrotsearch.hppc.IntIntHashMap) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Bits(org.apache.lucene.util.Bits) PostingsEnum(org.apache.lucene.index.PostingsEnum) BytesRef(org.apache.lucene.util.BytesRef) HashSet(java.util.HashSet)

Aggregations

IntIntHashMap (com.carrotsearch.hppc.IntIntHashMap)3 IntFloatHashMap (com.carrotsearch.hppc.IntFloatHashMap)1 IntIntMap (com.carrotsearch.hppc.IntIntMap)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 LeafReader (org.apache.lucene.index.LeafReader)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 PostingsEnum (org.apache.lucene.index.PostingsEnum)1 Terms (org.apache.lucene.index.Terms)1 TermsEnum (org.apache.lucene.index.TermsEnum)1 ScoreDoc (org.apache.lucene.search.ScoreDoc)1 TopDocs (org.apache.lucene.search.TopDocs)1 Bits (org.apache.lucene.util.Bits)1 BytesRef (org.apache.lucene.util.BytesRef)1 SolrException (org.apache.solr.common.SolrException)1 SolrRequestInfo (org.apache.solr.request.SolrRequestInfo)1 SchemaField (org.apache.solr.schema.SchemaField)1 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1