如何使用 Spring-data-elastic 查询 Elastic [英] How to query Elastic with Spring-data-elastic

查看:40
本文介绍了如何使用 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 表(名字、姓氏)中的一些记录,并且我希望能够允许高级搜索.例如,如果我的名字是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
    ...

 }

目前我正在使用 spring-data-elastic ElasticRepository 进行搜索,如果我要允许高级搜索,我相信可能需要更改.一种选择是直接在 UserService 中使用 EntityManagerTemplate,但是我还不确定如何去做.

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.

谢谢!

推荐答案

首先,不支持自动生成模糊查询,就文档告诉

First, there is not support for automatic fuzzy query generation, as far as the documentation tells

所以我们必须使用自定义存储库方法添加.

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

假设您的基础存储库是

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

您必须创建一个自定义存储库接口来添加您的自定义方法(这就是 标准 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);
}

并让你的历史repo实现这个接口,即:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆