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:
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. My strings seem to always break the API, but I guess with a working example this could be worked around.
The response model PhraseSuggestOption seems to miss the collate_match field.
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".
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?
}
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 .
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?
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?
}
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.