# Phrase query with collate with Elastic Java Client 8.4

**URL:** <https://discuss.elastic.co/t/phrase-query-with-collate-with-elastic-java-client-8-4/315218>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [September 26, 2022, 9:33pm UTC](https://discuss.elastic.co/t/phrase-query-with-collate-with-elastic-java-client-8-4/315218 "2022-09-26T21:33:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Strasser](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peter_strasser/32/99068_2.png) [@Peter\_Strasser](https://discuss.elastic.co/u/Peter_Strasser)\
**Post date:** [September 26, 2022, 9:33pm UTC](https://discuss.elastic.co/t/phrase-query-with-collate-with-elastic-java-client-8-4/315218/1 "2022-09-26T21:33:31Z")

</div>

Hi Elastic Community,

I am looking to run a phrase suggestion with the collate parameter in the Elastic Java Client Version 8.4., but I can not get any working example running.

I currently encounter 2 problems:

1. The collate.query.source parameter takes an argument of type String, which does not seem to work properly. I personally would've expected an argument of type [Query](https://github.com/elastic/elasticsearch-java/blob/main/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/Query.java). My strings seem to always break the API, but I guess with a working example this could be worked around.

2. The response model [PhraseSuggestOption](https://github.com/elastic/elasticsearch-java/blob/main/java-client/src/main/java/co/elastic/clients/elasticsearch/core/search/PhraseSuggestOption.java) seems to miss the collate\_match field.

Can anyone help me with a working example please?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [September 26, 2022, 10:31pm UTC](https://discuss.elastic.co/t/phrase-query-with-collate-with-elastic-java-client-8-4/315218/2 "2022-09-26T22:31:26Z")

</div>

Hi @Peter_Strasser

I made this example some time ago, I hope it helps you in some way.

```auto
  Map<String, FieldSuggester> map = new HashMap<>();
    map.put("simple_phrase", FieldSuggester.of(fs -> fs.phrase(
        PhraseSuggester.of(ps -> ps
            .field("title.trigram")
            .size(1)
            .directGenerator(DirectGenerator.of(dg -> dg.field("title.trigram").suggestMode(SuggestMode.Always).minWordLength(1)))
            .collate(PhraseSuggestCollate.of(
                c -> c.query(PhraseSuggestCollateQuery.of(psq -> psq
                    .source("{\"match\": {\"{{field_name}}\" : \"{{suggestion}}\"}}")))
                    .params(Map.of("field_name", JsonData.of("title")))
            ))
        )
        )
    ));
    Suggester suggester = Suggester.of(s -> s
        .suggesters(map)
        .text("noble prize")
    );

```

---

<div class="post-metadata">

**Author:** ![Peter\_Strasser](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peter_strasser/32/99068_2.png) [@Peter\_Strasser](https://discuss.elastic.co/u/Peter_Strasser)\
**Post date:** [September 27, 2022, 7:02am UTC](https://discuss.elastic.co/t/phrase-query-with-collate-with-elastic-java-client-8-4/315218/3 "2022-09-27T07:02:21Z")

</div>

Hi @RabBit_BR ,

thanks for your help. I converted your code to kotlin and the query ran through.  
I had to add the highlight option, because the client treats it as a required field, but that's another issue I guess.

This code prints the suggestion for "noble price", as expected. But I can not find the information, of collate\_match is true or false for the response anywhere. Am I using the correct models? I am casting the type with suggestion.phrase() into a "PhraseSuggest".

```auto

val simplePhrase = "simple_phrase"
val map = mapOf<String, FieldSuggester>(
    simplePhrase to
            FieldSuggester.of { fs ->
                fs.phrase(
                    PhraseSuggester.of { ps ->
                        ps
                            .field("title.trigram")
                            .size(1)
                            .highlight { h ->
                                h.preTag("<em>")
                                h.postTag("</em>")
                            }
                            .directGenerator(DirectGenerator.of { dg ->
                                dg.field("title.trigram").suggestMode(SuggestMode.Always).minWordLength(1)
                            })
                            .collate(PhraseSuggestCollate.of { c ->
                                c.query(PhraseSuggestCollateQuery.of { psq ->
                                    psq.source("{\"match\": {\"{{field_name}}\" : \"{{suggestion}}\"}}")
                                }).params(mapOf("field_name" to JsonData.of("title")))
                            })
                    }
                )
            })

val suggester: Suggester = Suggester.of { s ->
    s.suggesters(map)
        .text("noble price")
}

val suggestResponse = esClient.search(SearchRequest.of { sr ->
    sr
        .index(indexName)
        .suggest(suggester)
}, Any::class.java).asDeferred().await()

val suggestion = suggestResponse.suggest()[simplePhrase]?.getOrNull(0) ?: throw IllegalStateException("")

suggestion.phrase().options().forEach { phraseSuggestionOption ->
    println(phraseSuggestionOption.text())
    println(phraseSuggestionOption.highlighted())
    println(phraseSuggestionOption.score())
    // how do I get collate_match information?
}

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [September 27, 2022, 7:17am UTC](https://discuss.elastic.co/t/phrase-query-with-collate-with-elastic-java-client-8-4/315218/4 "2022-09-27T07:17:53Z")

</div>

Add prune = true

```auto
PhraseSuggestCollate.of(
                c -> c.query(PhraseSuggestCollateQuery.of(psq -> psq
                    .source("{\"match\": {\"{{field_name}}\" : \"{{suggestion}}\"}}")))
                    .params(Map.of("field_name", JsonData.of("title")))
                    .prune(true)

```

**[Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-suggesters.html#_basic_phrase_suggest_api_parameters)**:

> Additionally, you can specify a `prune` to control if all phrase suggestions will be returned; when set to `true` the suggestions will have an additional option `collate_match` , which will be `true` if matching documents for the phrase was found, `false` otherwise. The default value for `prune` is `false` .

---

<div class="post-metadata">

**Author:** ![Peter\_Strasser](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peter_strasser/32/99068_2.png) [@Peter\_Strasser](https://discuss.elastic.co/u/Peter_Strasser)\
**Post date:** [September 27, 2022, 7:31am UTC](https://discuss.elastic.co/t/phrase-query-with-collate-with-elastic-java-client-8-4/315218/5 "2022-09-27T07:31:33Z")

</div>

You are right I missed this in the query, thank you.

I am still unsure on how to access this flag from the code after adding this flag. How do I cast it to a model where the collate\_match is included?

Are these lines wrong?

```auto
val suggestion = suggestResponse.suggest()[simplePhrase]?.getOrNull(0) ?: throw IllegalStateException("")

suggestion.phrase().options().forEach { phraseSuggestionOption ->
    println(phraseSuggestionOption.text())
    println(phraseSuggestionOption.highlighted())
    println(phraseSuggestionOption.score())
    // how do I get collate_match information?
}

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [September 27, 2022, 7:56am UTC](https://discuss.elastic.co/t/phrase-query-with-collate-with-elastic-java-client-8-4/315218/6 "2022-09-27T07:56:50Z")

</div>

It looks like the new java api lost this field. In these cases I open an issue in GIT and wait for the fix.

---

<div class="post-metadata">

**Author:** ![Peter\_Strasser](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peter_strasser/32/99068_2.png) [@Peter\_Strasser](https://discuss.elastic.co/u/Peter_Strasser)\
**Post date:** [September 27, 2022, 8:59am UTC](https://discuss.elastic.co/t/phrase-query-with-collate-with-elastic-java-client-8-4/315218/7 "2022-09-27T08:59:51Z")

</div>

I created an issue in the GitHub repo, thank you very much for your help. 🙂

[PhraseSuggestOption missing collate\_match field · Issue #411 · elastic/elasticsearch-java (github.com)](https://github.com/elastic/elasticsearch-java/issues/411)

---

<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:** [October 25, 2022, 9:00am UTC](https://discuss.elastic.co/t/phrase-query-with-collate-with-elastic-java-client-8-4/315218/8 "2022-10-25T09:00:28Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
