Package org.elasticsearch.search

Examples of org.elasticsearch.search.SearchHits


  @Test
  public void shouldMapSearchRequestToPage() {
    //Given
    SearchHit[] hits = {createCarHit("Ford", "Grat"), createCarHit("BMW", "Arrow")};
    SearchHits searchHits = mock(SearchHits.class);
    when(searchHits.totalHits()).thenReturn(2L);
    when(searchHits.iterator()).thenReturn(new ArrayIterator(hits));
    when(response.getHits()).thenReturn(searchHits);

    //When
    FacetedPage<Car> page = resultMapper.mapResults(response, Car.class, null);
View Full Code Here


  @Test
  public void shouldMapPartialSearchRequestToObject() {
    //Given
    SearchHit[] hits = {createCarPartialHit("Ford", "Grat"), createCarPartialHit("BMW", "Arrow")};
    SearchHits searchHits = mock(SearchHits.class);
    when(searchHits.totalHits()).thenReturn(2L);
    when(searchHits.iterator()).thenReturn(new ArrayIterator(hits));
    when(response.getHits()).thenReturn(searchHits);

    //When
    FacetedPage<Car> page = resultMapper.mapResults(response, Car.class, null);
View Full Code Here

   */
  private SolrDocumentList convertToSolrDocumentList(RestRequest request, SearchResponse response) {
    SolrDocumentList results = new SolrDocumentList();

    // get the ES hits
    SearchHits hits = response.getHits();

    // set the result information on the SolrDocumentList
    results.setMaxScore(hits.getMaxScore());
    results.setNumFound(hits.getTotalHits());
    results.setStart(request.paramAsInt("start", 0));

    // loop though the results and convert each
    // one to a SolrDocument
    for (SearchHit hit : hits.getHits()) {
      SolrDocument doc = new SolrDocument();

      // always add score to document
      doc.addField("score", hit.score());

View Full Code Here

    NamedList<Object> highlightResponse = null;

    // if highlighting was requested create the NamedList for the highlights
    if (request.paramAsBoolean("hl", false)) {
      highlightResponse = new SimpleOrderedMap<Object>();
      SearchHits hits = response.getHits();
      // for each hit, get each highlight field and put the list
      // of highlight fragments in a NamedList specific to the hit
      for (SearchHit hit : hits.getHits()) {
        NamedList<Object> docHighlights = new SimpleOrderedMap<Object>();
        Map<String, HighlightField> highlightFields = hit.getHighlightFields();
        for (String fieldName : highlightFields.keySet()) {
          HighlightField highlightField = highlightFields.get(fieldName);
          docHighlights.add(fieldName, highlightField.getFragments());
View Full Code Here

    List<P> list = new ArrayList<P>();
    if (ids == null || ids.isEmpty()) {
      return list;
    }
    try {
      SearchHits hits = null;
      MultiGetResponse response = client().prepareMultiGet().add(appid, null, ids).execute().actionGet();
      for (MultiGetItemResponse multiGetItemResponse : response.getResponses()) {
        GetResponse res = multiGetItemResponse.getResponse();
        if (res.isExists() && !res.isSourceEmpty()) {
          list.add(Utils.setAnnotatedFields(res.getSource()));
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(appid).
        setSearchType(SearchType.DFS_QUERY_THEN_FETCH).
        setQuery(query).addSort(sort).setFrom(start).setSize(max);

      if (type != null) {
        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

    // 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 (type == null) {
      type = "_all";
    }

    SearchHits hits = null;

    try {
      SearchResponse response = client().prepareSearch(appid).
          setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setTypes(type).
          setQuery(query).addSort(sort).setFrom(start).setSize(max).
          execute().actionGet();

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

    return hits;
View Full Code Here

            if (log.isTraceEnabled()) {
                log.trace("elasticsearch query : " + searchRequest);
            }
            SearchResponse searchResponse = searchRequest.execute().actionGet();

            SearchHits searchHits = searchResponse.hits();
            Long hitsNumber = searchHits.totalHits();
            if (hitsNumber == 0) {
                return Collections.emptyList();
            }

            SearchHit[] hits = searchHits.hits();
            List<String> items = new ArrayList<String>(hits.length);
            for (SearchHit hit : hits) {
                items.add(hit.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.