使用JAVA在我的项目中实现Elasticsearch的最佳方法是什么? [关闭]

问题描述 投票:0回答:1

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

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

但在这里我面临一个问题 - 我需要为每种类型的数据(比如说UserModel和UserAddressModel)创建不同的模型类和一些字段(这将是静态的)。所以,如果以后我需要在该类型中添加新数据,我还需要在模型类中添加该字段。

那么,我们可以做那个动态???

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();
}
}

使用Springdata 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 spring elasticsearch spring-data-elasticsearch
1个回答
1
投票

如果您有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无关。如果您有动态映射,则可以考虑使用动态模板并取消提供字段映射。

在过去,Spring ES项目在追赶ES版本方面有点慢,因此请注意,如果使用Spring数据ES,则升级ES版本的能力可能会受到影响。

© www.soinside.com 2019 - 2024. All rights reserved.