@Test
  public void createIndexWarmer() {
    client().admin().indices().prepareDelete().execute().actionGet();

    client()
        .admin()
        .indices()
        .prepareCreate("test")
        .setSource(
            "{\n"
                + "    \"settings\" : {\n"
                + "        \"index.number_of_shards\" : 1\n"
                + "    },\n"
                + "    \"warmers\" : {\n"
                + "        \"warmer_1\" : {\n"
                + "            \"types\" : [],\n"
                + "            \"source\" : {\n"
                + "                \"query\" : {\n"
                + "                    \"match_all\" : {}\n"
                + "                }\n"
                + "            }\n"
                + "        }\n"
                + "    }\n"
                + "}")
        .execute()
        .actionGet();

    ClusterState clusterState =
        client().admin().cluster().prepareState().execute().actionGet().getState();
    IndexWarmersMetaData warmersMetaData =
        clusterState.metaData().index("test").custom(IndexWarmersMetaData.TYPE);
    assertThat(warmersMetaData, Matchers.notNullValue());
    assertThat(warmersMetaData.entries().size(), equalTo(1));

    client()
        .prepareIndex("test", "type1", "1")
        .setSource("field", "value1")
        .setRefresh(true)
        .execute()
        .actionGet();
    client()
        .prepareIndex("test", "type1", "2")
        .setSource("field", "value2")
        .setRefresh(true)
        .execute()
        .actionGet();
  }
Exemplo n.º 2
0
    public TerminationHandle internalWarm(
        final IndexShard indexShard,
        final IndexMetaData indexMetaData,
        final IndicesWarmer.WarmerContext warmerContext,
        ThreadPool threadPool,
        final boolean top) {
      IndexWarmersMetaData custom = indexMetaData.custom(IndexWarmersMetaData.TYPE);
      if (custom == null) {
        return TerminationHandle.NO_WAIT;
      }
      final Executor executor = threadPool.executor(executor());
      final CountDownLatch latch = new CountDownLatch(custom.entries().size());
      for (final IndexWarmersMetaData.Entry entry : custom.entries()) {
        executor.execute(
            new Runnable() {

              @Override
              public void run() {
                SearchContext context = null;
                try {
                  long now = System.nanoTime();
                  ShardSearchRequest request =
                      new ShardSearchLocalRequest(
                          indexShard.shardId(),
                          indexMetaData.numberOfShards(),
                          SearchType.QUERY_THEN_FETCH,
                          entry.source(),
                          entry.types(),
                          entry.queryCache());
                  context = createContext(request, warmerContext.searcher());
                  // if we use sort, we need to do query to sort on it and load relevant field data
                  // if not, we might as well set size=0 (and cache if needed)
                  if (context.sort() == null) {
                    context.size(0);
                  }
                  boolean canCache = indicesQueryCache.canCache(request, context);
                  // early terminate when we can cache, since we can only do proper caching on top
                  // level searcher
                  // also, if we can't cache, and its top, we don't need to execute it, since we
                  // already did when its not top
                  if (canCache != top) {
                    return;
                  }
                  loadOrExecuteQueryPhase(request, context, queryPhase);
                  long took = System.nanoTime() - now;
                  if (indexShard.warmerService().logger().isTraceEnabled()) {
                    indexShard
                        .warmerService()
                        .logger()
                        .trace(
                            "warmed [{}], took [{}]", entry.name(), TimeValue.timeValueNanos(took));
                  }
                } catch (Throwable t) {
                  indexShard.warmerService().logger().warn("warmer [{}] failed", t, entry.name());
                } finally {
                  try {
                    if (context != null) {
                      freeContext(context.id());
                      cleanContext(context);
                    }
                  } finally {
                    latch.countDown();
                  }
                }
              }
            });
      }
      return new TerminationHandle() {
        @Override
        public void awaitTermination() throws InterruptedException {
          latch.await();
        }
      };
    }
  @Test
  public void templateWarmer() {
    client().admin().indices().prepareDelete().execute().actionGet();

    client()
        .admin()
        .indices()
        .preparePutTemplate("template_1")
        .setSource(
            "{\n"
                + "    \"template\" : \"*\",\n"
                + "    \"warmers\" : {\n"
                + "        \"warmer_1\" : {\n"
                + "            \"types\" : [],\n"
                + "            \"source\" : {\n"
                + "                \"query\" : {\n"
                + "                    \"match_all\" : {}\n"
                + "                }\n"
                + "            }\n"
                + "        }\n"
                + "    }\n"
                + "}")
        .execute()
        .actionGet();

    client()
        .admin()
        .indices()
        .prepareCreate("test")
        .setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1))
        .execute()
        .actionGet();

    client()
        .admin()
        .cluster()
        .prepareHealth()
        .setWaitForEvents(Priority.LANGUID)
        .setWaitForGreenStatus()
        .execute()
        .actionGet();

    ClusterState clusterState =
        client().admin().cluster().prepareState().execute().actionGet().getState();
    IndexWarmersMetaData warmersMetaData =
        clusterState.metaData().index("test").custom(IndexWarmersMetaData.TYPE);
    assertThat(warmersMetaData, Matchers.notNullValue());
    assertThat(warmersMetaData.entries().size(), equalTo(1));

    client()
        .prepareIndex("test", "type1", "1")
        .setSource("field", "value1")
        .setRefresh(true)
        .execute()
        .actionGet();
    client()
        .prepareIndex("test", "type1", "2")
        .setSource("field", "value2")
        .setRefresh(true)
        .execute()
        .actionGet();
  }