Hybrid search by using knn and query in java client

Hello.
I want the hybrid search by using both the knn option and a query on the page below with java client.

With spring-boot 3.1.5 and elasticsarch-java 8.7.1 environment,
I made some codes as below, but seems like .withKnnQuery(knnQuery) condition is not processed. Is there any way?

MatchQuery matchQuery = new MatchQuery.Builder().field(textfield).query(searchText).boost(1.0f).build();
BoolQuery boolQuery = new BoolQuery.Builder().must(List.of(matchQuery._toQuery())).build();

TermsQuery termsQuery = new TermsQuery.Builder().field("ownerNm").terms(new TermsQueryField.Builder().value(List.of(FieldValue.of("luis"))).build()).build();
KnnQuery knnQuery = new KnnQuery.Builder()
					.field("text_VEC")
					.queryVector(queryEmbedding)
					.numCandidates(100)
					.k(10)
					.boost(10.0f)
					.filter(List.of(termsQuery._toQuery()))
					.build();
		
Query searchQuery = NativeQuery.builder()
				.withKnnQuery(knnQuery)
				.withQuery(boolQuery._toQuery())
				.withSearchType(null)
				.withMaxResults(5)
				.build();

SearchHits<TetstIndex> searchHits = operations.search(searchQuery, TestIndex.class);

Hi @wshan13 .

You can try replace this:

for this:

   SearchRequest sr = SearchRequest.of(s -> s
        .index("idx")
        .query(boolQuery._toQuery())
        .knn(knnQuery)
    );

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