我对Elasticseach完全陌生,并且还通过遵循以下两个步骤进行了一些虚拟应用程序->
1>使用 Spring数据elasticsearch (不需要在后台运行elasticsearch),只需导入 ElasticsearchRepository ,即可进行分类操作。
但是在这里,我面临一个问题-我需要为每种类型的数据(假设是UserModel和UserAddressModel)使用一些字段(将是静态的)创建不同的模型类。因此,如果以后需要添加该类型的新数据,也需要在模型类中添加该字段。
那么,我们可以使其动态化吗?
2>在另一个应用程序中,我使用了 JAVA TransportClient ,通过它我可以进行原始操作并可以保存任何数据(此处不使用模型),并且还可以动态添加新字段。 / p>
因此,我很困惑,哪种方法是根据生产水平的性能明智的方法进一步进行的最佳方法?还是两者都一样?
使用 TransportClient :
var labels = document.getElementsByClassName('some-class-name');
var textContent_criteria = ['Description', 'Category', 'Department', 'Justification'];
Array.from(labels).filter(label => textContent_criteria.some(criteria => label.textContent.includes(criteria)));
使用 Springdata Elasticsearch :
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();
}
}
数据加载器:
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;
}
答案 0 :(得分: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版本的能力可能会受到影响。