Kotlin and Elasticsearch (help! I'm newbie)

So, I am a total newbie. I have a lot of Jsons in Elasticsearch. And the query I have to do is very simple for me to understand in a traditional database, but I can not understand how to do a subquery here.

My situation is the next:
All my Jsons have a field of text. FIRST, I have to filter all the jsons that have X, Y or Z keywords in this field. And then the user ask to filter this list and show the ones that have S or T in the field.
So obviously, I can not do a plain query of filter ALL the jsons that have x, y, z, s or t because that would not comply. What I need is to do a subquery of the first result, but I haven’t been able to do this.
(it also filters by words that does not want inside the field, but this filter works perfectly)

I write down the code I have until now. I hope someone can help me.
THANKS!!! :sweat_smile:
–excludedKeywords → are the keywords you don’t want that appear on the text
–keywords → the first original keywords
–keywordsA-> the second set of keywords

   private fun generateTermsQueryA(excludedKeywords: List<String>, keywords: List<String>, keywordsA: List<String>, field: String = "unifiedText"): BoolQueryBuilder? {
        val excludedTerms = arrayListOf<String>()
        var excludedSubquery = BoolQueryBuilder()
        LOG.info("generateTermsQueryA - field : {}", field)
        excludedKeywords.forEach {
            if (it.contains(" ")) excludedSubquery = excludedSubquery.mustNot(QueryBuilders.matchPhraseQuery(field, it))
            else excludedTerms.add(it)
        if (excludedTerms.isNotEmpty()) {
            excludedSubquery = excludedSubquery.mustNot(QueryBuilders.termsQuery(field, excludedTerms))

        val includedTerms = arrayListOf<String>()
        var includedSubquery = BoolQueryBuilder()
        keywords.forEach {
            if (it.contains(" ")) includedSubquery = includedSubquery.should(QueryBuilders.matchPhraseQuery(field, it))
            else includedTerms.add(it)
        if (includedTerms.isNotEmpty()) {
            includedSubquery = includedSubquery.should(QueryBuilders.termsQuery(field, includedTerms))

        val includedTermsA = arrayListOf<String>()
        var includedSubqueryA = BoolQueryBuilder()
        keywordsA.forEach {
            if (it.contains(" ")) includedSubqueryA = includedSubqueryA.must(QueryBuilders.matchPhraseQuery(field, it))
            else includedTermsA.add(it)
        if (includedTermsA.isNotEmpty()) {
            includedSubqueryA = includedSubqueryA.should(QueryBuilders.termsQuery(field, includedTerms))

        val totalQuery = excludedSubquery.must(includedSubquery).must(includedSubqueryA)

        return totalQuery