# How to set a analyser for all the fields and the "\_all" field

**URL:** <https://discuss.elastic.co/t/how-to-set-a-analyser-for-all-the-fields-and-the--all-field/31498>\
**Category:** Elasticsearch\
**Created:** [October 1, 2015, 5:47pm UTC](https://discuss.elastic.co/t/how-to-set-a-analyser-for-all-the-fields-and-the--all-field/31498 "2015-10-01T17:47:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guill](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guill/32/4779_2.png) [@Guill](https://discuss.elastic.co/u/Guill)\
**Post date:** [October 1, 2015, 5:47pm UTC](https://discuss.elastic.co/t/how-to-set-a-analyser-for-all-the-fields-and-the--all-field/31498/1 "2015-10-01T17:47:15Z")

</div>

Hello, I am using the elasticsearch-analysis-kuromoji, being created like so:

```
public final static String JAPANESE_LANGUAGE_ANALYSIS = "japanese_analyzer";

private void createCustomerIndex() {
    try {
        client.admin().indices().refresh(new RefreshRequest(Index.customer.name()));
        final CreateIndexRequestBuilder createIndexRequestBuilder = client.admin()
                .indices()
                .prepareCreate(Index.customer.name())
                .setSettings(ImmutableSettings.settingsBuilder().loadFromSource(jsonBuilder()
                        .startObject()
                        .startObject("analysis")
                                //
                        .startObject("tokenizer")
                        .startObject("kuromoji_user_dict")
                        .field("type", "kuromoji_tokenizer")
                        .field("mode", "search")
                        .field("discard_punctuation", "false")
                        .endObject()
                        .endObject()
                                //
                        .startObject("analyzer")
                        .startObject(JAPANESE_LANGUAGE_ANALYSIS)
                        .field("type", "custom")
                        .field("tokenizer", "kuromoji_user_dict")
                        .endObject()
                        .endObject()
                                //
                        .endObject()
                        .endObject().string()));

        createIndexRequestBuilder.execute().actionGet();

        String indexProperties = Customer.indexProperties();

        //if a index had properties
        if (indexProperties != null) {
            createIndexProperties(Index.customer.name(), indexProperties);
        }

    } catch (IOException e) {
        logger.error("Error Customer company Index");
        logger.error(e.getMessage());
    }

```

}

And am querying it like this:

```
public SearchResponse queryForFieldAndTerm(String field, String term, ElasticSearchService.Index index) {
    logger.info("Searching index {} for term: {}", index.name(), term);

    SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client);
    BoolQueryBuilder qb = QueryBuilders.boolQuery();

    QueryStringQueryBuilder queryBuilder = QueryBuilders
            .queryString(term)
            .defaultField(field)
            .defaultOperator(QueryStringQueryBuilder.Operator.AND);
    queryBuilder.analyzer("japanese_analyzer");
    qb.minimumShouldMatch("1");
    qb.must(queryBuilder);

    searchRequestBuilder.setQuery(qb);

    return searchRequestBuilder
            .setFrom(1)
            .setSize(10)
            .setIndices(index.name())
            .execute()
            .actionGet();
}

```

If I add the mapping to a field specifically, like:

```
XContentBuilder xbMapping =
        jsonBuilder()
                .startObject()
                .startObject(indexType)
                .startObject("properties")
                .startObject("source")
                .field("type", "string")
                .endObject()
                .startObject("text")
                .field("type", "string")
                .field("analyzer", JAPANESE_LANGUAGE_ANALYSIS)
                .endObject()
                .endObject()
                .endObject()
                .endObject();

elasticSearchClient.admin().indices()
        .preparePutMapping(indexName)
        .setType(indexType)
        .setSource(xbMapping)
        .execute().get();

```

If works, but I don't have control over all of the fields, so I am looking for the optimal way to set the analysers, if I should have a index for each language, and how to set the analyser for all the fields, or if I can do that with a type.

Thanks !!

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [October 1, 2015, 7:50pm UTC](https://discuss.elastic.co/t/how-to-set-a-analyser-for-all-the-fields-and-the--all-field/31498/2 "2015-10-01T19:50:33Z")

</div>

Not sure what `JAPANESE_LANGUAGE_ANALYSIS` stands for, but if you set the analyzer name to `default`, Elasticsearch will use it for all fields, including `_all`.

---

<div class="post-metadata">

**Author:** ![Guill](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guill/32/4779_2.png) [@Guill](https://discuss.elastic.co/u/Guill)\
**Post date:** [October 1, 2015, 9:15pm UTC](https://discuss.elastic.co/t/how-to-set-a-analyser-for-all-the-fields-and-the--all-field/31498/3 "2015-10-01T21:15:49Z")

</div>

I have edited it, its just the name. Am running some tests with default as the name, will let you know, thanks !!

---

<div class="post-metadata">

**Author:** ![Guill](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guill/32/4779_2.png) [@Guill](https://discuss.elastic.co/u/Guill)\
**Post date:** [October 6, 2015, 8:09pm UTC](https://discuss.elastic.co/t/how-to-set-a-analyser-for-all-the-fields-and-the--all-field/31498/4 "2015-10-06T20:09:28Z")

</div>

I have run a few tests, but tbh ii don't see the difference between having or not having the analyser:  
here is it having it

```
@Test
public void queryWithJapaneseAnalyserOnAll() throws InterruptedException {
    try {
        client.admin().indices().refresh(new RefreshRequest(indexName));
        final CreateIndexRequestBuilder createIndexRequestBuilder = client.admin()
                .indices()
                .prepareCreate(indexName)
                .setSettings(ImmutableSettings.settingsBuilder()
                        .loadFromSource(jsonBuilder()
                                .startObject()
                                .startObject("analysis")
                                        //
                                .startObject("tokenizer")
                                .startObject("kuromoji_user_dict")
                                .field("type", "kuromoji_tokenizer")
                                .field("mode", "search")
                                .field("discard_punctuation", "false")
                                .endObject()
                                .endObject()
                                        //
                                .startObject("analyzer")
                                        //named default
                                .startObject("default")
                                .field("type", "custom")
                                .field("tokenizer", "kuromoji_user_dict")
                                .endObject()
                                .endObject()
                                        //
                                .endObject()
                                .endObject().string()));
        createIndexRequestBuilder.execute().actionGet();
    } catch (Exception e) {
        logger.error("Error creating index {}", indexName);
        logger.error(e.getMessage());
    }
    ObjectMapper objectMapper = new ObjectMapper();
    Set<String> interestSet = null;
    Set<Address> addressSet = null;
    Set<Telephone> telephoneSet = null;
    Customer customer = new Customer("1", "開発者ジャワ", "Melo", "title", "Test Writer", "test@gmail.org", interestSet, addressSet, telephoneSet);
    try {
        String data = objectMapper.writeValueAsString(customer);
        logger.debug("Sending index indexName={} indexType={} id={}", new Object[]{indexName, indexName, customer.getId()});
        client.prepareIndex(indexName, indexName)
                .setId(customer.getId())
                .setSource(data)
                .execute().actionGet();
    } catch (IOException e) {
        logger.error("Error sending Index {}", e);
    }
    Thread.sleep(900);
    SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client);
    BoolQueryBuilder qb = QueryBuilders.boolQuery();
    QueryStringQueryBuilder queryBuilder = QueryBuilders
            .queryString("ジャワ")
            .defaultOperator(QueryStringQueryBuilder.Operator.AND);
    qb.minimumShouldMatch("1");
    queryBuilder.analyzer("default");
    qb.must(queryBuilder);
    searchRequestBuilder.setQuery(qb);
    SearchResponse response = searchRequestBuilder
            .setIndices(indexName)
            .execute()
            .actionGet();
    try {
        DeleteIndexResponse deleteIndexResponse = client.admin().indices().delete(new DeleteIndexRequest(indexName)).actionGet();
        logger.debug("Delete index response={}", ToStringBuilder.reflectionToString(deleteIndexResponse));
    } catch (Exception e) {
        logger.error(e.getMessage());
    }
    Assert.assertEquals(1, response.getHits().totalHits());
}
```

---

<div class="post-metadata">

**Author:** ![Guill](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guill/32/4779_2.png) [@Guill](https://discuss.elastic.co/u/Guill)\
**Post date:** [October 6, 2015, 8:09pm UTC](https://discuss.elastic.co/t/how-to-set-a-analyser-for-all-the-fields-and-the--all-field/31498/5 "2015-10-06T20:09:51Z")

</div>

And here not having it:

```
@Test
    public void query_WithNoAnalyserOnField() throws InterruptedException {
        try {
            client.admin().indices().refresh(new RefreshRequest(indexName));
            final CreateIndexRequestBuilder createIndexRequestBuilder = client.admin()
                    .indices()
                    .prepareCreate(indexName);
            createIndexRequestBuilder.execute().actionGet();
        } catch (Exception e) {
            logger.error("Error creating index {}", indexName);
            logger.error(e.getMessage());
        }
        ObjectMapper objectMapper = new ObjectMapper();
        Set<String> interestSet = null;
        Set<Address> addressSet = null;
        Set<Telephone> telephoneSet = null;
        Customer customer = new Customer("1", "開発者ジャワ", "Melo", "title", "Test Writer", "test@gmail.org", interestSet, addressSet, telephoneSet);
        try {
            String data = objectMapper.writeValueAsString(customer);
            logger.debug("Sending index indexName={} indexType={} id={}", new Object[]{indexName, indexName, customer.getId()});
            client.prepareIndex(indexName, indexName)
                    .setId(customer.getId())
                    .setSource(data)
                    .execute().actionGet();

        } catch (IOException e) {
            logger.error("Error sending Index {}", e);
        }
        Thread.sleep(900);
        SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client);
        BoolQueryBuilder qb = QueryBuilders.boolQuery();
        QueryStringQueryBuilder queryBuilder = QueryBuilders
                .queryString("ジャワ")
                .defaultField("firstName")
                .defaultOperator(QueryStringQueryBuilder.Operator.AND);
        qb.minimumShouldMatch("1");
        qb.must(queryBuilder);
        searchRequestBuilder.setQuery(qb);
        SearchResponse response = searchRequestBuilder
                .setIndices(indexName)
                .execute()
                .actionGet();
       try {
            DeleteIndexResponse deleteIndexResponse = client.admin().indices().delete(new DeleteIndexRequest(indexName)).actionGet();

            logger.debug("Delete index response={}", ToStringBuilder.reflectionToString(deleteIndexResponse));
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
            Assert.assertEquals(1, response.getHits().totalHits());
        }
    }
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:46pm UTC](https://discuss.elastic.co/t/how-to-set-a-analyser-for-all-the-fields-and-the--all-field/31498/6 "2017-07-05T23:46:25Z")

</div>


