/**
   * Remove the documents from ElasticSearch that are not present in uris
   *
   * @param uris uris that should be present in the index.
   * @return true if the action completed, false if it failed during the process.
   */
  private int removeMissingUris(Set<String> uris) {
    int searchKeepAlive = 60000;
    int count = 0;

    SearchResponse response =
        client
            .prepareSearch()
            .setIndices(indexName)
            .setTypes(typeName)
            .setScroll(new TimeValue(searchKeepAlive))
            .setQuery(QueryBuilders.matchAllQuery())
            .execute()
            .actionGet();

    while (response.getHits().getHits().length > 0) {
      for (SearchHit hit : response.getHits()) {
        if (uris.contains(hit.getId())) continue;

        DeleteResponse deleteResponse =
            client.prepareDelete(indexName, typeName, hit.getId()).execute().actionGet();
        if (deleteResponse.isFound()) count++;
      }

      response =
          client
              .prepareSearchScroll(response.getScrollId())
              .setScroll(new TimeValue(searchKeepAlive))
              .execute()
              .actionGet();
    }
    return count;
  }
예제 #2
0
 public final ImmutableList<ModelEntryT> getModels(
     final Logger logger,
     final Marker logMarker,
     final ModelFactory<ModelEntryT> modelFactory,
     final SearchRequestBuilder requestBuilder)
     throws IoExceptionT {
   checkNotNull(requestBuilder);
   try {
     logger.debug(logMarker, "searching index {} with {}", indexName, requestBuilder);
     final SearchResponse response = requestBuilder.execute().actionGet();
     final ImmutableList.Builder<ModelEntryT> modelsBuilder = ImmutableList.builder();
     for (final SearchHit searchHit : response.getHits()) {
       try {
         modelsBuilder.add(
             modelFactory.createModelEntry(searchHit.getId(), searchHit.getSourceRef()));
       } catch (final InvalidModelException e) {
         logger.error(logMarker, "invalid model {} from index, ignoring: ", searchHit.getId(), e);
         continue;
       }
     }
     return modelsBuilder.build();
   } catch (final IndexNotFoundException e) {
     throw e;
   } catch (final ElasticsearchException e) {
     logger.error(
         logMarker, "error searching models on index {} with {}: ", indexName, requestBuilder, e);
     throw exceptionFactory.newIoException(
         e,
         String.format("error searching models on index %s with '%s'", indexName, requestBuilder));
   }
 }
예제 #3
0
 private void checkValueInEachDocWithFunctionScore(
     String fieldScript,
     Map<String, Object> expectedFieldVals,
     String scoreScript,
     Map<String, Object> expectedScore,
     int numExpectedDocs) {
   SearchResponse sr =
       client()
           .prepareSearch("test")
           .setQuery(
               QueryBuilders.functionScoreQuery(ScoreFunctionBuilders.scriptFunction(scoreScript)))
           .addScriptField("tvtest", fieldScript)
           .execute()
           .actionGet();
   assertHitCount(sr, numExpectedDocs);
   for (SearchHit hit : sr.getHits().getHits()) {
     Object result = hit.getFields().get("tvtest").getValues().get(0);
     Object expectedResult = expectedFieldVals.get(hit.getId());
     assertThat("for doc " + hit.getId(), result, equalTo(expectedResult));
     assertThat(
         "for doc " + hit.getId(),
         ((Float) expectedScore.get(hit.getId())).doubleValue(),
         Matchers.closeTo(hit.score(), 1.e-4));
   }
 }
예제 #4
0
  void assertAllSearchWorks(String indexName) {
    logger.info("--> testing _all search");
    SearchResponse searchRsp = client().prepareSearch(indexName).get();
    ElasticsearchAssertions.assertNoFailures(searchRsp);
    assertThat(searchRsp.getHits().getTotalHits(), greaterThanOrEqualTo(1L));
    SearchHit bestHit = searchRsp.getHits().getAt(0);

    // Make sure there are payloads and they are taken into account for the score
    // the 'string' field has a boost of 4 in the mappings so it should get a payload boost
    String stringValue = (String) bestHit.sourceAsMap().get("string");
    assertNotNull(stringValue);
    Explanation explanation =
        client()
            .prepareExplain(indexName, bestHit.getType(), bestHit.getId())
            .setQuery(QueryBuilders.matchQuery("_all", stringValue))
            .get()
            .getExplanation();
    assertTrue(
        "Could not find payload boost in explanation\n" + explanation,
        findPayloadBoostInExplanation(explanation));

    // Make sure the query can run on the whole index
    searchRsp =
        client()
            .prepareSearch(indexName)
            .setQuery(QueryBuilders.matchQuery("_all", stringValue))
            .setExplain(true)
            .get();
    ElasticsearchAssertions.assertNoFailures(searchRsp);
    assertThat(searchRsp.getHits().getTotalHits(), greaterThanOrEqualTo(1L));
  }
  void assertRealtimeGetWorks(String indexName) {
    assertAcked(
        client()
            .admin()
            .indices()
            .prepareUpdateSettings(indexName)
            .setSettings(Settings.builder().put("refresh_interval", -1).build()));
    SearchRequestBuilder searchReq =
        client().prepareSearch(indexName).setQuery(QueryBuilders.matchAllQuery());
    SearchHit hit = searchReq.get().getHits().getAt(0);
    String docId = hit.getId();
    // foo is new, it is not a field in the generated index
    client().prepareUpdate(indexName, "doc", docId).setDoc("foo", "bar").get();
    GetResponse getRsp = client().prepareGet(indexName, "doc", docId).get();
    Map<String, Object> source = getRsp.getSourceAsMap();
    assertThat(source, Matchers.hasKey("foo"));

    assertAcked(
        client()
            .admin()
            .indices()
            .prepareUpdateSettings(indexName)
            .setSettings(
                Settings.builder()
                    .put("refresh_interval", EngineConfig.DEFAULT_REFRESH_INTERVAL)
                    .build()));
  }
예제 #6
0
 private void checkValueInEachDoc(
     String script, Map<String, Object> expectedResults, int numExpectedDocs) {
   SearchResponse sr =
       client()
           .prepareSearch("test")
           .setQuery(QueryBuilders.matchAllQuery())
           .addScriptField("tvtest", script)
           .execute()
           .actionGet();
   assertHitCount(sr, numExpectedDocs);
   for (SearchHit hit : sr.getHits().getHits()) {
     Object result = hit.getFields().get("tvtest").getValues().get(0);
     Object expectedResult = expectedResults.get(hit.getId());
     assertThat("for doc " + hit.getId(), result, equalTo(expectedResult));
   }
 }
  @Override
  public <T> FacetedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
    long totalHits = response.getHits().totalHits();
    List<T> results = new ArrayList<T>();
    for (SearchHit hit : response.getHits()) {
      if (hit != null) {
        T result = null;
        if (!Strings.isNullOrEmpty(hit.sourceAsString())) {
          result = mapEntity(hit.sourceAsString(), clazz);
        } else {
          result = mapEntity(hit.getFields().values(), clazz);
        }
        setPersistentEntityId(result, hit.getId(), clazz);
        results.add(result);
      }
    }
    List<FacetResult> facets = new ArrayList<FacetResult>();
    if (response.getFacets() != null) {
      for (Facet facet : response.getFacets()) {
        FacetResult facetResult = DefaultFacetMapper.parse(facet);
        if (facetResult != null) {
          facets.add(facetResult);
        }
      }
    }

    return new FacetedPageImpl<T>(results, pageable, totalHits, facets);
  }
예제 #8
0
 @Override
 public Set<String> loadAllKeys() {
   logger.info("Load all keys called");
   SearchResponse response =
       elasticsearchConnection
           .getClient()
           .prepareSearch(TABLE_META_INDEX)
           .setTypes(TABLE_META_TYPE)
           .setQuery(QueryBuilders.matchAllQuery())
           .setSearchType(SearchType.SCAN)
           .setScroll(new TimeValue(30, TimeUnit.SECONDS))
           .setNoFields()
           .execute()
           .actionGet();
   Set<String> ids = Sets.newHashSet();
   while (true) {
     response =
         elasticsearchConnection
             .getClient()
             .prepareSearchScroll(response.getScrollId())
             .setScroll(new TimeValue(60000))
             .execute()
             .actionGet();
     SearchHits hits = response.getHits();
     for (SearchHit hit : hits) {
       ids.add(hit.getId());
     }
     if (0 == response.getHits().hits().length) {
       break;
     }
   }
   logger.info("Loaded value count: " + ids.size());
   return ids;
 }
예제 #9
0
  public void addDeletion(SearchRequestBuilder searchRequest) {
    searchRequest
        .addSort("_doc", SortOrder.ASC)
        .setScroll(TimeValue.timeValueMinutes(5))
        .setSize(100)
        // load only doc ids, not _source fields
        .setFetchSource(false);

    // this search is synchronous. An optimization would be to be non-blocking,
    // but it requires to tracking pending requests in close().
    // Same semaphore can't be reused because of potential deadlock (requires to acquire
    // two locks)
    SearchResponse searchResponse = searchRequest.get();

    while (true) {
      SearchHit[] hits = searchResponse.getHits().getHits();
      for (SearchHit hit : hits) {
        DeleteRequestBuilder deleteRequestBuilder =
            client.prepareDelete(hit.index(), hit.type(), hit.getId());
        SearchHitField routing = hit.field("_routing");
        if (routing != null) {
          deleteRequestBuilder.setRouting(routing.getValue());
        }
        add(deleteRequestBuilder.request());
      }

      String scrollId = searchResponse.getScrollId();
      searchResponse =
          client.prepareSearchScroll(scrollId).setScroll(TimeValue.timeValueMinutes(5)).get();
      if (hits.length == 0) {
        client.nativeClient().prepareClearScroll().addScrollId(scrollId).get();
        break;
      }
    }
  }
예제 #10
0
 private void checkOnlyFunctionScore(
     String scoreScript, Map<String, Object> expectedScore, int numExpectedDocs) {
   SearchResponse sr =
       client()
           .prepareSearch("test")
           .setQuery(
               QueryBuilders.functionScoreQuery(ScoreFunctionBuilders.scriptFunction(scoreScript)))
           .execute()
           .actionGet();
   assertHitCount(sr, numExpectedDocs);
   for (SearchHit hit : sr.getHits().getHits()) {
     assertThat(
         "for doc " + hit.getId(),
         ((Float) expectedScore.get(hit.getId())).doubleValue(),
         Matchers.closeTo(hit.score(), 1.e-4));
   }
 }
 private List<String> extractIds(SearchResponse response) {
   List<String> ids = new ArrayList<String>();
   for (SearchHit hit : response.getHits()) {
     if (hit != null) {
       ids.add(hit.getId());
     }
   }
   return ids;
 }
예제 #12
0
  public static List<Map<String, Object>> doSearchResultEs(String value, String fq) {

    System.out.println("fq: " + fq);
    SearchHit[] shs = getFromEs(value, fq);

    Map<String, Object> itemContent;
    List<Map<String, Object>> rcdStr = new LinkedList<Map<String, Object>>();
    //       id:3341326TAX_CODE
    //       score:2.1178803
    //       source:
    //       {
    //       "NUMBER_OF_CLICKS":0,
    //       "KEY":"TAX_CODE",
    //       "VALUE":"Tax Code",
    //
    // "FILE_PATH":"ipm/src/imaging-ui/src/oracle/imaging/axf/resources/user/ApplicationStrings.java",
    //       "FID":41326,
    //       "LAST_MODIFIED":"2012-03-08T21:38:33.627-08:00",
    //       "FTID":263,
    //       "CN":"IPM",
    //       "FE":".java",
    //       "DID":3,
    //       "DN":"drop30",
    //       "RID":3,
    //       "RN":"11.1.1.6",
    //       "PID":3,
    //       "PN":"ias",
    //       "BID":97
    //       }

    // {"NUMBER_OF_CLICKS":0,"KEY":"TAX_CODE","VALUE":"Tax
    // Code","FILE_PATH":"ipm/src/imaging-ui/src/oracle/imaging/axf/resources/user/ApplicationStrings.java","FID":41326,"LAST_MODIFIED":"2012-03-08T21:38:33.627-08:00","FTID":263,"CN":"IPM","FE":".java","DID":3,"DN":"drop30","RID":3,"RN":"11.1.1.6","PID":3,"PN":"ias","BID":97}

    String resource = null;

    for (SearchHit hit : shs) {
      itemContent = new HashMap<String, Object>();
      itemContent.put("id", hit.getId());
      itemContent.put("score", hit.getScore());
      for (Map.Entry<String, Object> entry : hit.getSource().entrySet()) {
        // System.out.println("key: "+entry.getKey() + ":" + entry.getValue());
        if (entry.getKey().equals("FILE_PATH")) itemContent.put("fP", entry.getValue());
        else if (entry.getKey().equals("VALUE")) itemContent.put("vL", entry.getValue());
        else if (entry.getKey().equals("DN")) itemContent.put("dN", entry.getValue());
        else if (entry.getKey().equals("RN")) itemContent.put("rN", entry.getValue());
        else if (entry.getKey().equals("PN")) itemContent.put("pN", entry.getValue());
        else if (entry.getKey().equals("CN")) itemContent.put("sP", entry.getValue());
        else if (entry.getKey().equals("BID")) itemContent.put("bid", entry.getValue());
        else if (entry.getKey().equals("KEY")) itemContent.put("key", entry.getValue());
        else if (entry.getKey().equals("FID")) itemContent.put("fid", entry.getValue());
      }
      if (itemContent != null) {
        rcdStr.add(itemContent);
      }
    }
    return rcdStr;
  }
예제 #13
0
  @Override
  public List<T> collectObjects(SearchResponse rsp) {
    SearchHits docs = rsp.getHits();
    List<T> list = new ArrayList<T>(docs.hits().length);
    for (SearchHit sd : docs) {
      if (sd.getExplanation() != null) {
        String res = "";
        for (Explanation str : sd.getExplanation().getDetails()) {
          res += str.toString();
        }
        logger.info(sd.getId() + " " + res);
      }
      T o = readDoc(sd.getId(), sd.getVersion(), sd.getSource());
      if (o != null) list.add(o);
    }

    return list;
  }
예제 #14
0
 public static void removePendingRatings(String userId, ElasticSearchContextListener es) {
   // definitely not  more than 2, but to be safe, take 100...
   SearchResponse response =
       es.searchrequest(INDEX_NAME, QueryBuilders.matchQuery(ES_STATUS, ES_STATUS_PENDING), 0, 100)
           .actionGet();
   for (SearchHit hit : response.getHits()) {
     es.deleteRequest(INDEX_NAME, hit.getId()).actionGet();
   }
 }
예제 #15
0
  private <T> T createSavedObjectWithHit(SearchHit hit, Query<T> query) {
    T object = createSavedObject(hit.getType(), hit.getId(), query);
    State objectState = State.getInstance(object);

    if (!objectState.isReferenceOnly()) {
      objectState.setValues(hit.getSource());
    }

    return swapObjectType(query, object);
  }
예제 #16
0
 private void checkArrayValsInEachDoc(
     String script, HashMap<String, List<Object>> expectedArray, int expectedHitSize) {
   SearchResponse sr =
       client()
           .prepareSearch("test")
           .setQuery(QueryBuilders.matchAllQuery())
           .addScriptField("tvtest", script)
           .execute()
           .actionGet();
   assertHitCount(sr, expectedHitSize);
   int nullCounter = 0;
   for (SearchHit hit : sr.getHits().getHits()) {
     Object result = hit.getFields().get("tvtest").getValues();
     Object expectedResult = expectedArray.get(hit.getId());
     assertThat("for doc " + hit.getId(), result, equalTo(expectedResult));
     if (expectedResult != null) {
       nullCounter++;
     }
   }
   assertThat(nullCounter, equalTo(expectedArray.size()));
 }
예제 #17
0
  @Test
  public void testChildrenAggs() throws Exception {
    SearchResponse searchResponse =
        client()
            .prepareSearch("test")
            .setQuery(matchQuery("randomized", true))
            .addAggregation(
                terms("category")
                    .field("category")
                    .size(0)
                    .subAggregation(
                        children("to_comment")
                            .childType("comment")
                            .subAggregation(
                                terms("commenters")
                                    .field("commenter")
                                    .size(0)
                                    .subAggregation(topHits("top_comments")))))
            .get();
    assertSearchResponse(searchResponse);

    Terms categoryTerms = searchResponse.getAggregations().get("category");
    assertThat(categoryTerms.getBuckets().size(), equalTo(categoryToControl.size()));
    for (Map.Entry<String, Control> entry1 : categoryToControl.entrySet()) {
      Terms.Bucket categoryBucket = categoryTerms.getBucketByKey(entry1.getKey());
      assertThat(categoryBucket.getKey(), equalTo(entry1.getKey()));
      assertThat(categoryBucket.getDocCount(), equalTo((long) entry1.getValue().articleIds.size()));

      Children childrenBucket = categoryBucket.getAggregations().get("to_comment");
      assertThat(childrenBucket.getName(), equalTo("to_comment"));
      assertThat(childrenBucket.getDocCount(), equalTo((long) entry1.getValue().commentIds.size()));
      assertThat(
          (long) childrenBucket.getProperty("_count"),
          equalTo((long) entry1.getValue().commentIds.size()));

      Terms commentersTerms = childrenBucket.getAggregations().get("commenters");
      assertThat((Terms) childrenBucket.getProperty("commenters"), sameInstance(commentersTerms));
      assertThat(
          commentersTerms.getBuckets().size(),
          equalTo(entry1.getValue().commenterToCommentId.size()));
      for (Map.Entry<String, Set<String>> entry2 :
          entry1.getValue().commenterToCommentId.entrySet()) {
        Terms.Bucket commentBucket = commentersTerms.getBucketByKey(entry2.getKey());
        assertThat(commentBucket.getKey(), equalTo(entry2.getKey()));
        assertThat(commentBucket.getDocCount(), equalTo((long) entry2.getValue().size()));

        TopHits topHits = commentBucket.getAggregations().get("top_comments");
        for (SearchHit searchHit : topHits.getHits().getHits()) {
          assertThat(entry2.getValue().contains(searchHit.getId()), is(true));
        }
      }
    }
  }
예제 #18
0
  public static SearchHit[] getFromEs(String value, String fq) {
    // create a node client
    Settings settings =
        ImmutableSettings.settingsBuilder().put("cluster.name", "esgoodluck").build();
    Client client =
        new TransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress("slc08ynb.us.oracle.com", 9300));

    String filterTemplate = "   \"filter\": {" + "\"bool\": {" + fq + "} }";

    String queryStr =
        "{\"query\": {\"filtered\":{\"query\":{\"multi_match\":{\"query\":\""
            + value
            + "\", \"type\":\"most_fields\",\"fields\":[\"VALUE\",\"VALUE.UNPROCESSED^10\",\"VALUE.VALUE_ST_STEM^5\"]}},"
            + filterTemplate
            + "}},\"sort\":[\"_score\",{\"_script\":{\"script\":\"doc['VALUE.ORIGINAL'].value.length()\",\"type\":\"number\"}}]}";
    SearchResponse response =
        client
            .prepareSearch("latest")
            .setTypes("latest")
            .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
            .setSource(queryStr)
            .execute()
            .actionGet();
    System.out.println("query string:" + queryStr);

    for (SearchHit hit : response.getHits().getHits()) {
      System.out.println(hit.getId());
      System.out.println(hit.getScore());
      System.out.println(hit.getSourceAsString());
      for (Map.Entry<String, SearchHitField> entry : hit.getFields().entrySet()) {
        System.out.println(entry.getKey() + ": " + entry.getValue());
      }
    }

    return response.getHits().getHits();
  }
 private void indexSearchHit(
     BulkNodeClient bulkNodeClient, KnapsackPullRequest request, SearchHit hit)
     throws IOException {
   IndexRequest indexRequest =
       new IndexRequest(
           mapIndex(request, hit.getIndex()),
           mapType(request, hit.getIndex(), hit.getType()),
           hit.getId());
   for (String f : hit.getFields().keySet()) {
     switch (f) {
       case "_parent":
         indexRequest.parent(hit.getFields().get(f).getValue().toString());
         break;
       case "_routing":
         indexRequest.routing(hit.getFields().get(f).getValue().toString());
         break;
       case "_timestamp":
         indexRequest.timestamp(hit.getFields().get(f).getValue().toString());
         break;
       case "_version":
         indexRequest
             .versionType(VersionType.EXTERNAL)
             .version(Long.parseLong(hit.getFields().get(f).getValue().toString()));
         break;
       case "_source":
         indexRequest.source(hit.getSourceAsString());
         break;
       default:
         indexRequest.source(f, hit.getFields().get(f).getValue().toString());
         break;
     }
   }
   if (!hit.getFields().keySet().contains("_source")) {
     indexRequest.source(hit.getSourceAsString());
   }
   bulkNodeClient.bulkIndex(indexRequest);
 }
예제 #20
0
 public final long deleteModels(
     final Logger logger, final Marker logMarker, final QueryBuilder query) throws IoExceptionT {
   logger.debug(logMarker, "deleting models from index {} with query {}", indexName, query);
   try {
     final int bulkRequestSize = 1000;
     long count = 0;
     final TimeValue scrollTimeout = new TimeValue(5 * 60 * 1000);
     SearchResponse searchResponse =
         client
             .prepareSearch(indexName)
             .setQuery(query)
             .setScroll(scrollTimeout)
             .setSize(bulkRequestSize)
             .execute()
             .actionGet();
     while (searchResponse.getHits().getHits().length > 0) {
       final BulkRequestBuilder bulkRequest = client.prepareBulk();
       for (final SearchHit hit : searchResponse.getHits()) {
         bulkRequest.add(client.prepareDelete(indexName, documentType, hit.getId()));
       }
       bulkRequest.execute().actionGet();
       count += searchResponse.getHits().getHits().length;
       searchResponse =
           client
               .prepareSearchScroll(searchResponse.getScrollId())
               .setScroll(scrollTimeout)
               .execute()
               .actionGet();
     }
     return count;
   } catch (final IndexNotFoundException e) {
     logger.warn(logMarker, "tried to delete models from missing index {}", indexName);
     return 0l;
   } catch (final ElasticsearchException e) {
     throw exceptionFactory.newIoException(e, "error deleting models from index " + indexName);
   }
 }
  @Override
  public Page<Article> queryPage(Page<Article> page, String queryString) {
    Assert.notNull(page, "page must not null");
    Assert.hasText(queryString, "queryString must not be empty!");
    SearchResponse searchResponse =
        transportClient
            .prepareSearch(INDICE)
            .setTypes(TYPE)
            .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
            .setQuery(QueryBuilders.queryString(queryString))
            .setFrom(page.getFirst() - 1)
            .setSize(page.getPageSize())
            .setExplain(true)
            .execute()
            .actionGet();

    SearchHits hits = searchResponse.getHits();
    long total = hits.getTotalHits();
    logger.debug("search articles result total:{}", total);
    List<Article> list = new ArrayList<Article>();
    Article article = null;
    for (SearchHit hit : hits) {
      long id = Long.parseLong(hit.getId());
      String title = (String) hit.getSource().get(TITLE);
      String summary = (String) hit.getSource().get(SUMMARY);
      logger.debug("id:{},title:{},summary:{}", id, title, summary);

      article = new Article();
      article.setId(id);
      article.setTitle(title);
      article.setSummary(summary);
      list.add(article);
    }
    page.setTotalCount(total);
    page.setResult(list);
    return page;
  }
 public List<T> list() {
   List<T> results = new ArrayList<>(0);
   try {
     SearchResponse response = client.prepareSearch(index).setTypes(entity).execute().actionGet();
     SearchHits hits = response.getHits();
     results = new ArrayList<>(hits.getHits().length);
     for (SearchHit hit : hits) {
       String source = hit.getSourceAsString();
       if (source == null) {
         continue;
       }
       try {
         T result = mapper.readValue(source, clazz);
         result.setId(hit.getId());
         results.add(result);
       } catch (IOException e) {
         logger.error("Error listing entities of type " + this.entity, e);
       }
     }
   } catch (IndexMissingException e) {
     logger.error("Error listing entities of type " + this.entity, e);
   }
   return results;
 }
예제 #23
0
 @Override
 public void describeMismatchSafely(
     final SearchHit searchHit, final Description mismatchDescription) {
   mismatchDescription.appendText(" was ").appendValue(searchHit.getId());
 }
예제 #24
0
 @Override
 protected boolean matchesSafely(SearchHit searchHit) {
   return searchHit.getId().equals(id);
 }
 @Override
 public String getId() {
   return delegate.getId();
 }
예제 #26
0
  @Test
  public void testParentWithMultipleBuckets() throws Exception {
    SearchResponse searchResponse =
        client()
            .prepareSearch("test")
            .setQuery(matchQuery("randomized", false))
            .addAggregation(
                terms("category")
                    .field("category")
                    .size(0)
                    .subAggregation(
                        children("to_comment")
                            .childType("comment")
                            .subAggregation(topHits("top_comments").addSort("_id", SortOrder.ASC))))
            .get();
    assertSearchResponse(searchResponse);

    Terms categoryTerms = searchResponse.getAggregations().get("category");
    assertThat(categoryTerms.getBuckets().size(), equalTo(3));

    for (Terms.Bucket bucket : categoryTerms.getBuckets()) {
      logger.info("bucket=" + bucket.getKey());
      Children childrenBucket = bucket.getAggregations().get("to_comment");
      TopHits topHits = childrenBucket.getAggregations().get("top_comments");
      logger.info("total_hits={}", topHits.getHits().getTotalHits());
      for (SearchHit searchHit : topHits.getHits()) {
        logger.info(
            "hit= {} {} {}", searchHit.sortValues()[0], searchHit.getType(), searchHit.getId());
      }
    }

    Terms.Bucket categoryBucket = categoryTerms.getBucketByKey("a");
    assertThat(categoryBucket.getKey(), equalTo("a"));
    assertThat(categoryBucket.getDocCount(), equalTo(3l));

    Children childrenBucket = categoryBucket.getAggregations().get("to_comment");
    assertThat(childrenBucket.getName(), equalTo("to_comment"));
    assertThat(childrenBucket.getDocCount(), equalTo(2l));
    TopHits topHits = childrenBucket.getAggregations().get("top_comments");
    assertThat(topHits.getHits().totalHits(), equalTo(2l));
    assertThat(topHits.getHits().getAt(0).sortValues()[0].toString(), equalTo("a"));
    assertThat(topHits.getHits().getAt(0).getId(), equalTo("a"));
    assertThat(topHits.getHits().getAt(0).getType(), equalTo("comment"));
    assertThat(topHits.getHits().getAt(1).sortValues()[0].toString(), equalTo("c"));
    assertThat(topHits.getHits().getAt(1).getId(), equalTo("c"));
    assertThat(topHits.getHits().getAt(1).getType(), equalTo("comment"));

    categoryBucket = categoryTerms.getBucketByKey("b");
    assertThat(categoryBucket.getKey(), equalTo("b"));
    assertThat(categoryBucket.getDocCount(), equalTo(2l));

    childrenBucket = categoryBucket.getAggregations().get("to_comment");
    assertThat(childrenBucket.getName(), equalTo("to_comment"));
    assertThat(childrenBucket.getDocCount(), equalTo(1l));
    topHits = childrenBucket.getAggregations().get("top_comments");
    assertThat(topHits.getHits().totalHits(), equalTo(1l));
    assertThat(topHits.getHits().getAt(0).getId(), equalTo("c"));
    assertThat(topHits.getHits().getAt(0).getType(), equalTo("comment"));

    categoryBucket = categoryTerms.getBucketByKey("c");
    assertThat(categoryBucket.getKey(), equalTo("c"));
    assertThat(categoryBucket.getDocCount(), equalTo(2l));

    childrenBucket = categoryBucket.getAggregations().get("to_comment");
    assertThat(childrenBucket.getName(), equalTo("to_comment"));
    assertThat(childrenBucket.getDocCount(), equalTo(1l));
    topHits = childrenBucket.getAggregations().get("top_comments");
    assertThat(topHits.getHits().totalHits(), equalTo(1l));
    assertThat(topHits.getHits().getAt(0).getId(), equalTo("c"));
    assertThat(topHits.getHits().getAt(0).getType(), equalTo("comment"));
  }
예제 #27
0
 /* (non-Javadoc)
  * @see org.nojdbc.Cursor#next()
  */
 @Override
 public Document next() {
   SearchHit h = hits[i];
   i++;
   return new ElasticSearchDocument(h.getId(), h.getFields());
 }
예제 #28
0
 public static CandidateResult parseIndexDocId(final SearchHit hit) {
   return parseIndexDocId(hit.getId());
 }
예제 #29
0
  public static CandidateResult parseIndexDocId(final SearchHit hit, boolean isGeo) {

    final String documentId = hit.getId();
    final double distance = isGeo ? (double) hit.sortValues()[0] : -1;
    return parseIndexDocId(documentId, distance);
  }