Search in sources :

Example 6 with SearchHit

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

the class InnerHitsIT method testInnerHitsOnHasParent.

public void testInnerHitsOnHasParent() throws Exception {
    assertAcked(prepareCreate("stack").addMapping("question", "body", "type=text").addMapping("answer", "_parent", "type=question", "body", "type=text"));
    List<IndexRequestBuilder> requests = new ArrayList<>();
    requests.add(client().prepareIndex("stack", "question", "1").setSource("body", "I'm using HTTPS + Basic authentication " + "to protect a resource. How can I throttle authentication attempts to protect against brute force attacks?"));
    requests.add(client().prepareIndex("stack", "answer", "1").setParent("1").setSource("body", "install fail2ban and enable rules for apache"));
    requests.add(client().prepareIndex("stack", "question", "2").setSource("body", "I have firewall rules set up and also denyhosts installed.\\ndo I also need to install fail2ban?"));
    requests.add(client().prepareIndex("stack", "answer", "2").setParent("2").setSource("body", "Denyhosts protects only ssh; Fail2Ban protects all daemons."));
    indexRandom(true, requests);
    SearchResponse response = client().prepareSearch("stack").setTypes("answer").addSort("_uid", SortOrder.ASC).setQuery(boolQuery().must(matchQuery("body", "fail2ban")).must(hasParentQuery("question", matchAllQuery(), false).innerHit(new InnerHitBuilder(), false))).get();
    assertNoFailures(response);
    assertHitCount(response, 2);
    SearchHit searchHit = response.getHits().getAt(0);
    assertThat(searchHit.getId(), equalTo("1"));
    assertThat(searchHit.getType(), equalTo("answer"));
    assertThat(searchHit.getInnerHits().get("question").getTotalHits(), equalTo(1L));
    assertThat(searchHit.getInnerHits().get("question").getAt(0).getType(), equalTo("question"));
    assertThat(searchHit.getInnerHits().get("question").getAt(0).getId(), equalTo("1"));
    searchHit = response.getHits().getAt(1);
    assertThat(searchHit.getId(), equalTo("2"));
    assertThat(searchHit.getType(), equalTo("answer"));
    assertThat(searchHit.getInnerHits().get("question").getTotalHits(), equalTo(1L));
    assertThat(searchHit.getInnerHits().get("question").getAt(0).getType(), equalTo("question"));
    assertThat(searchHit.getInnerHits().get("question").getAt(0).getId(), equalTo("2"));
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) ElasticsearchAssertions.assertSearchHit(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHit) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 7 with SearchHit

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

the class InnerHitsIT method testRandomParentChild.

public void testRandomParentChild() throws Exception {
    assertAcked(prepareCreate("idx").addMapping("parent").addMapping("child1", "_parent", "type=parent").addMapping("child2", "_parent", "type=parent"));
    int numDocs = scaledRandomIntBetween(5, 50);
    List<IndexRequestBuilder> requestBuilders = new ArrayList<>();
    int child1 = 0;
    int child2 = 0;
    int[] child1InnerObjects = new int[numDocs];
    int[] child2InnerObjects = new int[numDocs];
    for (int parent = 0; parent < numDocs; parent++) {
        String parentId = String.format(Locale.ENGLISH, "%03d", parent);
        requestBuilders.add(client().prepareIndex("idx", "parent", parentId).setSource("{}", XContentType.JSON));
        int numChildDocs = child1InnerObjects[parent] = scaledRandomIntBetween(1, numDocs);
        int limit = child1 + numChildDocs;
        for (; child1 < limit; child1++) {
            requestBuilders.add(client().prepareIndex("idx", "child1", String.format(Locale.ENGLISH, "%04d", child1)).setParent(parentId).setSource("{}", XContentType.JSON));
        }
        numChildDocs = child2InnerObjects[parent] = scaledRandomIntBetween(1, numDocs);
        limit = child2 + numChildDocs;
        for (; child2 < limit; child2++) {
            requestBuilders.add(client().prepareIndex("idx", "child2", String.format(Locale.ENGLISH, "%04d", child2)).setParent(parentId).setSource("{}", XContentType.JSON));
        }
    }
    indexRandom(true, requestBuilders);
    int size = randomIntBetween(0, numDocs);
    BoolQueryBuilder boolQuery = new BoolQueryBuilder();
    boolQuery.should(constantScoreQuery(hasChildQuery("child1", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder().setName("a").addSort(new FieldSortBuilder("_uid").order(SortOrder.ASC)).setSize(size), false)));
    boolQuery.should(constantScoreQuery(hasChildQuery("child2", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder().setName("b").addSort(new FieldSortBuilder("_uid").order(SortOrder.ASC)).setSize(size), false)));
    SearchResponse searchResponse = client().prepareSearch("idx").setSize(numDocs).setTypes("parent").addSort("_uid", SortOrder.ASC).setQuery(boolQuery).get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, numDocs);
    assertThat(searchResponse.getHits().getHits().length, equalTo(numDocs));
    int offset1 = 0;
    int offset2 = 0;
    for (int parent = 0; parent < numDocs; parent++) {
        SearchHit searchHit = searchResponse.getHits().getAt(parent);
        assertThat(searchHit.getType(), equalTo("parent"));
        assertThat(searchHit.getId(), equalTo(String.format(Locale.ENGLISH, "%03d", parent)));
        assertThat(searchHit.getShard(), notNullValue());
        SearchHits inner = searchHit.getInnerHits().get("a");
        assertThat(inner.getTotalHits(), equalTo((long) child1InnerObjects[parent]));
        for (int child = 0; child < child1InnerObjects[parent] && child < size; child++) {
            SearchHit innerHit = inner.getAt(child);
            assertThat(innerHit.getType(), equalTo("child1"));
            String childId = String.format(Locale.ENGLISH, "%04d", offset1 + child);
            assertThat(innerHit.getId(), equalTo(childId));
            assertThat(innerHit.getNestedIdentity(), nullValue());
        }
        offset1 += child1InnerObjects[parent];
        inner = searchHit.getInnerHits().get("b");
        assertThat(inner.getTotalHits(), equalTo((long) child2InnerObjects[parent]));
        for (int child = 0; child < child2InnerObjects[parent] && child < size; child++) {
            SearchHit innerHit = inner.getAt(child);
            assertThat(innerHit.getType(), equalTo("child2"));
            String childId = String.format(Locale.ENGLISH, "%04d", offset2 + child);
            assertThat(innerHit.getId(), equalTo(childId));
            assertThat(innerHit.getNestedIdentity(), nullValue());
        }
        offset2 += child2InnerObjects[parent];
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) ElasticsearchAssertions.assertSearchHit(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHit) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 8 with SearchHit

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

the class TransportTwoNodesSearchIT method testDfsQueryThenFetch.

public void testDfsQueryThenFetch() throws Exception {
    Settings.Builder settingsBuilder = Settings.builder().put(indexSettings());
    client().admin().indices().create(createIndexRequest("test").settings(settingsBuilder)).actionGet();
    ensureGreen();
    // it should be if dfs is working correctly)
    for (int i = 1024; i < 1124; i++) {
        index(Integer.toString(i - 1024), "test", i);
    }
    refresh();
    int total = 0;
    SearchResponse searchResponse = client().prepareSearch("test").setSearchType(DFS_QUERY_THEN_FETCH).setQuery(termQuery("multi", "test")).setSize(60).setExplain(true).setScroll(TimeValue.timeValueSeconds(30)).get();
    while (true) {
        assertNoFailures(searchResponse);
        assertThat(searchResponse.getHits().getTotalHits(), equalTo(100L));
        SearchHit[] hits = searchResponse.getHits().getHits();
        if (hits.length == 0) {
            // finished
            break;
        }
        for (int i = 0; i < hits.length; ++i) {
            SearchHit hit = hits[i];
            assertThat(hit.getExplanation(), notNullValue());
            assertThat(hit.getExplanation().getDetails().length, equalTo(1));
            assertThat(hit.getExplanation().getDetails()[0].getDetails().length, equalTo(2));
            assertThat(hit.getExplanation().getDetails()[0].getDetails()[0].getDetails().length, equalTo(2));
            assertThat(hit.getExplanation().getDetails()[0].getDetails()[0].getDetails()[0].getDescription(), equalTo("docFreq"));
            assertThat(hit.getExplanation().getDetails()[0].getDetails()[0].getDetails()[0].getValue(), equalTo(100.0f));
            assertThat(hit.getExplanation().getDetails()[0].getDetails()[0].getDetails()[1].getDescription(), equalTo("docCount"));
            assertThat(hit.getExplanation().getDetails()[0].getDetails()[0].getDetails()[1].getValue(), equalTo(100.0f));
            assertThat("id[" + hit.getId() + "] -> " + hit.getExplanation().toString(), hit.getId(), equalTo(Integer.toString(100 - total - i - 1)));
        }
        total += hits.length;
        searchResponse = client().prepareSearchScroll(searchResponse.getScrollId()).setScroll(TimeValue.timeValueSeconds(30)).get();
    }
    clearScroll(searchResponse.getScrollId());
    assertEquals(100, total);
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) Settings(org.elasticsearch.common.settings.Settings) SearchResponse(org.elasticsearch.action.search.SearchResponse) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse)

Example 9 with SearchHit

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

the class TransportTwoNodesSearchIT method testQueryThenFetchWithFrom.

public void testQueryThenFetchWithFrom() throws Exception {
    Set<String> fullExpectedIds = prepareData();
    SearchSourceBuilder source = searchSource().query(matchAllQuery()).explain(true);
    Set<String> collectedIds = new TreeSet<>();
    SearchResponse searchResponse = client().search(searchRequest("test").source(source.from(0).size(60)).searchType(QUERY_THEN_FETCH)).actionGet();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(100L));
    assertThat(searchResponse.getHits().getHits().length, equalTo(60));
    for (int i = 0; i < 60; i++) {
        SearchHit hit = searchResponse.getHits().getHits()[i];
        collectedIds.add(hit.getId());
    }
    searchResponse = client().search(searchRequest("test").source(source.from(60).size(60)).searchType(QUERY_THEN_FETCH)).actionGet();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(100L));
    assertThat(searchResponse.getHits().getHits().length, equalTo(40));
    for (int i = 0; i < 40; i++) {
        SearchHit hit = searchResponse.getHits().getHits()[i];
        collectedIds.add(hit.getId());
    }
    assertThat(collectedIds, equalTo(fullExpectedIds));
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) TreeSet(java.util.TreeSet) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse)

Example 10 with SearchHit

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

the class TransportTwoNodesSearchIT method testQueryThenFetch.

public void testQueryThenFetch() throws Exception {
    prepareData();
    int total = 0;
    SearchResponse searchResponse = client().prepareSearch("test").setSearchType(QUERY_THEN_FETCH).setQuery(termQuery("multi", "test")).setSize(60).setExplain(true).addSort("nid", SortOrder.DESC).setScroll(TimeValue.timeValueSeconds(30)).get();
    while (true) {
        assertNoFailures(searchResponse);
        assertThat(searchResponse.getHits().getTotalHits(), equalTo(100L));
        SearchHit[] hits = searchResponse.getHits().getHits();
        if (hits.length == 0) {
            // finished
            break;
        }
        for (int i = 0; i < hits.length; ++i) {
            SearchHit hit = hits[i];
            assertThat(hit.getExplanation(), notNullValue());
            assertThat("id[" + hit.getId() + "]", hit.getId(), equalTo(Integer.toString(100 - total - i - 1)));
        }
        total += hits.length;
        searchResponse = client().prepareSearchScroll(searchResponse.getScrollId()).setScroll(TimeValue.timeValueSeconds(30)).get();
    }
    clearScroll(searchResponse.getScrollId());
    assertEquals(100, total);
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchResponse(org.elasticsearch.action.search.SearchResponse) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse)

Aggregations

SearchHit (org.elasticsearch.search.SearchHit)160 SearchResponse (org.elasticsearch.action.search.SearchResponse)112 SearchHits (org.elasticsearch.search.SearchHits)49 ArrayList (java.util.ArrayList)27 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)25 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)20 IOException (java.io.IOException)18 Matchers.containsString (org.hamcrest.Matchers.containsString)17 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)16 HashMap (java.util.HashMap)14 ScoreDoc (org.apache.lucene.search.ScoreDoc)14 SearchHitField (org.elasticsearch.search.SearchHitField)14 HashSet (java.util.HashSet)12 Test (org.junit.Test)12 Map (java.util.Map)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 TopDocs (org.apache.lucene.search.TopDocs)10 Text (org.elasticsearch.common.text.Text)10 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)9 FetchSearchResult (org.elasticsearch.search.fetch.FetchSearchResult)9