本文介绍了如何用Spring-data-elastic查询Elastic的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Elastic和spring-data-elastic的新手.我一直在这里和网络的其他区域进行搜索,但到目前为止,仍找不到我的问题的答案.我希望SO能够提供帮助.

I am new to Elastic and spring-data-elastic. I am been searching here and other areas of the web, but so far have been unable to find the answer to my question. I am hoping SO might be able to help.

我正在索引 Users 表中的某些记录(firstName,lastName),并且希望能够进行高级搜索.因此,例如,如果我的名字是"Frances",并且输入"Frank",那么系统足够聪明,可以返回记录.'Robinson'和'Robinsen'等也一样.

I am indexing some records from my Users table (firstName, lastName) and I am looking to be able to allow advanced searching. So for example if I have the name 'Frances' and I enter 'Frank' then the system is smart enough to return the record. Same for 'Robinson' and 'Robinsen', etc.

我将POJO设置为以下内容:

I've setup my POJO to be the following:

Public Users {
    @Field(fieldType = FieldType.String)
    private String firstName;

    @Field(fieldType = FieldType.String)
    private String lastName

    // mutators
    ...

 }

目前,我正在使用弹簧数据弹性的 ElasticRepository 进行搜索,我认为如果允许高级搜索,可能必须更改.一种选择是直接在 UserService 中使用 EntityManager Template ,但是我不确定该怎么做

Currently I am using a spring-data-elastic ElasticRepository to do my searching, which I believe will probably have to change if I am going to allow for advanced searching. One option would be to use the EntityManager or Template directly in the UserService, however I'm not sure how to go about doing that just yet.

正如我描述的问题,这是索引问题还是搜索问题,或者可能两者兼而有之?我不是在找任何人做这项工作,只是为了指出正确的方向.

As I've described the problem, is this an indexing issue or a searching issue or possibly both? I'm not looking for anyone to do the work, just to point me in the right direction.

谢谢!

推荐答案

首先,就文档说明

因此,我们将不得不使用自定义存储库方法.

So we'll have to use custom repository methods additions.

说您的基本存储库是

@Repository
public interface UserRepository extends CrudRepository<UserEntity, Long>

您必须创建一个Custom仓库接口才能添加您的自定义方法(这全部是标准Spring数据,没什么特别的)

You'll have to create a Custom repository interface to add you custom method (this is all standard Spring data, nothing particular)

public interface UserRepositoryCustom {
    public List<User> findFuzzyByLastNameAndFirstName(String firstName, String lastName);
}

并使您的历史存储库实现此接口,即:

And make your historic repo implement this interface, that is :

@Repository
public interface UserRepository extends CrudRepository<UserEntity, Long>, UserRepositoryCustom

现在,您将需要以某种方式实现自定义"界面.这很容易(再次参阅手册,您必须遵守命名方案,以便Spring可以在运行时连接接口和实现):

Now, you'll need to implement your "custom" interface somehow. This is easy (once again see the manual, you have to respect naming schemes so that Spring can wire interfaces and implementations at run time):

public class UserRepositoryCustomImpl implements UserRepositoryCustom {
    @Autowired protected ElasticsearchTemplate elasticsearchTemplate;

    public List<User> findFuzzyByLastNameAndFirstName(String firstName, String lastName) {
        Criteria c = new Criteria("firstName").fuzzy(firstName).and(new Criteria("lastName").fuzzy(lastName));
        return elasticsearchTemplate.queryForList(new CriteriaQuery(c), CandidateEntity.class);
    }
}

重新编译,重新启动,您应该能够使存储库像这样进行模糊搜索.

Recompile, relaunch, and you should be able to have your repository do the fuzzy search like so.

然后再次(请参阅问题注释),您可能还希望将查询定义为字符串,并且不需要自定义实现.这取决于您.

Then again (see the questions comments), you might also want to define the query as a String and you wouldn't need custom implementations. This is up to you.

这篇关于如何用Spring-data-elastic查询Elastic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 02:47