@Test
 public void testPreTokenization() throws IOException {
   // Make sure that pretokenization works well and that it can be used even with token chars which
   // are supplementary characters
   final Index index = new Index("test");
   final String name = "ngr";
   final Settings indexSettings = ImmutableSettings.EMPTY;
   Settings settings =
       ImmutableSettings.builder()
           .put("min_gram", 2)
           .put("max_gram", 3)
           .put("token_chars", "letter,digit")
           .build();
   assertTokenStreamContents(
       new NGramTokenizerFactory(index, indexSettings, name, settings)
           .create(new StringReader("Åbc déf g\uD801\uDC00f ")),
       new String[] {
         "Åb", "Åbc", "bc", "dé", "déf", "éf", "g\uD801\uDC00", "g\uD801\uDC00f", "\uD801\uDC00f"
       });
   settings =
       ImmutableSettings.builder()
           .put("min_gram", 2)
           .put("max_gram", 3)
           .put("token_chars", "letter,digit,punctuation,whitespace,symbol")
           .build();
   assertTokenStreamContents(
       new NGramTokenizerFactory(index, indexSettings, name, settings)
           .create(new StringReader(" a!$ 9")),
       new String[] {" a", " a!", "a!", "a!$", "!$", "!$ ", "$ ", "$ 9", " 9"});
 }
 @Test
 public void testParseTokenChars() {
   final Index index = new Index("test");
   final String name = "ngr";
   final Settings indexSettings = ImmutableSettings.EMPTY;
   for (String tokenChars : Arrays.asList("letters", "number", "DIRECTIONALITY_UNDEFINED")) {
     final Settings settings =
         ImmutableSettings.builder()
             .put("min_gram", 2)
             .put("max_gram", 3)
             .put("token_chars", tokenChars)
             .build();
     try {
       new NGramTokenizerFactory(index, indexSettings, name, settings)
           .create(new StringReader(""));
       fail();
     } catch (ElasticSearchIllegalArgumentException expected) {
       // OK
     }
   }
   for (String tokenChars :
       Arrays.asList("letter", " digit ", "punctuation", "DIGIT", "CoNtRoL", "dash_punctuation")) {
     final Settings settings =
         ImmutableSettings.builder()
             .put("min_gram", 2)
             .put("max_gram", 3)
             .put("token_chars", tokenChars)
             .build();
     new NGramTokenizerFactory(index, indexSettings, name, settings).create(new StringReader(""));
     // no exception
   }
 }
  @Test
  public void testSetIndexCompoundOnFlush() {
    client()
        .admin()
        .indices()
        .prepareCreate("test")
        .setSettings(
            ImmutableSettings.builder().put("number_of_replicas", 0).put("number_of_shards", 1))
        .get();
    client().prepareIndex("test", "foo").setSource("field", "foo").get();
    refresh();
    assertTotalCompoundSegments(1, 1, "test");
    client()
        .admin()
        .indices()
        .prepareUpdateSettings("test")
        .setSettings(ImmutableSettings.builder().put(RobinEngine.INDEX_COMPOUND_ON_FLUSH, false))
        .get();
    client().prepareIndex("test", "foo").setSource("field", "foo").get();
    refresh();
    assertTotalCompoundSegments(1, 2, "test");

    client()
        .admin()
        .indices()
        .prepareUpdateSettings("test")
        .setSettings(ImmutableSettings.builder().put(RobinEngine.INDEX_COMPOUND_ON_FLUSH, true))
        .get();
    client().prepareIndex("test", "foo").setSource("field", "foo").get();
    refresh();
    assertTotalCompoundSegments(2, 3, "test");
  }
  @Test
  public void testThatDifferentVersionsCanBeLoaded() {
    PreBuiltTokenizerFactoryFactory factory =
        new PreBuiltTokenizerFactoryFactory(
            PreBuiltTokenizers.STANDARD.getTokenizerFactory(Version.CURRENT));

    // different es versions, same lucene version, thus cached
    TokenizerFactory former090TokenizerFactory =
        factory.create(
            "standard",
            ImmutableSettings.settingsBuilder()
                .put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_0_90_1)
                .build());
    TokenizerFactory former090TokenizerFactoryCopy =
        factory.create(
            "standard",
            ImmutableSettings.settingsBuilder()
                .put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_0_90_2)
                .build());
    TokenizerFactory currentTokenizerFactory =
        factory.create(
            "standard",
            ImmutableSettings.settingsBuilder()
                .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
                .build());

    assertThat(currentTokenizerFactory, is(not(former090TokenizerFactory)));
    assertThat(currentTokenizerFactory, is(not(former090TokenizerFactoryCopy)));
    assertThat(former090TokenizerFactory, is(former090TokenizerFactoryCopy));
  }
  @Test
  public void testDefaultApplied() throws IOException {
    createIndex("test1", ImmutableSettings.settingsBuilder().build());
    createIndex("test2", ImmutableSettings.settingsBuilder().build());
    XContentBuilder defaultMapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject(MapperService.DEFAULT_MAPPING)
            .startObject("_size")
            .field("enabled", true)
            .endObject()
            .endObject()
            .endObject();
    client()
        .admin()
        .indices()
        .preparePutMapping()
        .setType(MapperService.DEFAULT_MAPPING)
        .setSource(defaultMapping)
        .get();
    XContentBuilder typeMapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("_all")
            .field("enabled", false)
            .endObject()
            .endObject()
            .endObject();
    client()
        .admin()
        .indices()
        .preparePutMapping("test1")
        .setType("type")
        .setSource(typeMapping)
        .get();
    client()
        .admin()
        .indices()
        .preparePutMapping("test1", "test2")
        .setType("type")
        .setSource(typeMapping)
        .get();

    GetMappingsResponse response = client().admin().indices().prepareGetMappings("test2").get();
    assertNotNull(response.getMappings().get("test2").get("type").getSourceAsMap().get("_all"));
    assertFalse(
        (Boolean)
            ((LinkedHashMap)
                    response.getMappings().get("test2").get("type").getSourceAsMap().get("_all"))
                .get("enabled"));
    assertNotNull(response.getMappings().get("test2").get("type").getSourceAsMap().get("_size"));
    assertTrue(
        (Boolean)
            ((LinkedHashMap)
                    response.getMappings().get("test2").get("type").getSourceAsMap().get("_size"))
                .get("enabled"));
  }
  @Test
  public void testSettings() throws IOException {
    {
      Settings settings =
          ImmutableSettings.settingsBuilder()
              .put("index.analysis.filter.limit_1.type", "limit")
              .put("index.analysis.filter.limit_1.max_token_count", 3)
              .put("index.analysis.filter.limit_1.consume_all_tokens", true)
              .build();
      AnalysisService analysisService =
          AnalysisTestsHelper.createAnalysisServiceFromSettings(settings);
      TokenFilterFactory tokenFilter = analysisService.tokenFilter("limit_1");
      String source = "the quick brown fox";
      String[] expected = new String[] {"the", "quick", "brown"};
      Tokenizer tokenizer = new WhitespaceTokenizer();
      tokenizer.setReader(new StringReader(source));
      assertTokenStreamContents(tokenFilter.create(tokenizer), expected);
    }
    {
      Settings settings =
          ImmutableSettings.settingsBuilder()
              .put("index.analysis.filter.limit_1.type", "limit")
              .put("index.analysis.filter.limit_1.max_token_count", 3)
              .put("index.analysis.filter.limit_1.consume_all_tokens", false)
              .build();
      AnalysisService analysisService =
          AnalysisTestsHelper.createAnalysisServiceFromSettings(settings);
      TokenFilterFactory tokenFilter = analysisService.tokenFilter("limit_1");
      String source = "the quick brown fox";
      String[] expected = new String[] {"the", "quick", "brown"};
      Tokenizer tokenizer = new WhitespaceTokenizer();
      tokenizer.setReader(new StringReader(source));
      assertTokenStreamContents(tokenFilter.create(tokenizer), expected);
    }

    {
      Settings settings =
          ImmutableSettings.settingsBuilder()
              .put("index.analysis.filter.limit_1.type", "limit")
              .put("index.analysis.filter.limit_1.max_token_count", 17)
              .put("index.analysis.filter.limit_1.consume_all_tokens", true)
              .build();
      AnalysisService analysisService =
          AnalysisTestsHelper.createAnalysisServiceFromSettings(settings);
      TokenFilterFactory tokenFilter = analysisService.tokenFilter("limit_1");
      String source = "the quick brown fox";
      String[] expected = new String[] {"the", "quick", "brown", "fox"};
      Tokenizer tokenizer = new WhitespaceTokenizer();
      tokenizer.setReader(new StringReader(source));
      assertTokenStreamContents(tokenFilter.create(tokenizer), expected);
    }
  }
 protected synchronized Settings nodeSettings(int nodeOrdinal) {
   if (nodeOrdinal == 0) { // at least one
     return ImmutableSettings.builder().put("node.bench", true).build();
   } else {
     if (benchNodes.containsKey(nodeOrdinal)) {
       return ImmutableSettings.builder().put("node.bench", benchNodes.get(nodeOrdinal)).build();
     } else {
       boolean b = randomBoolean();
       benchNodes.put(nodeOrdinal, b);
       return ImmutableSettings.builder().put("node.bench", b).build();
     }
   }
 }
  public void testDuelGlobalOrdinals() throws Exception {
    Random random = getRandom();
    final int numDocs = scaledRandomIntBetween(10, 1000);
    final int numValues = scaledRandomIntBetween(10, 500);
    final String[] values = new String[numValues];
    for (int i = 0; i < numValues; ++i) {
      values[i] = new String(RandomStrings.randomAsciiOfLength(random, 10));
    }
    for (int i = 0; i < numDocs; i++) {
      Document d = new Document();
      final int numVals = randomInt(3);
      for (int j = 0; j < numVals; ++j) {
        final String value = RandomPicks.randomFrom(random, Arrays.asList(values));
        d.add(new StringField("string", value, Field.Store.NO));
        d.add(new SortedSetDocValuesField("bytes", new BytesRef(value)));
      }
      writer.addDocument(d);
      if (randomInt(10) == 0) {
        refreshReader();
      }
    }
    refreshReader();

    Map<FieldDataType, Type> typeMap = new HashMap<FieldDataType, DuelFieldDataTests.Type>();
    typeMap.put(
        new FieldDataType("string", ImmutableSettings.builder().put("format", "fst")), Type.Bytes);
    typeMap.put(
        new FieldDataType("string", ImmutableSettings.builder().put("format", "paged_bytes")),
        Type.Bytes);
    typeMap.put(
        new FieldDataType("string", ImmutableSettings.builder().put("format", "doc_values")),
        Type.Bytes);

    for (Map.Entry<FieldDataType, Type> entry : typeMap.entrySet()) {
      ifdService.clear();
      IndexOrdinalsFieldData fieldData =
          getForField(entry.getKey(), entry.getValue().name().toLowerCase(Locale.ROOT));
      RandomAccessOrds left = fieldData.load(readerContext).getOrdinalsValues();
      fieldData.clear();
      RandomAccessOrds right =
          fieldData
              .loadGlobal(topLevelReader)
              .load(topLevelReader.leaves().get(0))
              .getOrdinalsValues();
      assertEquals(left.getValueCount(), right.getValueCount());
      for (long ord = 0; ord < left.getValueCount(); ++ord) {
        assertEquals(left.lookupOrd(ord), right.lookupOrd(ord));
      }
    }
  }
Example #9
0
  @Before
  public void setUp() throws Exception {
    super.setUp();
    threadPool = new ThreadPool();
    serviceA = build(ImmutableSettings.builder().put("name", "TS_A").build(), version0);
    nodeA =
        new DiscoveryNode(
            "TS_A",
            "TS_A",
            serviceA.boundAddress().publishAddress(),
            ImmutableMap.<String, String>of(),
            version0);
    serviceB = build(ImmutableSettings.builder().put("name", "TS_B").build(), version1);
    nodeB =
        new DiscoveryNode(
            "TS_B",
            "TS_B",
            serviceB.boundAddress().publishAddress(),
            ImmutableMap.<String, String>of(),
            version1);

    // wait till all nodes are properly connected and the event has been sent, so tests in this
    // class
    // will not get this callback called on the connections done in this setup
    final CountDownLatch latch = new CountDownLatch(4);
    TransportConnectionListener waitForConnection =
        new TransportConnectionListener() {
          @Override
          public void onNodeConnected(DiscoveryNode node) {
            latch.countDown();
          }

          @Override
          public void onNodeDisconnected(DiscoveryNode node) {
            assert false : "disconnect should not be called " + node;
          }
        };
    serviceA.addConnectionListener(waitForConnection);
    serviceB.addConnectionListener(waitForConnection);

    serviceA.connectToNode(nodeB);
    serviceA.connectToNode(nodeA);
    serviceB.connectToNode(nodeA);
    serviceB.connectToNode(nodeB);

    assertThat(
        "failed to wait for all nodes to connect", latch.await(5, TimeUnit.SECONDS), equalTo(true));
    serviceA.removeConnectionListener(waitForConnection);
    serviceB.removeConnectionListener(waitForConnection);
  }
  @Test
  public void testBackwardsCompatibilityEdgeNgramTokenFilter()
      throws IllegalArgumentException, IllegalAccessException {
    int iters = atLeast(20);
    for (int i = 0; i < iters; i++) {
      final Index index = new Index("test");
      final String name = "ngr";
      Version v = randomVersion(random());
      if (v.onOrAfter(Version.V_0_90_2)) {
        Builder builder = ImmutableSettings.builder().put("min_gram", 2).put("max_gram", 3);
        boolean compatVersion = false;
        if ((compatVersion = random().nextBoolean())) {
          builder.put("version", "4." + random().nextInt(3));
        }
        boolean reverse = random().nextBoolean();
        if (reverse) {
          builder.put("side", "back");
        }
        Settings settings = builder.build();
        Settings indexSettings =
            ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, v.id).build();
        TokenStream edgeNGramTokenFilter =
            new EdgeNGramTokenFilterFactory(index, indexSettings, name, settings)
                .create(new MockTokenizer(new StringReader("foo bar")));
        if (compatVersion) {
          assertThat(edgeNGramTokenFilter, instanceOf(EdgeNGramTokenFilter.class));
        } else if (reverse && !compatVersion) {
          assertThat(edgeNGramTokenFilter, instanceOf(ReverseStringFilter.class));
        } else {
          assertThat(edgeNGramTokenFilter, instanceOf(EdgeNGramTokenFilter.class));
        }

      } else {
        Builder builder = ImmutableSettings.builder().put("min_gram", 2).put("max_gram", 3);
        boolean reverse = random().nextBoolean();
        if (reverse) {
          builder.put("side", "back");
        }
        Settings settings = builder.build();
        Settings indexSettings =
            ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, v.id).build();
        TokenStream edgeNGramTokenFilter =
            new EdgeNGramTokenFilterFactory(index, indexSettings, name, settings)
                .create(new MockTokenizer(new StringReader("foo bar")));
        assertThat(edgeNGramTokenFilter, instanceOf(EdgeNGramTokenFilter.class));
      }
    }
  }
Example #11
0
  @Test
  public void testLimitFilter() throws Exception {
    try {
      client.admin().indices().prepareDelete("test").execute().actionGet();
    } catch (Exception e) {
      // ignore
    }

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

    client.prepareIndex("test", "type1", "1").setSource("field1", "value1_1").execute().actionGet();
    client.prepareIndex("test", "type1", "2").setSource("field1", "value1_2").execute().actionGet();
    client.prepareIndex("test", "type1", "3").setSource("field2", "value2_3").execute().actionGet();
    client.prepareIndex("test", "type1", "4").setSource("field3", "value3_4").execute().actionGet();

    client.admin().indices().prepareRefresh().execute().actionGet();

    SearchResponse searchResponse =
        client
            .prepareSearch()
            .setQuery(filteredQuery(matchAllQuery(), limitFilter(2)))
            .execute()
            .actionGet();
    assertThat(searchResponse.hits().totalHits(), equalTo(2l));
  }
Example #12
0
    public IndexMetaData build() {
      MapBuilder<String, AliasMetaData> tmpAliases = aliases;
      Settings tmpSettings = settings;

      // For backward compatibility
      String[] legacyAliases = settings.getAsArray("index.aliases");
      if (legacyAliases.length > 0) {
        tmpAliases = MapBuilder.newMapBuilder();
        for (String alias : legacyAliases) {
          AliasMetaData aliasMd = AliasMetaData.newAliasMetaDataBuilder(alias).build();
          tmpAliases.put(alias, aliasMd);
        }
        tmpAliases.putAll(aliases.immutableMap());
        // Remove index.aliases from settings once they are migrated to the new data structure
        tmpSettings =
            ImmutableSettings.settingsBuilder().put(settings).putArray("index.aliases").build();
      }

      // update default mapping on the MappingMetaData
      if (mappings.containsKey(MapperService.DEFAULT_MAPPING)) {
        MappingMetaData defaultMapping = mappings.get(MapperService.DEFAULT_MAPPING);
        for (MappingMetaData mappingMetaData : mappings.map().values()) {
          mappingMetaData.updateDefaultMapping(defaultMapping);
        }
      }

      return new IndexMetaData(
          index,
          version,
          state,
          tmpSettings,
          mappings.immutableMap(),
          tmpAliases.immutableMap(),
          customs.immutableMap());
    }
Example #13
0
  public static void main(String[] args) {
    Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "TSB-QA").build();
    // on startup
    Client client =
        new TransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress("10.0.1.68", 9300))
            .addTransportAddress(new InetSocketTransportAddress("10.0.1.69", 9300));

    SearchRequestBuilder srb1 =
        client.prepareSearch().setQuery(QueryBuilders.queryString("TSB-QA")).setSize(1);
    SearchRequestBuilder srb2 =
        client.prepareSearch().setQuery(QueryBuilders.matchQuery("name", "kimchy")).setSize(1);

    MultiSearchResponse sr = client.prepareMultiSearch().add(srb1).add(srb2).execute().actionGet();

    // You will get all individual responses from
    // MultiSearchResponse#getResponses()
    long nbHits = 0;
    for (MultiSearchResponse.Item item : sr.getResponses()) {
      SearchResponse response = item.getResponse();
      System.out.println(response.getHits().getTotalHits());
      nbHits += response.getHits().getTotalHits();
    }
    System.out.println(nbHits);
  }
  public void testDisablingBackcompat() throws Exception {
    // before 1.5, disabling happened by setting index:no
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("_field_names")
            .field("index", "no")
            .endObject()
            .endObject()
            .endObject()
            .string();

    Settings indexSettings =
        ImmutableSettings.builder()
            .put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id)
            .build();
    DocumentMapper docMapper =
        createIndex("test", indexSettings).mapperService().documentMapperParser().parse(mapping);
    FieldNamesFieldMapper fieldNamesMapper = docMapper.rootMapper(FieldNamesFieldMapper.class);
    assertFalse(fieldNamesMapper.enabled());

    ParsedDocument doc =
        docMapper.parse(
            "type",
            "1",
            XContentFactory.jsonBuilder()
                .startObject()
                .field("field", "value")
                .endObject()
                .bytes());

    assertNull(doc.rootDoc().get("_field_names"));
  }
  @Test
  public void simpleDateRangeWithUpperInclusiveEnabledTests() throws Exception {
    prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder()).execute().actionGet();
    client()
        .prepareIndex("test", "type1", "1")
        .setSource("field", "2010-01-05T02:00")
        .execute()
        .actionGet();
    client()
        .prepareIndex("test", "type1", "2")
        .setSource("field", "2010-01-06T02:00")
        .execute()
        .actionGet();
    client().admin().indices().prepareRefresh().execute().actionGet();

    // test include upper on ranges to include the full day on the upper bound
    SearchResponse searchResponse =
        client()
            .prepareSearch("test")
            .setQuery(QueryBuilders.rangeQuery("field").gte("2010-01-05").lte("2010-01-06"))
            .execute()
            .actionGet();
    assertHitCount(searchResponse, 2l);
    searchResponse =
        client()
            .prepareSearch("test")
            .setQuery(QueryBuilders.rangeQuery("field").gte("2010-01-05").lt("2010-01-06"))
            .execute()
            .actionGet();
    assertHitCount(searchResponse, 1l);
  }
  @Test
  public void testFactoryWithTransportClient() throws IOException {
    // setup client for testing http connection params
    Path baseDir = Paths.get("target/elasticsearch");
    String dataPath = Files.createTempDirectory(baseDir, null).toAbsolutePath().toString();
    Settings build = ImmutableSettings.builder().put("path.data", dataPath).build();
    Node node = nodeBuilder().settings(build).node();

    assertTrue(new ElasticDataStoreFactory().isAvailable());
    scanForPlugins();

    Map<String, Serializable> map = new HashMap<>();
    map.put(ElasticDataStoreFactory.HOSTNAME.key, "localhost");
    map.put(ElasticDataStoreFactory.HOSTPORT.key, String.valueOf(9300));
    map.put(ElasticDataStoreFactory.INDEX_NAME.key, "sample");

    Iterator<DataStoreFactorySpi> ps = getAvailableDataSources();
    ElasticDataStoreFactory fac;
    while (ps.hasNext()) {
      fac = (ElasticDataStoreFactory) ps.next();

      try {
        if (fac.canProcess(map)) {
          source = fac.createDataStore(map);
        }
      } catch (Throwable t) {
        LOGGER.log(Level.WARNING, "Could not acquire " + fac.getDescription() + ":" + t, t);
      }
    }

    assertNotNull(source);
    assertTrue(source instanceof ElasticDataStore);
    node.close();
  }
 @Override
 protected Settings nodeSettings(int nodeOrdinal) {
   return ImmutableSettings.settingsBuilder()
       .put(super.nodeSettings(nodeOrdinal))
       .put("plugin.types", ExternalMapperPlugin.class.getName())
       .build();
 }
  @Test
  public void simpleIdTests() {
    client.admin().indices().prepareDelete().execute().actionGet();
    client
        .admin()
        .indices()
        .prepareCreate("test")
        .setSettings(ImmutableSettings.settingsBuilder().put("number_of_shards", 1))
        .execute()
        .actionGet();

    client
        .prepareIndex("test", "type", "1")
        .setSource("field", "value")
        .setRefresh(true)
        .execute()
        .actionGet();
    // id is not indexed, but lets see that we automatically convert to
    SearchResponse searchResponse =
        client.prepareSearch().setQuery(QueryBuilders.termQuery("_id", "1")).execute().actionGet();
    assertThat(searchResponse.hits().totalHits(), equalTo(1l));

    searchResponse =
        client.prepareSearch().setQuery(QueryBuilders.queryString("_id:1")).execute().actionGet();
    assertThat(searchResponse.hits().totalHits(), equalTo(1l));
  }
  @Override
  public void connect() {
    ImmutableSettings.Builder builder = ImmutableSettings.settingsBuilder();

    loadOptionalDefaultConfigurations(builder);

    String[] additionalConfigurations = elasticsearchConfiguration.additionalConfigurations();

    if (ArrayUtil.isNotEmpty(additionalConfigurations)) {
      StringBundler sb = new StringBundler(additionalConfigurations.length * 2);

      for (String additionalConfiguration : additionalConfigurations) {
        sb.append(additionalConfiguration);
        sb.append(StringPool.NEW_LINE);
      }

      builder.loadFromSource(sb.toString());
    }

    loadRequiredDefaultConfigurations(builder);

    loadSettingsContributors(builder);

    _client = createClient(builder);
  }
  public static void createDevNode() {
    LOGGER.info("Creating dev ES node ...");
    Path localDevDataDirectory = Paths.get(ES_LOCAL_DATA);
    try {
      FileUtils.deleteDirectory(localDevDataDirectory.toFile());
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    Settings settings =
        ImmutableSettings.builder()
            .put("http.port", "9200")
            .put("network.host", "localhost")
            .put("path.data", ES_LOCAL_DATA)
            .build();

    Node node =
        NodeBuilder.nodeBuilder()
            .local(true)
            .data(true)
            .clusterName("elasticSearch" + UUID.randomUUID())
            .settings(settings)
            .build();
    node.start();
    // loading settings
    run(ReloadDefaultSettings.class);
    ImportAllDataJob.elasticType = "test-ES";
    run(ImportAllDataJob.class);
  }
Example #21
0
  @Test
  public void passQueryAsStringTest() throws Exception {
    try {
      client.admin().indices().prepareDelete("test").execute().actionGet();
    } catch (Exception e) {
      // ignore
    }

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

    client
        .prepareIndex("test", "type1", "1")
        .setSource("field1", "value1_1", "field2", "value2_1")
        .setRefresh(true)
        .execute()
        .actionGet();

    SearchResponse searchResponse =
        client
            .prepareSearch()
            .setQuery("{ \"term\" : { \"field1\" : \"value1_1\" }}")
            .execute()
            .actionGet();
    assertThat(searchResponse.hits().totalHits(), equalTo(1l));
  }
 @Test
 public void testSizeParsing() throws IOException {
   IndexService indexService = createIndex("test", ImmutableSettings.settingsBuilder().build());
   XContentBuilder indexMapping = XContentFactory.jsonBuilder();
   boolean enabled = randomBoolean();
   indexMapping
       .startObject()
       .startObject("type")
       .startObject("_size")
       .field("enabled", enabled)
       .endObject()
       .endObject()
       .endObject();
   DocumentMapper documentMapper =
       indexService
           .mapperService()
           .parse("type", new CompressedString(indexMapping.string()), true);
   assertThat(documentMapper.sizeFieldMapper().enabled(), equalTo(enabled));
   assertTrue(documentMapper.sizeFieldMapper().fieldType().stored());
   documentMapper.refreshSource();
   documentMapper =
       indexService
           .mapperService()
           .parse("type", new CompressedString(documentMapper.mappingSource().string()), true);
   assertThat(documentMapper.sizeFieldMapper().enabled(), equalTo(enabled));
 }
  @Test
  public void testThatGetFromTranslogShouldWorkWithIncludeExcludeAndFields() throws Exception {
    client.admin().indices().prepareDelete().execute().actionGet();
    String index = "test";
    String type = "type1";

    String mapping =
        jsonBuilder()
            .startObject()
            .startObject("source_excludes")
            .startObject("_source")
            .array("includes", "included")
            .array("exlcudes", "excluded")
            .endObject()
            .endObject()
            .endObject()
            .string();

    client
        .admin()
        .indices()
        .prepareCreate(index)
        .addMapping(type, mapping)
        .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1))
        .execute()
        .actionGet();

    client
        .prepareIndex(index, type, "1")
        .setSource(
            jsonBuilder()
                .startObject()
                .field("field", "1", "2")
                .field("included", "should be seen")
                .field("excluded", "should not be seen")
                .endObject())
        .execute()
        .actionGet();

    GetResponse responseBeforeFlush =
        client
            .prepareGet(index, type, "1")
            .setFields("_source", "included", "excluded")
            .execute()
            .actionGet();
    client.admin().indices().prepareFlush(index).execute().actionGet();
    GetResponse responseAfterFlush =
        client
            .prepareGet(index, type, "1")
            .setFields("_source", "included", "excluded")
            .execute()
            .actionGet();

    assertThat(responseBeforeFlush.isExists(), is(true));
    assertThat(responseAfterFlush.isExists(), is(true));
    assertThat(responseBeforeFlush.getSourceAsMap(), not(hasKey("excluded")));
    assertThat(responseBeforeFlush.getSourceAsMap(), not(hasKey("field")));
    assertThat(responseBeforeFlush.getSourceAsMap(), hasKey("included"));
    assertThat(responseBeforeFlush.getSourceAsString(), is(responseAfterFlush.getSourceAsString()));
  }
  @Test(expected = MergeMappingException.class)
  public void updateMappingWithConflicts() throws Exception {

    client()
        .admin()
        .indices()
        .prepareCreate("test")
        .setSettings(
            ImmutableSettings.settingsBuilder()
                .put("index.number_of_shards", 2)
                .put("index.number_of_replicas", 0))
        .addMapping("type", "{\"type\":{\"properties\":{\"body\":{\"type\":\"string\"}}}}")
        .execute()
        .actionGet();
    client()
        .admin()
        .cluster()
        .prepareHealth()
        .setWaitForEvents(Priority.LANGUID)
        .setWaitForGreenStatus()
        .execute()
        .actionGet();

    PutMappingResponse putMappingResponse =
        client()
            .admin()
            .indices()
            .preparePutMapping("test")
            .setType("type")
            .setSource("{\"type\":{\"properties\":{\"body\":{\"type\":\"integer\"}}}}")
            .execute()
            .actionGet();

    assertThat(putMappingResponse.isAcknowledged(), equalTo(true));
  }
Example #25
0
 @Override
 protected Settings nodeSettings(int nodeOrdinal) {
   return ImmutableSettings.builder()
       .put(super.nodeSettings(nodeOrdinal))
       .put(InternalNode.HTTP_ENABLED, true)
       .build();
 }
 @Override
 protected Settings nodeSettings(int nodeOrdinal) {
   return ImmutableSettings.settingsBuilder()
       .put(super.nodeSettings(nodeOrdinal))
       .put("threadpool.search.type", "cached")
       .build();
 }
 @Override
 protected Settings getIndexSettings() throws IOException {
   return ImmutableSettings.builder()
       .put("index.number_of_replicas", 0)
       .put("index.number_of_shards", 1)
       .build();
 }
  @Test
  public void testSearchRandomPreference() throws InterruptedException, ExecutionException {
    client()
        .admin()
        .indices()
        .prepareCreate("test")
        .setSettings(
            ImmutableSettings.settingsBuilder().put("index.number_of_shards", between(1, 3)))
        .get();
    indexRandom(
        true,
        client().prepareIndex("test", "type", "1").setSource("field", "value"),
        client().prepareIndex("test", "type", "2").setSource("field", "value"),
        client().prepareIndex("test", "type", "3").setSource("field", "value"),
        client().prepareIndex("test", "type", "4").setSource("field", "value"),
        client().prepareIndex("test", "type", "5").setSource("field", "value"),
        client().prepareIndex("test", "type", "6").setSource("field", "value"));

    int iters = atLeast(10);
    for (int i = 0; i < iters; i++) {
      // id is not indexed, but lets see that we automatically convert to
      SearchResponse searchResponse =
          client()
              .prepareSearch()
              .setQuery(QueryBuilders.matchAllQuery())
              .setPreference(randomUnicodeOfLengthBetween(0, 4))
              .get();
      assertHitCount(searchResponse, 6l);
    }
  }
 @Test
 public void testDefault() throws IOException {
   Settings settings =
       ImmutableSettings.settingsBuilder()
           .put("index.analysis.filter.limit_default.type", "limit")
           .build();
   AnalysisService analysisService =
       AnalysisTestsHelper.createAnalysisServiceFromSettings(settings);
   {
     TokenFilterFactory tokenFilter = analysisService.tokenFilter("limit_default");
     String source = "the quick brown fox";
     String[] expected = new String[] {"the"};
     Tokenizer tokenizer = new WhitespaceTokenizer();
     tokenizer.setReader(new StringReader(source));
     assertTokenStreamContents(tokenFilter.create(tokenizer), expected);
   }
   {
     TokenFilterFactory tokenFilter = analysisService.tokenFilter("limit");
     String source = "the quick brown fox";
     String[] expected = new String[] {"the"};
     Tokenizer tokenizer = new WhitespaceTokenizer();
     tokenizer.setReader(new StringReader(source));
     assertTokenStreamContents(tokenFilter.create(tokenizer), expected);
   }
 }
  @Test
  @TestLogging("action.search.type:TRACE,action.admin.indices.refresh:TRACE")
  public void simpleDateMathTests() throws Exception {
    prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder()).execute().actionGet();
    client()
        .prepareIndex("test", "type1", "1")
        .setSource("field", "2010-01-05T02:00")
        .execute()
        .actionGet();
    client()
        .prepareIndex("test", "type1", "2")
        .setSource("field", "2010-01-06T02:00")
        .execute()
        .actionGet();
    ensureGreen();
    refresh();
    SearchResponse searchResponse =
        client()
            .prepareSearch("test")
            .setQuery(
                QueryBuilders.rangeQuery("field").gte("2010-01-03||+2d").lte("2010-01-04||+2d"))
            .execute()
            .actionGet();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, 2l);

    searchResponse =
        client()
            .prepareSearch("test")
            .setQuery(QueryBuilders.queryString("field:[2010-01-03||+2d TO 2010-01-04||+2d]"))
            .execute()
            .actionGet();
    assertHitCount(searchResponse, 2l);
  }