# Search as you type with Java API

**URL:** <https://discuss.elastic.co/t/search-as-you-type-with-java-api/25683>\
**Category:** Elasticsearch\
**Created:** [July 16, 2015, 4:04am UTC](https://discuss.elastic.co/t/search-as-you-type-with-java-api/25683 "2015-07-16T04:04:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![elasticsearcher1](https://avatars.discourse-cdn.com/v4/letter/e/7ea924/32.png) [@elasticsearcher1](https://discuss.elastic.co/u/elasticsearcher1)\
**Post date:** [July 16, 2015, 4:04am UTC](https://discuss.elastic.co/t/search-as-you-type-with-java-api/25683/1 "2015-07-16T04:04:55Z")

</div>

2 Questions.

What is the equivalent of match\_phrase\_prefix in the Java API? I'm unable to find any classes that implement QueryBuilder that correspond to this?

I'm trying to set a custom analyzer for an index to also try search as you type during index time, but I'm having issues. I'm unable to change the analyzer setting for my index. Right now I'm giving it a json object with the settings, but it isn't going through. Could anyone provide an example on how to do it? I've copied this template: [http://stackoverflow.com/questions/6275727/define-custom-elasticsearch-analyzer-using-java-api](http://stackoverflow.com/questions/6275727/define-custom-elasticsearch-analyzer-using-java-api) but it doesn't appear to work.

Thanks.

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [July 16, 2015, 7:46am UTC](https://discuss.elastic.co/t/search-as-you-type-with-java-api/25683/2 "2015-07-16T07:46:17Z")

</div>

For Question 1:

Try this:

```java
org.elasticsearch.index.query.QueryBuilders.matchPhrasePrefixQuery(String name, Object text)

```

Where `name` is the field name and `text` is query text (to be analyzed).

For Question 2:

Can you paste a code snippet of the exact code you are using? Also when you say:

> [@elasticsearcher1](#):
>
> but it doesn't appear to work.

What do you mean? Are you getting an error in the response or in the Elasticsearch logs? If so, what is this error?

---

<div class="post-metadata">

**Author:** ![elasticsearcher1](https://avatars.discourse-cdn.com/v4/letter/e/7ea924/32.png) [@elasticsearcher1](https://discuss.elastic.co/u/elasticsearcher1)\
**Post date:** [July 16, 2015, 4:42pm UTC](https://discuss.elastic.co/t/search-as-you-type-with-java-api/25683/3 "2015-07-16T16:42:07Z")

</div>

Thank you.

I'm creating an index like this. Using the settings from [https://www.elastic.co/guide/en/elasticsearch/guide/current/\_index\_time\_search\_as\_you\_type.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/_index_time_search_as_you_type.html)

```
            client.admin().indices().prepareCreate(indexName)
                .setSettings(ImmutableSettings.settingsBuilder().loadFromSource(jsonBuilder()
                        .startObject()
                        .startObject("analysis")
                        .startObject("analyzer")
                        .startObject("autocomplete")
                        .field("type", "custom")
                        .field("tokenizer", "standard")
                        .field("filter", new String[]{"lowercase", "autocomplete_filter"})
                        .endObject()
                        .endObject()
                        .startObject("filter")
                        .startObject("autocomplete_filter")
                        .field("type", "edge_ngram")
                        .field("min_gram", "1")
                        .field("max_gram", "20")
                        .endObject()
                        .endObject()
                        .endObject()
                        .endObject().string()))
                .execute().actionGet();

```

Adding a document

```
        String json = "{" +
            "\"user\":\"kimchy\"," +
            "\"postDate\":\"2013-01-30\"," +
            "\"message\":\"trying out Elasticsearch\"" +
            "}";

    client.prepareIndex(indexName, "tweet", "1")
            .setSource(json)
            .execute()
            .actionGet();

```

And I check the tokens for the analyzer

```
    AnalyzeResponse analyzeResponse = client.admin().indices().prepareAnalyze("trying out").execute().actionGet();
    for (AnalyzeResponse.AnalyzeToken analyzeToken : analyzeResponse.getTokens()) {
        System.out.println(analyzeToken.getTerm());
    }

```

I'm expecting to receive  
t  
tr  
try  
tryi  
tryin  
trying  
o  
ou  
out

but all I receive as tokens are "trying" and "out." I can't figure out how to setup the analyzer correctly.

I'm also wondering how I can do fuzzy queries with matchPhrasePrefixQuery. It seems like the fuzziness parameter doesn't work. Do I have to modify the search analyzer such that for every letter typed it tokenizes the current token with some degree of fuzziness?  
Or would it be better to break down the words at index time using the above analyzer and use a fuzzy multimatchquery?

Thanks again for the help!

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [July 16, 2015, 8:20pm UTC](https://discuss.elastic.co/t/search-as-you-type-with-java-api/25683/4 "2015-07-16T20:20:45Z")

</div>

Have you set the field(s) in your mapping to use the analyzer `autocomplete`?

In your analyze code you haven't specified the analyzer to use so it will use the standard analyzer. try the following

```java
    AnalyzeResponse analyzeResponse = client.admin().indices().prepareAnalyze("trying out").setAnalyzer("autocomplete").execute().actionGet();
    for (AnalyzeResponse.AnalyzeToken analyzeToken : analyzeResponse.getTokens()) {
        System.out.println(analyzeToken.getTerm());
    }

```

---

<div class="post-metadata">

**Author:** ![elasticsearcher1](https://avatars.discourse-cdn.com/v4/letter/e/7ea924/32.png) [@elasticsearcher1](https://discuss.elastic.co/u/elasticsearcher1)\
**Post date:** [July 17, 2015, 1:14am UTC](https://discuss.elastic.co/t/search-as-you-type-with-java-api/25683/5 "2015-07-17T01:14:52Z")

</div>

That seemed to work. Thanks.

Is there a way to map it to all fields and types in the index by default?

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [July 17, 2015, 7:24am UTC](https://discuss.elastic.co/t/search-as-you-type-with-java-api/25683/6 "2015-07-17T07:24:06Z")

</div>

Take a look at: [https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-root-object-type.html#\_dynamic\_templates](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-root-object-type.html#_dynamic_templates)

You can use dynamic templates in your mapping to set rules for fields so based on the fields name pattern and/or type it gets a particular mapping

---

<div class="post-metadata">

**Author:** ![elasticsearcher1](https://avatars.discourse-cdn.com/v4/letter/e/7ea924/32.png) [@elasticsearcher1](https://discuss.elastic.co/u/elasticsearcher1)\
**Post date:** [July 21, 2015, 6:21pm UTC](https://discuss.elastic.co/t/search-as-you-type-with-java-api/25683/7 "2015-07-21T18:21:29Z")

</div>

Thank you for the help, but I was unable to get that working.

What I'm doing at the moment is

```
           client.admin().indices().preparePutMapping(indexName)
                //set all the types in the index to adhere to this mapping, "_default_"
                .setType("_default_")
                .setSource(jsonBuilder()

                        //apply analyzer to all fields in the type. This is deprecated
                        .startObject()
                        .field("index_analyzer", "autocomplete")
                        .field("search_analyzer", "standard")
                        .endObject())

```

But I am reading that doing it like this is deprecated.

I tried this but was unable to get it to work. Could anything go wrong with using that specific deprecated feature? For all intents and purposes, it's working fine at the moment.

```
            client.admin().indices().preparePutMapping(indexName)
                //set all the types in the index to adhere to this mapping, "_default_"
                .setType("_default_")
                .setSource(jsonBuilder()

                                                            .startObject()
                                                            .startObject("dynamic_templates")
                                                            .startArray()
                                                            .startObject("template1")
                                                            .startObject()
                                                            .field("match", "*")
                                                            .startObject("mapping")
                                                            .field("type", "string")
                                                            .field("index_analyzer", "autocomplete")
                                                            .field("search_analyzer", "standard")
                                                            .endObject()
                                                            .endObject()
                                                            .endObject()
                                                            .endArray()
                                                            .endObject()
                                                            .endObject())
```

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [July 22, 2015, 7:34am UTC](https://discuss.elastic.co/t/search-as-you-type-with-java-api/25683/8 "2015-07-22T07:34:52Z")

</div>

The documentation indicates that the body of the request should start with the type name. Try this instead:

```json
client.admin().indices().preparePutMapping(indexName)
                //set all the types in the index to adhere to this mapping, "_default_"
                .setType("_default_")
                .setSource(jsonBuilder()

                                                            .startObject()
                                                            .startObject("_default_")
                                                            .startObject("dynamic_templates")
                                                            .startArray()
                                                            .startObject("template1")
                                                            .startObject()
                                                            .field("match", "*")
                                                            .startObject("mapping")
                                                            .field("type", "string")
                                                            .field("index_analyzer", "autocomplete")
                                                            .field("search_analyzer", "standard")
                                                            .endObject()
                                                            .endObject()
                                                            .endObject()
                                                            .endArray()
                                                            .endObject()
                                                            .endObject()
                                                            .endObject())

```

---

<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 6, 2017, 12:00am UTC](https://discuss.elastic.co/t/search-as-you-type-with-java-api/25683/9 "2017-07-06T00:00:02Z")

</div>


