Package org.elasticsearch.search

Examples of org.elasticsearch.search.SearchHits


            ExtTIntArrayList docIds = entry.getValue();
            FetchSearchResult fetchResult = nodeToSearchService.get(shardTarget.nodeId()).executeFetchPhase(new FetchSearchRequest(queryResults.get(shardTarget).queryResult().id(), docIds));
            fetchResults.put(fetchResult.shardTarget(), fetchResult.initCounter());
        }

        SearchHits hits = searchPhaseController.merge(sortedShardList, queryResults, fetchResults).hits();

        assertThat(hits.totalHits(), equalTo(100l));
        assertThat(hits.hits().length, equalTo(60));
        for (int i = 0; i < 60; i++) {
            SearchHit hit = hits.hits()[i];
//            System.out.println(hit.explanation());
            assertThat("id[" + hit.id() + "]", hit.id(), equalTo(Integer.toString(100 - i - 1)));
        }

        // now try and scroll to the next batch of results
        Map<SearchShardTarget, QuerySearchResultProvider> scrollQueryResults = newHashMap();
        for (QuerySearchResultProvider queryResult : queryResults.values()) {
            scrollQueryResults.put(queryResult.queryResult().shardTarget(), nodeToSearchService.get(queryResult.shardTarget().nodeId()).executeQueryPhase(new InternalScrollSearchRequest(queryResult.id())).queryResult());
        }
        queryResults = scrollQueryResults;

        sortedShardList = searchPhaseController.sortDocs(queryResults.values());
        docIdsToLoad = searchPhaseController.docIdsToLoad(sortedShardList);

        fetchResults = newHashMap();
        for (Map.Entry<SearchShardTarget, ExtTIntArrayList> entry : docIdsToLoad.entrySet()) {
            SearchShardTarget shardTarget = entry.getKey();
            ExtTIntArrayList docIds = entry.getValue();
            FetchSearchResult fetchResult = nodeToSearchService.get(shardTarget.nodeId()).executeFetchPhase(new FetchSearchRequest(queryResults.get(shardTarget).queryResult().id(), docIds));
            fetchResults.put(fetchResult.shardTarget(), fetchResult.initCounter());
        }

        hits = searchPhaseController.merge(sortedShardList, queryResults, fetchResults).hits();
        assertThat(hits.totalHits(), equalTo(100l));
        assertThat(hits.hits().length, equalTo(40));
        for (int i = 0; i < 40; i++) {
            SearchHit hit = hits.hits()[i];
            assertThat("id[" + hit.id() + "]", hit.id(), equalTo(Integer.toString(100 - 60 - 1 - i)));
        }
    }
View Full Code Here


    // find nearby Address objects
    QueryBuilder qb1 = QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
        FilterBuilders.geoDistanceFilter("latlng").point(lat, lng).
        distance(radius, DistanceUnit.KILOMETERS));

    SearchHits hits1 = searchQueryRaw(appid, Utils.type(Address.class), qb1, pager);

    if (hits1 == null) {
      return new ArrayList<P>();
    }

    // then find their parent objects
    String[] ridsarr = new String[(int) hits1.getTotalHits()];
    for (int i = 0; i < hits1.getTotalHits(); i++) {
      Object pid = hits1.getAt(i).getSource().get(Config._PARENTID);
      if (pid != null) {
        ridsarr[i] = pid.toString();
      }
    }

    QueryBuilder qb2 = QueryBuilders.filteredQuery(QueryBuilders.queryString(query),
        FilterBuilders.idsFilter(type).ids(ridsarr));
    SearchHits hits2 = searchQueryRaw(appid, type, qb2, pager);

    return searchQuery(appid, hits2);
  }
View Full Code Here

    }
    if (sort == null) {
      sort = SortBuilders.scoreSort();
    }

    SearchHits hits = null;

    try {
      SearchRequestBuilder srb = client().prepareSearch(stripRouting(appid)).
        setSearchType(SearchType.DFS_QUERY_THEN_FETCH).
        setRouting(getRouting(appid, null)).
        setQuery(query).addSort(sort).setFrom(start).setSize(max);

      if (!StringUtils.isBlank(type)) {
        srb.setTypes(type);
      }

      hits = srb.execute().actionGet().getHits();
      page.setCount(hits.getTotalHits());
    } catch (Exception e) {
      logger.warn(null, e);
    }

    return hits;
View Full Code Here

    return client.prepareSearch(timestampedIndexName)
        .setTypes(DEFAULT_INDEX_TYPE).setQuery(query).execute().actionGet();
  }

  void assertSearch(int expectedHits, SearchResponse response, Event... events) {
    SearchHits hitResponse = response.getHits();
    assertEquals(expectedHits, hitResponse.getTotalHits());

    SearchHit[] hits = hitResponse.getHits();
    Arrays.sort(hits, new Comparator<SearchHit>() {
      @Override
      public int compare(SearchHit o1, SearchHit o2) {
        return o1.getSourceAsString().compareTo(o2.getSourceAsString());
      }
View Full Code Here

            final QueryBuilder queryBuilder, final Integer from,
            final Integer size, final SortBuilder sortBuilder) {
        final List<T> targetList = new ArrayList<T>();
        final SearchResponse response = getSearchResponse(sessionId,
                queryBuilder, from, size, sortBuilder);
        final SearchHits hits = response.getHits();
        if (hits.getTotalHits() != 0) {
            try {
                for (final SearchHit searchHit : hits.getHits()) {
                    targetList.add(Beans
                            .createAndCopy(clazz, searchHit.getSource())
                            .converter(new EsTimestampConverter(),
                                    timestampFields).excludesWhitespace()
                            .execute());
View Full Code Here

                    .setQuery(QueryBuilders.termQuery("url", url))
                    .addSort(
                            SortBuilders.fieldSort(LAST_MODIFIED).order(
                                    SortOrder.DESC)).setFrom(0).setSize(1)
                    .execute().actionGet();
            final SearchHits hits = response.getHits();
            if (hits.getTotalHits() > 0) {
                final SearchHit hit = hits.getHits()[0];
                final Map<String, Object> sourceMap = hit.getSource();
                final Date date = (Date) sourceMap.get(LAST_MODIFIED);
                if (date != null) {
                    urlQueue.setLastModified(new Timestamp(date.getTime()));
                }
View Full Code Here

            final boolean hasData) {
        final SearchResponse response = riverConfig.getClient()
                .prepareSearch(index).setTypes(type)
                .setQuery(QueryBuilders.termQuery(URL, url)).execute()
                .actionGet();
        final SearchHits hits = response.getHits();
        final List<AccessResult> accessResultList = new ArrayList<AccessResult>();
        if (hits.getTotalHits() != 0) {
            for (final SearchHit searchHit : hits.getHits()) {
                accessResultList.add(Beans
                        .createAndCopy(EsAccessResult.class,
                                searchHit.getSource())
                        .converter(new EsTimestampConverter(), timestampFields)
                        .excludesWhitespace().execute());
View Full Code Here

                .setScroll(new TimeValue(scrollTimeout))
                .setPostFilter(FilterBuilders.termFilter(SESSION_ID, sessionId))
                .setQuery(QueryBuilders.matchAllQuery()).setSize(scrollSize)
                .execute().actionGet();
        while (true) {
            final SearchHits searchHits = response.getHits();
            for (final SearchHit searchHit : searchHits) {
                final AccessResult accessResult = Beans
                        .createAndCopy(EsAccessResult.class,
                                searchHit.getSource())
                        .converter(new EsTimestampConverter(), timestampFields)
                        .excludesWhitespace().execute();
                callback.iterate(accessResult);
            }

            if (searchHits.hits().length == 0) {
                break;
            }
            response = riverConfig.getClient()
                    .prepareSearchScroll(response.getScrollId())
                    .setScroll(new TimeValue(scrollTimeout)).execute()
View Full Code Here

        this.rsp = rsp;
        this.keepTimeInMinutes = keepTimeInMinutes;
    }

    @Override public MySearchHits hits() {
        final SearchHits hits = rsp.getHits();
        // uh iterable is strange
        return new MySearchHits() {
            @Override public Iterable<MySearchHit> getHits() {
                return new Iterable<MySearchHit>() {
                    @Override public Iterator<MySearchHit> iterator() {
                        return new Iterator<MySearchHit>() {
                            SearchHit[] arr = hits.hits();
                            int counter = 0;

                            @Override public boolean hasNext() {
                                return counter < arr.length;
                            }

                            @Override public MySearchHit next() {
                                bytes += arr[counter].source().length;
                                MySearchHitES ret = new MySearchHitES(arr[counter]);
                                counter++;
                                return ret;
                            }

                            @Override public void remove() {
                                throw new UnsupportedOperationException("Not supported yet.");
                            }
                        };
                    }
                };
            }

            @Override
            public long totalHits() {
                return hits.totalHits();
            }
        };
    }
View Full Code Here

        if (forceRefresh)
            refresh(intoIndex);
    }

    public List<MyTweet> collectTweets(SearchResponse rsp) {
        SearchHits docs = rsp.getHits();
        List<MyTweet> list = new ArrayList<MyTweet>();
        for (SearchHit sd : docs) {
            list.add(readDoc(sd.getSource(), sd.getId()));
        }
View Full Code Here

TOP

Related Classes of org.elasticsearch.search.SearchHits

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.