3

I have this query:

NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(multiMatchQuery(searchPattern)
                        .field("fullName")
                        .field("npi")
                        .type(MultiMatchQueryBuilder.Type.BEST_FIELDS))
                .build();

How can I run it? All methods in the ElasticsearchRepository are deprecated:

public interface ElasticsearchRepository<T, ID> extends PagingAndSortingRepository<T, ID> {
    /** @deprecated */
    @Deprecated
    default <S extends T> S index(S entity) {
        return this.save(entity);
    }

    /** @deprecated */
    @Deprecated
    <S extends T> S indexWithoutRefresh(S var1);

    /** @deprecated */
    Iterable<T> search(QueryBuilder var1);

    /** @deprecated */
    Page<T> search(QueryBuilder var1, Pageable var2);

    /** @deprecated */
    Page<T> search(Query var1);

    Page<T> searchSimilar(T var1, @Nullable String[] var2, Pageable var3);

    /** @deprecated */
    @Deprecated
    void refresh();
}
Pavel Petrashov
  • 1,073
  • 1
  • 15
  • 34

1 Answers1

2

The Javadoc for the deprecated methods states that you should

  • either define the queries by using the standard method name derivation like findByName
  • or use the @Query annotation wit a query string
  • or don not use the repository interface but the ElasticsearchOperations to pass your custom Query derived queries.

We deprecated the methods from the repository interface that use the Queryclasses. Not only in Spring Data Elasticsearch, but we do not intorduce them in other modules as well; see the comment on this Spring Data Mongo issue for more information.

P.J.Meisch
  • 18,013
  • 6
  • 50
  • 66
  • Ok. I need to get this query:`"fields" : [ "fullName^1.0", "npi^1.0" ],` How can do it with `@Query` annotation or `ElasticsearchOperations` ? – Pavel Petrashov Nov 24 '20 at 11:57
  • 1
    In ElasticsearchOperations Almost all methods are deprecated too. I didn't find examples(only old examples about NativeSearchQuery) – Pavel Petrashov Nov 24 '20 at 11:59
  • `ElasticsearchOperations`has `search` methods that take a `Query` as parameter. Which methods in`ElasticsearchOperations` are deprecated? – P.J.Meisch Nov 24 '20 at 12:12
  • `org.springframework.data.elasticsearch.core.ElasticsearchOperations` doesn't have `search` method. I do not understand what class you suggest – Pavel Petrashov Nov 24 '20 at 18:37
  • `ElasticsearchOperations` extends `SearchOperations` and therefore has this method: https://github.com/spring-projects/spring-data-elasticsearch/blob/b17999f8fa86e938c175b958c46f85b41acd79ed/src/main/java/org/springframework/data/elasticsearch/core/SearchOperations.java#L332-L340. So you let inject an `ElasticsearchOperations` object and use it's `search`method. – P.J.Meisch Nov 24 '20 at 19:57