TermQuery upgrade

Hi, I'm working through an upgrade and have an existing function using List for values in a TermQueryBuilder.
I've found online a method to convert the values into a FieldValue, but I'm not sure how to handle the case when Object is passed (due to how the code might send in any data type).

Any guides?

fun getTermFilter(searchType: SearchType?, field: String, values: List<Object>): Query? {
    if (values.isEmpty()) return null

    val valuesToUse = TermsQueryField.Builder()
        .value(values.stream().map(FieldValue::of).toList())
        .build()
    val builder = TermsQuery.of { t -> t
        .field(field)
        .terms(valuesToUse) }._toQuery()

Hi!
I think you got it, putting everything together in a search request should look something like:

        var list = new ArrayList<Object>();
        SearchRequest.of(s -> s
            .index("test")
            .query(q -> q
                .terms(t -> t
                    .field("field")
                    .terms(tt -> tt.value(list.stream().map(FieldValue::of).toList())))));
1 Like

Thanks for replying @ltrotta - but the issue is still that this bit of code fails, as
FieldValue::of can't be resolved, since FieldValue doesn't operate on an Object (which is in the list).
?

FieldValue does operate on Objects, it converts them directly to Json, so they need to be serializable for it to work.

@ltrotta would you have an example please?
Thanks!