@Test
  public void testIpMultiField() throws Exception {
    assertAcked(
        client()
            .admin()
            .indices()
            .prepareCreate("my-index")
            .addMapping("my-type", createMappingSource("ip")));

    GetMappingsResponse getMappingsResponse =
        client().admin().indices().prepareGetMappings("my-index").get();
    MappingMetaData mappingMetaData = getMappingsResponse.mappings().get("my-index").get("my-type");
    assertThat(mappingMetaData, not(nullValue()));
    Map<String, Object> mappingSource = mappingMetaData.sourceAsMap();
    Map aField = ((Map) XContentMapValues.extractValue("properties.a", mappingSource));
    assertThat(aField.size(), equalTo(2));
    assertThat(aField.get("type").toString(), equalTo("ip"));
    assertThat(aField.get("fields"), notNullValue());

    Map bField = ((Map) XContentMapValues.extractValue("properties.a.fields.b", mappingSource));
    assertThat(bField.size(), equalTo(2));
    assertThat(bField.get("type").toString(), equalTo("string"));
    assertThat(bField.get("index").toString(), equalTo("not_analyzed"));

    client()
        .prepareIndex("my-index", "my-type", "1")
        .setSource("a", "127.0.0.1")
        .setRefresh(true)
        .get();
    CountResponse countResponse =
        client().prepareCount("my-index").setQuery(matchQuery("a.b", "127.0.0.1")).get();
    assertThat(countResponse.getCount(), equalTo(1l));
  }
  @Test
  public void testMultiFields() throws Exception {
    assertAcked(
        client()
            .admin()
            .indices()
            .prepareCreate("my-index")
            .addMapping("my-type", createTypeSource()));

    GetMappingsResponse getMappingsResponse =
        client().admin().indices().prepareGetMappings("my-index").get();
    MappingMetaData mappingMetaData = getMappingsResponse.mappings().get("my-index").get("my-type");
    assertThat(mappingMetaData, not(nullValue()));
    Map<String, Object> mappingSource = mappingMetaData.sourceAsMap();
    Map titleFields =
        ((Map) XContentMapValues.extractValue("properties.title.fields", mappingSource));
    assertThat(titleFields.size(), equalTo(1));
    assertThat(titleFields.get("not_analyzed"), notNullValue());
    assertThat(
        ((Map) titleFields.get("not_analyzed")).get("index").toString(), equalTo("not_analyzed"));

    client()
        .prepareIndex("my-index", "my-type", "1")
        .setSource("title", "Multi fields")
        .setRefresh(true)
        .get();

    SearchResponse searchResponse =
        client().prepareSearch("my-index").setQuery(matchQuery("title", "multi")).get();
    assertThat(searchResponse.getHits().totalHits(), equalTo(1l));
    searchResponse =
        client()
            .prepareSearch("my-index")
            .setQuery(matchQuery("title.not_analyzed", "Multi fields"))
            .get();
    assertThat(searchResponse.getHits().totalHits(), equalTo(1l));

    assertAcked(
        client()
            .admin()
            .indices()
            .preparePutMapping("my-index")
            .setType("my-type")
            .setSource(createPutMappingSource())
            .setIgnoreConflicts(
                true) // If updated with multi-field type, we need to ignore failures.
        );

    getMappingsResponse = client().admin().indices().prepareGetMappings("my-index").get();
    mappingMetaData = getMappingsResponse.mappings().get("my-index").get("my-type");
    assertThat(mappingMetaData, not(nullValue()));
    mappingSource = mappingMetaData.sourceAsMap();
    assertThat(
        ((Map) XContentMapValues.extractValue("properties.title", mappingSource)).size(),
        equalTo(2));
    titleFields = ((Map) XContentMapValues.extractValue("properties.title.fields", mappingSource));
    assertThat(titleFields.size(), equalTo(2));
    assertThat(titleFields.get("not_analyzed"), notNullValue());
    assertThat(
        ((Map) titleFields.get("not_analyzed")).get("index").toString(), equalTo("not_analyzed"));
    assertThat(titleFields.get("uncased"), notNullValue());
    assertThat(
        ((Map) titleFields.get("uncased")).get("analyzer").toString(), equalTo("whitespace"));

    client()
        .prepareIndex("my-index", "my-type", "1")
        .setSource("title", "Multi fields")
        .setRefresh(true)
        .get();

    searchResponse =
        client().prepareSearch("my-index").setQuery(matchQuery("title.uncased", "Multi")).get();
    assertThat(searchResponse.getHits().totalHits(), equalTo(1l));
  }
  public void testOldPercolatorIndex() throws Exception {
    setupNode();

    // verify cluster state:
    ClusterState state = client().admin().cluster().prepareState().get().getState();
    assertThat(state.metaData().indices().size(), equalTo(1));
    assertThat(state.metaData().indices().get(INDEX_NAME), notNullValue());
    assertThat(
        state.metaData().indices().get(INDEX_NAME).getCreationVersion(), equalTo(Version.V_2_0_0));
    assertThat(
        state.metaData().indices().get(INDEX_NAME).getUpgradedVersion(), equalTo(Version.CURRENT));
    assertThat(state.metaData().indices().get(INDEX_NAME).getMappings().size(), equalTo(2));
    assertThat(
        state.metaData().indices().get(INDEX_NAME).getMappings().get(".percolator"),
        notNullValue());
    // important: verify that the query field in the .percolator mapping is of type object (from
    // 3.0.0 this is of type percolator)
    MappingMetaData mappingMetaData =
        state.metaData().indices().get(INDEX_NAME).getMappings().get(".percolator");
    assertThat(
        XContentMapValues.extractValue("properties.query.type", mappingMetaData.sourceAsMap()),
        equalTo("object"));
    assertThat(
        state.metaData().indices().get(INDEX_NAME).getMappings().get("message"), notNullValue());

    // verify existing percolator queries:
    SearchResponse searchResponse =
        client()
            .prepareSearch(INDEX_NAME)
            .setTypes(".percolator")
            .addSort("_id", SortOrder.ASC)
            .get();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(3L));
    assertThat(searchResponse.getHits().getAt(0).id(), equalTo("1"));
    assertThat(searchResponse.getHits().getAt(1).id(), equalTo("2"));
    assertThat(searchResponse.getHits().getAt(2).id(), equalTo("3"));

    // verify percolate response
    PercolateResponse percolateResponse =
        client()
            .preparePercolate()
            .setIndices(INDEX_NAME)
            .setDocumentType("message")
            .setPercolateDoc(
                new PercolateSourceBuilder.DocBuilder()
                    .setDoc("message", "the quick brown fox jumps over the lazy dog"))
            .get();

    assertThat(percolateResponse.getCount(), equalTo(2L));
    assertThat(percolateResponse.getMatches().length, equalTo(2));
    assertThat(percolateResponse.getMatches()[0].getId().string(), equalTo("1"));
    assertThat(percolateResponse.getMatches()[1].getId().string(), equalTo("2"));

    // add an extra query and verify the results
    client()
        .prepareIndex(INDEX_NAME, ".percolator", "4")
        .setSource(
            jsonBuilder()
                .startObject()
                .field("query", matchQuery("message", "fox jumps"))
                .endObject())
        .get();
    refresh();

    percolateResponse =
        client()
            .preparePercolate()
            .setIndices(INDEX_NAME)
            .setDocumentType("message")
            .setPercolateDoc(
                new PercolateSourceBuilder.DocBuilder()
                    .setDoc("message", "the quick brown fox jumps over the lazy dog"))
            .get();

    assertThat(percolateResponse.getCount(), equalTo(3L));
    assertThat(percolateResponse.getMatches().length, equalTo(3));
    assertThat(percolateResponse.getMatches()[0].getId().string(), equalTo("1"));
    assertThat(percolateResponse.getMatches()[1].getId().string(), equalTo("2"));
    assertThat(percolateResponse.getMatches()[2].getId().string(), equalTo("4"));
  }