@Test
  public void testCustomInjection() {
    Settings settings = ImmutableSettings.builder().put("name", "testCustomInjection").build();

    IndexQueryParserModule queryParserModule = new IndexQueryParserModule(settings);
    queryParserModule.addQueryParser("my", PluginJsonQueryParser.class);
    queryParserModule.addFilterParser("my", PluginJsonFilterParser.class);

    Index index = new Index("test");
    Injector injector =
        new ModulesBuilder()
            .add(
                new CodecModule(settings),
                new CacheRecyclerModule(settings),
                new SettingsModule(settings),
                new ThreadPoolModule(settings),
                new IndicesQueriesModule(),
                new ScriptModule(settings),
                new IndexSettingsModule(index, settings),
                new IndexCacheModule(settings),
                new AnalysisModule(settings),
                new IndexEngineModule(settings),
                new SimilarityModule(settings),
                queryParserModule,
                new IndexNameModule(index),
                new FunctionScoreModule(),
                new AbstractModule() {
                  @Override
                  protected void configure() {
                    bind(ClusterService.class).toProvider(Providers.of((ClusterService) null));
                    bind(CircuitBreakerService.class).to(NoneCircuitBreakerService.class);
                  }
                })
            .createInjector();

    IndexQueryParserService indexQueryParserService =
        injector.getInstance(IndexQueryParserService.class);

    PluginJsonQueryParser myJsonQueryParser =
        (PluginJsonQueryParser) indexQueryParserService.queryParser("my");

    assertThat(myJsonQueryParser.names()[0], equalTo("my"));

    PluginJsonFilterParser myJsonFilterParser =
        (PluginJsonFilterParser) indexQueryParserService.filterParser("my");
    assertThat(myJsonFilterParser.names()[0], equalTo("my"));

    injector.getInstance(ThreadPool.class).shutdownNow();
  }
示例#2
0
  private Query parseQuery(String type, BytesReference querySource, XContentParser parser) {
    if (type == null) {
      if (parser != null) {
        return queryParserService.parse(parser).query();
      } else {
        return queryParserService.parse(querySource).query();
      }
    }

    String[] previousTypes = QueryParseContext.setTypesWithPrevious(new String[] {type});
    try {
      if (parser != null) {
        return queryParserService.parse(parser).query();
      } else {
        return queryParserService.parse(querySource).query();
      }
    } finally {
      QueryParseContext.setTypes(previousTypes);
    }
  }