使用 JAVA 在我的项目中实施 Elasticsearch 的最佳方式是什么?

what is the best way to proceed for impementing Elasticsearch in my project using JAVA?

我是 elasticseach 的新手,我也通过以下两个过程制作了一些虚拟应用程序->

1>使用Spring数据elasticsearch(其中我不需要运行 elasticsearch在后台),只需导入ElasticsearchRepository我可以做crud操作

但我面临一个问题——我需要为每种类型的数据(比如 UserModel 和 UserAddressModel)制作不同的模型 classes,其中包含一些字段(这将是静态的)。因此,如果稍后我需要添加该类型的新数据,我还需要在模型中添加该字段 class。

那么,我们可以让它动态化吗???

2>在另一个应用程序中,我使用了 JAVA TransportClient,通过它我可以进行 crud 操作并可以保存任何数据(这里不使用模型)和我也可以动态添加新字段。

所以,我很困惑,根据生产水平的性能,哪种方法是进一步推进的最佳方式?或者两者相同?

使用 TransportClient:

public class UserResource {

TransportClient client;

public UserResource() throws UnknownHostException {
    client = new PreBuiltTransportClient(Settings.EMPTY)
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

}

@GetMapping("/insert/{id}")
public String insert(@PathVariable final String id) throws IOException {

    IndexResponse response = client.prepareIndex("employee", "id", id)
            .setSource(jsonBuilder()
                    .startObject()
                    .field("fname", "peter")
                    .field("lname", "parker")
                    .field("salary", 1200)
                    .field("teamName", "Development")
                    .endObject()
            )
            .get();
    return response.getResult().toString();
}


@GetMapping("/view/{id}")
public Map<String, Object> view(@PathVariable final String id) {
    GetResponse getResponse = client.prepareGet("employee", "id", id).get();
    System.out.println(getResponse.getSource());


    return getResponse.getSource();
}

@GetMapping("/update/{id}")
public String update(@PathVariable final String id) throws IOException {

    UpdateRequest updateRequest = new UpdateRequest();
    updateRequest.index("employee")
            .type("id")
            .id(id)
            .doc(jsonBuilder()
                    .startObject()
                    .field("gender", "male")
                    .endObject());
    try {
        UpdateResponse updateResponse = client.update(updateRequest).get();
        System.out.println(updateResponse.status());
        return updateResponse.status().toString();
    } catch (InterruptedException | ExecutionException e) {
        System.out.println(e);
    }
    return "Exception";
}

@GetMapping("/delete/{id}")
public String delete(@PathVariable final String id) {

    DeleteResponse deleteResponse = client.prepareDelete("employee", "id", id).get();

    System.out.println(deleteResponse.getResult().toString());
    return deleteResponse.getResult().toString();
}
}

使用Spring数据 Elasticsearch:

public class SearchQueryBuilder {

@Autowired
private ElasticsearchTemplate elasticsearchTemplate;


public List<Users> getAll(String text) {

    QueryBuilder query = QueryBuilders.boolQuery()
            .should(
                    QueryBuilders.queryStringQuery(text)
                            .lenient(true)
                            .field("name")
                            .field("teamName")
            ).should(QueryBuilders.queryStringQuery("*" + text + "*")
                    .lenient(true)
                    .field("name")
                    .field("teamName"));

    NativeSearchQuery build = new NativeSearchQueryBuilder()
            .withQuery(query)
            .build();

    List<Users> userses = elasticsearchTemplate.queryForList(build, Users.class);

    return userses;
}

数据加载器:

public class Loaders {

@Autowired
ElasticsearchOperations operations;

@Autowired
UsersRepository usersRepository;

@Autowired
VideoRepository videoRepository;

@PostConstruct
@Transactional
public void loadAll(){


    operations.putMapping(Users.class);
    operations.putMapping(Videos.class);
    usersRepository.save(getData());
    videoRepository.save(getVideoData());

}

private List<Users> getData() {
    List<Users> userses = new ArrayList<>();
    userses.add(new Users("peter",123L, "Accounting", 12000L));

    return userses;
}

用户型号:

@Document(indexName = "new", type = "users", shards = 1)
public class Users {

private String name;
private Long id;
private String teamName;
private Long salary;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getTeamName() {
    return teamName;
}

public void setTeamName(String teamName) {
    this.teamName = teamName;
}

public Long getSalary() {
    return salary;
}

public void setSalary(Long salary) {
    this.salary = salary;
}

用户存储库:

public interface UsersRepository extends ElasticsearchRepository<Users, Long> {
}

如果您有一个 Java 应用程序,那么高级 Java REST 客户端是最佳选择 (https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.5/index.html)。

请注意,TransportClient 已被弃用,第 8 版将不再支持(请参阅 https://www.elastic.co/guide/en/elasticsearch/client/java-api/master/transport-client.html)。因此,仅考虑这一点就比任何性能考虑都更重要。

静态映射和动态映射之间的选择是一项基本选择 -- 本身与 Spring elasticsearch 无关。如果你有动态映射,你可以考虑使用动态模板并取消提供字段映射。

之前SpringES项目跟上ES版本有点慢,请注意如果使用Spring数据可能会影响升级ES版本的能力ES。