Esempio n. 1
0
  public QProfileRuleResult search(ProfileRuleQuery query, Paging paging) {
    SearchHits ruleHits =
        searchRules(
            query,
            paging,
            ruleFilterForActiveRuleSearch(query)
                .must(hasChildFilter(ESActiveRule.TYPE_ACTIVE_RULE, activeRuleFilter(query))));
    List<Integer> ruleIds = Lists.newArrayList();
    for (SearchHit ruleHit : ruleHits) {
      ruleIds.add(Integer.valueOf(ruleHit.id()));
    }

    List<QProfileRule> result = Lists.newArrayList();
    if (!ruleIds.isEmpty()) {
      SearchHits activeRuleHits = searchActiveRules(query, ruleIds, FIELD_SOURCE, FIELD_PARENT);

      Map<String, SearchHit> activeRuleByParent = Maps.newHashMap();
      for (SearchHit activeRuleHit : activeRuleHits) {
        activeRuleByParent.put(
            (String) activeRuleHit.field(FIELD_PARENT).getValue(), activeRuleHit);
      }

      for (SearchHit ruleHit : ruleHits) {
        result.add(
            new QProfileRule(
                ruleHit.sourceAsMap(), activeRuleByParent.get(ruleHit.id()).sourceAsMap()));
      }
    }
    return new QProfileRuleResult(
        result,
        PagingResult.create(paging.pageSize(), paging.pageIndex(), ruleHits.getTotalHits()));
  }
Esempio n. 2
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;
      }
    }
  }
  // Relates to #10397
  public void testUpdateScripts() {
    createIndex("test_index");
    ensureGreen("test_index");
    client().prepareIndex("test_index", "test_type", "1").setSource("{\"foo\":\"bar\"}").get();
    flush("test_index");

    int iterations = randomIntBetween(2, 11);
    for (int i = 1; i < iterations; i++) {
      assertAcked(
          client()
              .admin()
              .cluster()
              .preparePutStoredScript()
              .setScriptLang(GroovyScriptEngineService.NAME)
              .setId("script1")
              .setSource(new BytesArray("{\"script\":\"" + i + "\"}")));
      SearchResponse searchResponse =
          client()
              .prepareSearch()
              .setSource(
                  new SearchSourceBuilder()
                      .query(QueryBuilders.matchAllQuery())
                      .scriptField(
                          "test_field", new Script("script1", ScriptType.STORED, "groovy", null)))
              .setIndices("test_index")
              .setTypes("test_type")
              .get();
      assertHitCount(searchResponse, 1);
      SearchHit sh = searchResponse.getHits().getAt(0);
      assertThat(sh.field("test_field").getValue(), equalTo(i));
    }
  }
  public void testFieldIndexedScript() throws ExecutionException, InterruptedException {
    client()
        .admin()
        .cluster()
        .preparePutStoredScript()
        .setId("script1")
        .setScriptLang("groovy")
        .setSource(new BytesArray("{ \"script\" : \"2\"}"))
        .get();
    client()
        .admin()
        .cluster()
        .preparePutStoredScript()
        .setId("script2")
        .setScriptLang("groovy")
        .setSource(new BytesArray("{ \"script\" : \"factor * 2\"}"))
        .get();

    List<IndexRequestBuilder> builders = new ArrayList<>();
    builders.add(
        client().prepareIndex("test", "scriptTest", "1").setSource("{\"theField\":\"foo\"}"));
    builders.add(
        client().prepareIndex("test", "scriptTest", "2").setSource("{\"theField\":\"foo 2\"}"));
    builders.add(
        client().prepareIndex("test", "scriptTest", "3").setSource("{\"theField\":\"foo 3\"}"));
    builders.add(
        client().prepareIndex("test", "scriptTest", "4").setSource("{\"theField\":\"foo 4\"}"));
    builders.add(
        client().prepareIndex("test", "scriptTest", "5").setSource("{\"theField\":\"bar\"}"));

    indexRandom(true, builders);
    Map<String, Object> script2Params = new HashMap<>();
    script2Params.put("factor", 3);
    SearchResponse searchResponse =
        client()
            .prepareSearch()
            .setSource(
                new SearchSourceBuilder()
                    .query(QueryBuilders.matchAllQuery())
                    .size(1)
                    .scriptField("test1", new Script("script1", ScriptType.STORED, "groovy", null))
                    .scriptField(
                        "test2", new Script("script2", ScriptType.STORED, "groovy", script2Params)))
            .setIndices("test")
            .setTypes("scriptTest")
            .get();
    assertHitCount(searchResponse, 5);
    assertTrue(searchResponse.getHits().hits().length == 1);
    SearchHit sh = searchResponse.getHits().getAt(0);
    assertThat(sh.field("test1").getValue(), equalTo(2));
    assertThat(sh.field("test2").getValue(), equalTo(6));
  }
 private <T> T fieldValue(String fieldName) {
   SearchHitField field = delegate.field(fieldName);
   return field == null ? null : field.value();
 }