Search in sources :

Example 11 with ScoreDoc

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

the class TermVectorsUnitTests method writeStandardTermVector.

private void writeStandardTermVector(TermVectorsResponse outResponse) throws IOException {
    Directory dir = newDirectory();
    IndexWriterConfig conf = new IndexWriterConfig(new StandardAnalyzer());
    conf.setOpenMode(OpenMode.CREATE);
    IndexWriter writer = new IndexWriter(dir, conf);
    FieldType type = new FieldType(TextField.TYPE_STORED);
    type.setStoreTermVectorOffsets(true);
    type.setStoreTermVectorPayloads(false);
    type.setStoreTermVectorPositions(true);
    type.setStoreTermVectors(true);
    type.freeze();
    Document d = new Document();
    d.add(new Field("id", "abc", StringField.TYPE_STORED));
    d.add(new Field("title", "the1 quick brown fox jumps over  the1 lazy dog", type));
    d.add(new Field("desc", "the1 quick brown fox jumps over  the1 lazy dog", type));
    writer.updateDocument(new Term("id", "abc"), d);
    writer.commit();
    writer.close();
    DirectoryReader dr = DirectoryReader.open(dir);
    IndexSearcher s = new IndexSearcher(dr);
    TopDocs search = s.search(new TermQuery(new Term("id", "abc")), 1);
    ScoreDoc[] scoreDocs = search.scoreDocs;
    int doc = scoreDocs[0].doc;
    Fields termVectors = dr.getTermVectors(doc);
    EnumSet<Flag> flags = EnumSet.of(Flag.Positions, Flag.Offsets);
    outResponse.setFields(termVectors, null, flags, termVectors);
    dr.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) DirectoryReader(org.apache.lucene.index.DirectoryReader) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) Flag(org.elasticsearch.action.termvectors.TermVectorsRequest.Flag) FieldType(org.apache.lucene.document.FieldType) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) StringField(org.apache.lucene.document.StringField) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) Fields(org.apache.lucene.index.Fields) IndexWriter(org.apache.lucene.index.IndexWriter) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 12 with ScoreDoc

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

the class BestDocsDeferringCollector method runDeferredAggs.

private void runDeferredAggs() throws IOException {
    List<ScoreDoc> allDocs = new ArrayList<>(shardSize);
    for (int i = 0; i < perBucketSamples.size(); i++) {
        PerParentBucketSamples perBucketSample = perBucketSamples.get(i);
        if (perBucketSample == null) {
            continue;
        }
        perBucketSample.getMatches(allDocs);
    }
    // Sort the top matches by docID for the benefit of deferred collector
    ScoreDoc[] docsArr = allDocs.toArray(new ScoreDoc[allDocs.size()]);
    Arrays.sort(docsArr, (o1, o2) -> {
        if (o1.doc == o2.doc) {
            return o1.shardIndex - o2.shardIndex;
        }
        return o1.doc - o2.doc;
    });
    try {
        for (PerSegmentCollects perSegDocs : entries) {
            perSegDocs.replayRelatedMatches(docsArr);
        }
    } catch (IOException e) {
        throw new ElasticsearchException("IOException collecting best scoring results", e);
    }
    deferred.postCollection();
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 13 with ScoreDoc

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

the class SearchPhaseControllerTests method testMerge.

public void testMerge() throws IOException {
    List<CompletionSuggestion> suggestions = new ArrayList<>();
    for (int i = 0; i < randomIntBetween(1, 5); i++) {
        suggestions.add(new CompletionSuggestion(randomAsciiOfLength(randomIntBetween(1, 5)), randomIntBetween(1, 20)));
    }
    int nShards = randomIntBetween(1, 20);
    int queryResultSize = randomBoolean() ? 0 : randomIntBetween(1, nShards * 2);
    AtomicArray<QuerySearchResultProvider> queryResults = generateQueryResults(nShards, suggestions, queryResultSize, false);
    // calculate offsets and score doc array
    List<ScoreDoc> mergedScoreDocs = new ArrayList<>();
    ScoreDoc[] mergedSearchDocs = getTopShardDocs(queryResults);
    mergedScoreDocs.addAll(Arrays.asList(mergedSearchDocs));
    Suggest mergedSuggest = reducedSuggest(queryResults);
    for (Suggest.Suggestion<?> suggestion : mergedSuggest) {
        if (suggestion instanceof CompletionSuggestion) {
            CompletionSuggestion completionSuggestion = ((CompletionSuggestion) suggestion);
            mergedScoreDocs.addAll(completionSuggestion.getOptions().stream().map(CompletionSuggestion.Entry.Option::getDoc).collect(Collectors.toList()));
        }
    }
    ScoreDoc[] sortedDocs = mergedScoreDocs.toArray(new ScoreDoc[mergedScoreDocs.size()]);
    InternalSearchResponse mergedResponse = searchPhaseController.merge(true, sortedDocs, searchPhaseController.reducedQueryPhase(queryResults.asList()), generateFetchResults(nShards, mergedSearchDocs, mergedSuggest));
    assertThat(mergedResponse.hits().getHits().length, equalTo(mergedSearchDocs.length));
    Suggest suggestResult = mergedResponse.suggest();
    for (Suggest.Suggestion<?> suggestion : mergedSuggest) {
        assertThat(suggestion, instanceOf(CompletionSuggestion.class));
        if (suggestion.getEntries().get(0).getOptions().size() > 0) {
            CompletionSuggestion suggestionResult = suggestResult.getSuggestion(suggestion.getName());
            assertNotNull(suggestionResult);
            List<CompletionSuggestion.Entry.Option> options = suggestionResult.getEntries().get(0).getOptions();
            assertThat(options.size(), equalTo(suggestion.getEntries().get(0).getOptions().size()));
            for (CompletionSuggestion.Entry.Option option : options) {
                assertNotNull(option.getHit());
            }
        }
    }
}
Also used : CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) ArrayList(java.util.ArrayList) Suggest(org.elasticsearch.search.suggest.Suggest) ScoreDoc(org.apache.lucene.search.ScoreDoc) InternalSearchResponse(org.elasticsearch.search.internal.InternalSearchResponse)

Example 14 with ScoreDoc

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

the class SearchPhaseControllerTests method generateFetchResults.

private AtomicArray<QuerySearchResultProvider> generateFetchResults(int nShards, ScoreDoc[] mergedSearchDocs, Suggest mergedSuggest) {
    AtomicArray<QuerySearchResultProvider> fetchResults = new AtomicArray<>(nShards);
    for (int shardIndex = 0; shardIndex < nShards; shardIndex++) {
        float maxScore = -1F;
        SearchShardTarget shardTarget = new SearchShardTarget("", new Index("", ""), shardIndex);
        FetchSearchResult fetchSearchResult = new FetchSearchResult(shardIndex, shardTarget);
        List<SearchHit> searchHits = new ArrayList<>();
        for (ScoreDoc scoreDoc : mergedSearchDocs) {
            if (scoreDoc.shardIndex == shardIndex) {
                searchHits.add(new SearchHit(scoreDoc.doc, "", new Text(""), Collections.emptyMap()));
                if (scoreDoc.score > maxScore) {
                    maxScore = scoreDoc.score;
                }
            }
        }
        for (Suggest.Suggestion<?> suggestion : mergedSuggest) {
            if (suggestion instanceof CompletionSuggestion) {
                for (CompletionSuggestion.Entry.Option option : ((CompletionSuggestion) suggestion).getOptions()) {
                    ScoreDoc doc = option.getDoc();
                    if (doc.shardIndex == shardIndex) {
                        searchHits.add(new SearchHit(doc.doc, "", new Text(""), Collections.emptyMap()));
                        if (doc.score > maxScore) {
                            maxScore = doc.score;
                        }
                    }
                }
            }
        }
        SearchHit[] hits = searchHits.toArray(new SearchHit[searchHits.size()]);
        fetchSearchResult.hits(new SearchHits(hits, hits.length, maxScore));
        fetchResults.set(shardIndex, fetchSearchResult);
    }
    return fetchResults;
}
Also used : AtomicArray(org.elasticsearch.common.util.concurrent.AtomicArray) CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) SearchHit(org.elasticsearch.search.SearchHit) FetchSearchResult(org.elasticsearch.search.fetch.FetchSearchResult) ArrayList(java.util.ArrayList) Index(org.elasticsearch.index.Index) Text(org.elasticsearch.common.text.Text) Suggest(org.elasticsearch.search.suggest.Suggest) ScoreDoc(org.apache.lucene.search.ScoreDoc) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) SearchHits(org.elasticsearch.search.SearchHits)

Example 15 with ScoreDoc

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

the class SearchPhaseControllerTests method testSort.

public void testSort() throws Exception {
    List<CompletionSuggestion> suggestions = new ArrayList<>();
    for (int i = 0; i < randomIntBetween(1, 5); i++) {
        suggestions.add(new CompletionSuggestion(randomAsciiOfLength(randomIntBetween(1, 5)), randomIntBetween(1, 20)));
    }
    int nShards = randomIntBetween(1, 20);
    int queryResultSize = randomBoolean() ? 0 : randomIntBetween(1, nShards * 2);
    AtomicArray<QuerySearchResultProvider> results = generateQueryResults(nShards, suggestions, queryResultSize, false);
    ScoreDoc[] sortedDocs = searchPhaseController.sortDocs(true, results);
    int accumulatedLength = Math.min(queryResultSize, getTotalQueryHits(results));
    for (Suggest.Suggestion<?> suggestion : reducedSuggest(results)) {
        int suggestionSize = suggestion.getEntries().get(0).getOptions().size();
        accumulatedLength += suggestionSize;
    }
    assertThat(sortedDocs.length, equalTo(accumulatedLength));
}
Also used : CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) ArrayList(java.util.ArrayList) Suggest(org.elasticsearch.search.suggest.Suggest) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Aggregations

ScoreDoc (org.apache.lucene.search.ScoreDoc)211 TopDocs (org.apache.lucene.search.TopDocs)119 IndexSearcher (org.apache.lucene.search.IndexSearcher)94 Document (org.apache.lucene.document.Document)89 Query (org.apache.lucene.search.Query)65 TermQuery (org.apache.lucene.search.TermQuery)49 ArrayList (java.util.ArrayList)46 IOException (java.io.IOException)44 IndexReader (org.apache.lucene.index.IndexReader)42 Term (org.apache.lucene.index.Term)38 Directory (org.apache.lucene.store.Directory)37 BooleanQuery (org.apache.lucene.search.BooleanQuery)26 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)23 Sort (org.apache.lucene.search.Sort)22 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)21 Test (org.junit.Test)21 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)20 FieldDoc (org.apache.lucene.search.FieldDoc)20 HashMap (java.util.HashMap)18 HashSet (java.util.HashSet)17