I did not find the relevant information in the spring data elasticsearch specification about method for one query to find matches in two indexes. The method is necessary to get the results of matching the query with:

  • movie titles in the same index;
  • names of actors and directors in another index. Thus, for one request, it needs to get a list of films, actors, directors, if there are corresponding matches. Simplified example of query in Postman I have in mind is below: GET: localhost:9200/index1,index2/_search
{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "title": "smth"
                    }
                },
                {
                    "match": {
                        "name": "smth"
                    }
                }
            ]
        }
    }
}
T

This query produces the expected result. But the question is complicated by the need to apply a filter on other fields to the first index, containing the requirements "filter" for some fields and "should" for others.

For the query I applied ElasticsearchOperations and the query below (works for one index only):

   Query query = NativeQuery.builder()
            .withQuery(q -> q
                .bool(b ->
                        {
                            if (titleInLowercase.equals("")) {
                                b.should(m -> m
                                        .match(ma -> ma
                                                .field("title")
                                                .query(**searchInLowercase**)
                                        )
                                );
//                                b.should(m -> m
//                                        .match(ma -> ma
//                                                .field("actors.actorName")
//                                                .query(**searchInLowercase**)
//                                        )
//                                );
//                                b.should(m -> m
//                                        .match(ma -> ma
//                                                .field("directors.directorName")
//                                                .query(**searchInLowercase**)
//                                        )
//                                );
                            }
                            b.should(m -> m
                                    .term(ma -> ma
                                            .field("language_id.uuidLanguage")
                                            .value("uuid")));
                            b.should(m -> m
                                    .term(ma -> ma
                                            .field("countries.uuidCountry")
                                            .value("uuid")));
                            b.should(m -> m
                                    .term(ma -> ma
                                            .field("genres.uuidGenre")
                                            .value("uuid")));
                            b.filter(m -> m
                                    .term(t -> t
                                            .field("age")
                                            .value(ref.getInt("age"))));
                            b.filter(m -> m
                                    .term(t -> t
                                            .field("is_popular")
                                            .value(ref.getInt("is_popular"))));
                            return b;
                        }
                            )
            )
            .withSort(sortParam.get())
            .withPageable(Pageable.ofSize(pageSize))
            .build();

        SearchHits<Film> filmSearchHits = operations.search(query, Film.class, FILM_INDEX_COORDINATES);
        List<Film> films = new ArrayList<>();
        for (SearchHit<Film> searchHit : filmSearchHits) {
            Film film = searchHit.getContent();
            films.add(film);
        }
        return films;

The commented part of the code needs to be applied to another index. The fields for which I make the query "query(search In Lower case)" are "flattened"

I would appreciate yours help.

0

There are 0 best solutions below