Search in sources :

Example 16 with SearchHitField

use of org.elasticsearch.search.SearchHitField in project elasticsearch by elastic.

the class TopHitsIT method testFetchFeatures.

public void testFetchFeatures() {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").setQuery(matchQuery("text", "text").queryName("test")).addAggregation(terms("terms").executionHint(randomExecutionHint()).field(TERMS_AGGS_FIELD).subAggregation(topHits("hits").size(1).highlighter(new HighlightBuilder().field("text")).explain(true).storedField("text").fieldDataField("field1").scriptField("script", new Script(ScriptType.INLINE, MockScriptEngine.NAME, "5", Collections.emptyMap())).fetchSource("text", null).version(true))).get();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(5));
    for (Terms.Bucket bucket : terms.getBuckets()) {
        TopHits topHits = bucket.getAggregations().get("hits");
        SearchHits hits = topHits.getHits();
        assertThat(hits.getTotalHits(), equalTo(10L));
        assertThat(hits.getHits().length, equalTo(1));
        SearchHit hit = hits.getAt(0);
        HighlightField highlightField = hit.getHighlightFields().get("text");
        assertThat(highlightField.getFragments().length, equalTo(1));
        assertThat(highlightField.getFragments()[0].string(), equalTo("some <em>text</em> to entertain"));
        Explanation explanation = hit.getExplanation();
        assertThat(explanation.toString(), containsString("text:text"));
        long version = hit.getVersion();
        assertThat(version, equalTo(1L));
        assertThat(hit.getMatchedQueries()[0], equalTo("test"));
        SearchHitField field = hit.field("field1");
        assertThat(field.getValue().toString(), equalTo("5"));
        assertThat(hit.getSourceAsMap().get("text").toString(), equalTo("some text to entertain"));
        field = hit.field("script");
        assertThat(field.getValue().toString(), equalTo("5"));
        assertThat(hit.getSourceAsMap().size(), equalTo(1));
        assertThat(hit.getSourceAsMap().get("text").toString(), equalTo("some text to entertain"));
    }
}
Also used : Script(org.elasticsearch.script.Script) TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) SearchHit(org.elasticsearch.search.SearchHit) Explanation(org.apache.lucene.search.Explanation) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) SearchHitField(org.elasticsearch.search.SearchHitField) SearchHits(org.elasticsearch.search.SearchHits) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 17 with SearchHitField

use of org.elasticsearch.search.SearchHitField in project elasticsearch by elastic.

the class ExpandSearchPhaseTests method testFailOneItemFailsEntirePhase.

public void testFailOneItemFailsEntirePhase() throws IOException {
    AtomicBoolean executedMultiSearch = new AtomicBoolean(false);
    SearchHits collapsedHits = new SearchHits(new SearchHit[] { new SearchHit(2, "ID", new Text("type"), Collections.emptyMap()), new SearchHit(3, "ID", new Text("type"), Collections.emptyMap()) }, 1, 1.0F);
    MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(1);
    String collapseValue = randomBoolean() ? null : "boom";
    mockSearchPhaseContext.getRequest().source(new SearchSourceBuilder().collapse(new CollapseBuilder("someField").setInnerHits(new InnerHitBuilder().setName("foobarbaz"))));
    mockSearchPhaseContext.searchTransport = new SearchTransportService(Settings.builder().put("search.remote.connect", false).build(), null, null) {

        @Override
        void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionListener<MultiSearchResponse> listener) {
            assertTrue(executedMultiSearch.compareAndSet(false, true));
            InternalSearchResponse internalSearchResponse = new InternalSearchResponse(collapsedHits, null, null, null, false, null, 1);
            SearchResponse response = mockSearchPhaseContext.buildSearchResponse(internalSearchResponse, null);
            listener.onResponse(new MultiSearchResponse(new MultiSearchResponse.Item[] { new MultiSearchResponse.Item(null, new RuntimeException("boom")), new MultiSearchResponse.Item(response, null) }));
        }
    };
    SearchHits hits = new SearchHits(new SearchHit[] { new SearchHit(1, "ID", new Text("type"), Collections.singletonMap("someField", new SearchHitField("someField", Collections.singletonList(collapseValue)))), new SearchHit(2, "ID2", new Text("type"), Collections.singletonMap("someField", new SearchHitField("someField", Collections.singletonList(collapseValue)))) }, 1, 1.0F);
    InternalSearchResponse internalSearchResponse = new InternalSearchResponse(hits, null, null, null, false, null, 1);
    SearchResponse response = mockSearchPhaseContext.buildSearchResponse(internalSearchResponse, null);
    AtomicReference<SearchResponse> reference = new AtomicReference<>();
    ExpandSearchPhase phase = new ExpandSearchPhase(mockSearchPhaseContext, response, r -> new SearchPhase("test") {

        @Override
        public void run() throws IOException {
            reference.set(r);
        }
    });
    phase.run();
    assertThat(mockSearchPhaseContext.phaseFailure.get(), Matchers.instanceOf(RuntimeException.class));
    assertEquals("boom", mockSearchPhaseContext.phaseFailure.get().getMessage());
    assertNotNull(mockSearchPhaseContext.phaseFailure.get());
    assertNull(reference.get());
    assertEquals(0, mockSearchPhaseContext.phasesExecuted.get());
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchHits(org.elasticsearch.search.SearchHits) CollapseBuilder(org.elasticsearch.search.collapse.CollapseBuilder) Text(org.elasticsearch.common.text.Text) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) InternalSearchResponse(org.elasticsearch.search.internal.InternalSearchResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SearchHitField(org.elasticsearch.search.SearchHitField) InternalSearchResponse(org.elasticsearch.search.internal.InternalSearchResponse)

Example 18 with SearchHitField

use of org.elasticsearch.search.SearchHitField in project elasticsearch by elastic.

the class ExpandSearchPhaseTests method testSkipPhase.

public void testSkipPhase() throws IOException {
    MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(1);
    mockSearchPhaseContext.searchTransport = new SearchTransportService(Settings.builder().put("search.remote.connect", false).build(), null, null) {

        @Override
        void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionListener<MultiSearchResponse> listener) {
            fail("no collapsing here");
        }
    };
    SearchHits hits = new SearchHits(new SearchHit[] { new SearchHit(1, "ID", new Text("type"), Collections.singletonMap("someField", new SearchHitField("someField", Collections.singletonList(null)))), new SearchHit(2, "ID2", new Text("type"), Collections.singletonMap("someField", new SearchHitField("someField", Collections.singletonList(null)))) }, 1, 1.0F);
    InternalSearchResponse internalSearchResponse = new InternalSearchResponse(hits, null, null, null, false, null, 1);
    SearchResponse response = mockSearchPhaseContext.buildSearchResponse(internalSearchResponse, null);
    AtomicReference<SearchResponse> reference = new AtomicReference<>();
    ExpandSearchPhase phase = new ExpandSearchPhase(mockSearchPhaseContext, response, r -> new SearchPhase("test") {

        @Override
        public void run() throws IOException {
            reference.set(r);
        }
    });
    phase.run();
    mockSearchPhaseContext.assertNoFailure();
    assertNotNull(reference.get());
    assertEquals(1, mockSearchPhaseContext.phasesExecuted.get());
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) Text(org.elasticsearch.common.text.Text) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) InternalSearchResponse(org.elasticsearch.search.internal.InternalSearchResponse) SearchHitField(org.elasticsearch.search.SearchHitField) SearchHits(org.elasticsearch.search.SearchHits) InternalSearchResponse(org.elasticsearch.search.internal.InternalSearchResponse)

Example 19 with SearchHitField

use of org.elasticsearch.search.SearchHitField in project elasticsearch by elastic.

the class AbstractGeoTestCase method setupSuiteScopeCluster.

@Override
public void setupSuiteScopeCluster() throws Exception {
    createIndex(UNMAPPED_IDX_NAME);
    assertAcked(prepareCreate(IDX_NAME).addMapping("type", SINGLE_VALUED_FIELD_NAME, "type=geo_point", MULTI_VALUED_FIELD_NAME, "type=geo_point", NUMBER_FIELD_NAME, "type=long", "tag", "type=keyword"));
    singleTopLeft = new GeoPoint(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
    singleBottomRight = new GeoPoint(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY);
    multiTopLeft = new GeoPoint(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
    multiBottomRight = new GeoPoint(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY);
    singleCentroid = new GeoPoint(0, 0);
    multiCentroid = new GeoPoint(0, 0);
    unmappedCentroid = new GeoPoint(0, 0);
    numDocs = randomIntBetween(6, 20);
    numUniqueGeoPoints = randomIntBetween(1, numDocs);
    expectedDocCountsForGeoHash = new ObjectIntHashMap<>(numDocs * 2);
    expectedCentroidsForGeoHash = new ObjectObjectHashMap<>(numDocs * 2);
    singleValues = new GeoPoint[numUniqueGeoPoints];
    for (int i = 0; i < singleValues.length; i++) {
        singleValues[i] = RandomGeoGenerator.randomPoint(random());
        updateBoundsTopLeft(singleValues[i], singleTopLeft);
        updateBoundsBottomRight(singleValues[i], singleBottomRight);
    }
    multiValues = new GeoPoint[numUniqueGeoPoints];
    for (int i = 0; i < multiValues.length; i++) {
        multiValues[i] = RandomGeoGenerator.randomPoint(random());
        updateBoundsTopLeft(multiValues[i], multiTopLeft);
        updateBoundsBottomRight(multiValues[i], multiBottomRight);
    }
    List<IndexRequestBuilder> builders = new ArrayList<>();
    GeoPoint singleVal;
    final GeoPoint[] multiVal = new GeoPoint[2];
    double newMVLat, newMVLon;
    for (int i = 0; i < numDocs; i++) {
        singleVal = singleValues[i % numUniqueGeoPoints];
        multiVal[0] = multiValues[i % numUniqueGeoPoints];
        multiVal[1] = multiValues[(i + 1) % numUniqueGeoPoints];
        builders.add(client().prepareIndex(IDX_NAME, "type").setSource(jsonBuilder().startObject().array(SINGLE_VALUED_FIELD_NAME, singleVal.lon(), singleVal.lat()).startArray(MULTI_VALUED_FIELD_NAME).startArray().value(multiVal[0].lon()).value(multiVal[0].lat()).endArray().startArray().value(multiVal[1].lon()).value(multiVal[1].lat()).endArray().endArray().field(NUMBER_FIELD_NAME, i).field("tag", "tag" + i).endObject()));
        singleCentroid = singleCentroid.reset(singleCentroid.lat() + (singleVal.lat() - singleCentroid.lat()) / (i + 1), singleCentroid.lon() + (singleVal.lon() - singleCentroid.lon()) / (i + 1));
        newMVLat = (multiVal[0].lat() + multiVal[1].lat()) / 2d;
        newMVLon = (multiVal[0].lon() + multiVal[1].lon()) / 2d;
        multiCentroid = multiCentroid.reset(multiCentroid.lat() + (newMVLat - multiCentroid.lat()) / (i + 1), multiCentroid.lon() + (newMVLon - multiCentroid.lon()) / (i + 1));
    }
    assertAcked(prepareCreate(EMPTY_IDX_NAME).addMapping("type", SINGLE_VALUED_FIELD_NAME, "type=geo_point"));
    assertAcked(prepareCreate(DATELINE_IDX_NAME).addMapping("type", SINGLE_VALUED_FIELD_NAME, "type=geo_point", MULTI_VALUED_FIELD_NAME, "type=geo_point", NUMBER_FIELD_NAME, "type=long", "tag", "type=keyword"));
    GeoPoint[] geoValues = new GeoPoint[5];
    geoValues[0] = new GeoPoint(38, 178);
    geoValues[1] = new GeoPoint(12, -179);
    geoValues[2] = new GeoPoint(-24, 170);
    geoValues[3] = new GeoPoint(32, -175);
    geoValues[4] = new GeoPoint(-11, 178);
    for (int i = 0; i < 5; i++) {
        builders.add(client().prepareIndex(DATELINE_IDX_NAME, "type").setSource(jsonBuilder().startObject().array(SINGLE_VALUED_FIELD_NAME, geoValues[i].lon(), geoValues[i].lat()).field(NUMBER_FIELD_NAME, i).field("tag", "tag" + i).endObject()));
    }
    assertAcked(prepareCreate(HIGH_CARD_IDX_NAME).setSettings(Settings.builder().put("number_of_shards", 2)).addMapping("type", SINGLE_VALUED_FIELD_NAME, "type=geo_point", MULTI_VALUED_FIELD_NAME, "type=geo_point", NUMBER_FIELD_NAME, "type=long,store=true", "tag", "type=keyword"));
    for (int i = 0; i < 2000; i++) {
        singleVal = singleValues[i % numUniqueGeoPoints];
        builders.add(client().prepareIndex(HIGH_CARD_IDX_NAME, "type").setSource(jsonBuilder().startObject().array(SINGLE_VALUED_FIELD_NAME, singleVal.lon(), singleVal.lat()).startArray(MULTI_VALUED_FIELD_NAME).startArray().value(multiValues[i % numUniqueGeoPoints].lon()).value(multiValues[i % numUniqueGeoPoints].lat()).endArray().startArray().value(multiValues[(i + 1) % numUniqueGeoPoints].lon()).value(multiValues[(i + 1) % numUniqueGeoPoints].lat()).endArray().endArray().field(NUMBER_FIELD_NAME, i).field("tag", "tag" + i).endObject()));
        updateGeohashBucketsCentroid(singleVal);
    }
    builders.add(client().prepareIndex(IDX_ZERO_NAME, "type").setSource(jsonBuilder().startObject().array(SINGLE_VALUED_FIELD_NAME, 0.0, 1.0).endObject()));
    assertAcked(prepareCreate(IDX_ZERO_NAME).addMapping("type", SINGLE_VALUED_FIELD_NAME, "type=geo_point"));
    indexRandom(true, builders);
    ensureSearchable();
    // Added to debug a test failure where the terms aggregation seems to be reporting two documents with the same value for NUMBER_FIELD_NAME.  This will check that after
    // random indexing each document only has 1 value for NUMBER_FIELD_NAME and it is the correct value. Following this initial change its seems that this call was getting
    // more that 2000 hits (actual value was 2059) so now it will also check to ensure all hits have the correct index and type
    SearchResponse response = client().prepareSearch(HIGH_CARD_IDX_NAME).addStoredField(NUMBER_FIELD_NAME).addSort(SortBuilders.fieldSort(NUMBER_FIELD_NAME).order(SortOrder.ASC)).setSize(5000).get();
    assertSearchResponse(response);
    long totalHits = response.getHits().getTotalHits();
    XContentBuilder builder = XContentFactory.jsonBuilder();
    response.toXContent(builder, ToXContent.EMPTY_PARAMS);
    logger.info("Full high_card_idx Response Content:\n{ {} }", builder.string());
    for (int i = 0; i < totalHits; i++) {
        SearchHit searchHit = response.getHits().getAt(i);
        assertThat("Hit " + i + " with id: " + searchHit.getId(), searchHit.getIndex(), equalTo("high_card_idx"));
        assertThat("Hit " + i + " with id: " + searchHit.getId(), searchHit.getType(), equalTo("type"));
        SearchHitField hitField = searchHit.field(NUMBER_FIELD_NAME);
        assertThat("Hit " + i + " has wrong number of values", hitField.getValues().size(), equalTo(1));
        Long value = hitField.getValue();
        assertThat("Hit " + i + " has wrong value", value.intValue(), equalTo(i));
    }
    assertThat(totalHits, equalTo(2000L));
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchHitField(org.elasticsearch.search.SearchHitField) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 20 with SearchHitField

use of org.elasticsearch.search.SearchHitField in project elasticsearch by elastic.

the class InternalTopHitsTests method createTestInstance.

@Override
protected InternalTopHits createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
    int from = 0;
    int requestedSize = between(1, 40);
    int actualSize = between(0, requestedSize);
    float maxScore = Float.MIN_VALUE;
    ScoreDoc[] scoreDocs = new ScoreDoc[actualSize];
    SearchHit[] hits = new SearchHit[actualSize];
    Set<Integer> usedDocIds = new HashSet<>();
    for (int i = 0; i < actualSize; i++) {
        float score = randomFloat();
        maxScore = max(maxScore, score);
        int docId = randomValueOtherThanMany(usedDocIds::contains, () -> between(0, IndexWriter.MAX_DOCS));
        usedDocIds.add(docId);
        Map<String, SearchHitField> searchHitFields = new HashMap<>();
        if (testInstancesLookSortedByField) {
            Object[] fields = new Object[testInstancesSortFields.length];
            for (int f = 0; f < testInstancesSortFields.length; f++) {
                fields[f] = randomOfType(testInstancesSortFields[f].getType());
            }
            scoreDocs[i] = new FieldDoc(docId, score, fields);
        } else {
            scoreDocs[i] = new ScoreDoc(docId, score);
        }
        hits[i] = new SearchHit(docId, Integer.toString(i), new Text("test"), searchHitFields);
        hits[i].score(score);
    }
    int totalHits = between(actualSize, 500000);
    SearchHits searchHits = new SearchHits(hits, totalHits, maxScore);
    TopDocs topDocs;
    Arrays.sort(scoreDocs, scoreDocComparator());
    if (testInstancesLookSortedByField) {
        topDocs = new TopFieldDocs(totalHits, scoreDocs, testInstancesSortFields, maxScore);
    } else {
        topDocs = new TopDocs(totalHits, scoreDocs, maxScore);
    }
    return new InternalTopHits(name, from, requestedSize, topDocs, searchHits, pipelineAggregators, metaData);
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) FieldDoc(org.apache.lucene.search.FieldDoc) HashMap(java.util.HashMap) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) Text(org.elasticsearch.common.text.Text) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) SearchHitField(org.elasticsearch.search.SearchHitField) SearchHits(org.elasticsearch.search.SearchHits) HashSet(java.util.HashSet)

Aggregations

SearchHitField (org.elasticsearch.search.SearchHitField)23 SearchHit (org.elasticsearch.search.SearchHit)16 SearchResponse (org.elasticsearch.action.search.SearchResponse)12 SearchHits (org.elasticsearch.search.SearchHits)10 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)8 Map (java.util.Map)7 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)7 IOException (java.io.IOException)6 List (java.util.List)6 Text (org.elasticsearch.common.text.Text)6 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)5 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)4 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)4 Script (org.elasticsearch.script.Script)4 CollapseBuilder (org.elasticsearch.search.collapse.CollapseBuilder)4 HighlightBuilder (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)4 QueryHelper (org.codelibs.fess.helper.QueryHelper)3 FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)3 OptionalEntity (org.dbflute.optional.OptionalEntity)3