ES整合JAVA实现
前面我们对ES基本概念和API的基本使用进行大致的说明,有兴趣的朋友可以参考一下专栏
ES专栏
这一章我们使用java来整合es查询
1.数据处理
我们对nba中国官网的数据进行爬取并处理成表。
并通过逆向工程生成基本的service、mapper等文件。
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("nba_player")
public class NbaPlayer implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@TableField("countryEn")
private String countryEn;
@TableField("teamName")
private String teamName;
@TableField("birthDay")
private Long birthDay;
private String country;
@TableField("teamCityEn")
private String teamCityEn;
private String code;
@TableField("displayAffiliation")
private String displayAffiliation;
@TableField("displayName")
private String displayName;
@TableField("schoolType")
private String schoolType;
@TableField("teamConference")
private String teamConference;
@TableField("teamConferenceEn")
private String teamConferenceEn;
private String weight;
@TableField("teamCity")
private String teamCity;
@TableField("playYear")
private Integer playYear;
@TableField("jerseyNo")
private String jerseyNo;
@TableField("teamNameEn")
private String teamNameEn;
private Integer draft;
@TableField("displayNameEn")
private String displayNameEn;
@TableField("birthDayStr")
private String birthDayStr;
@TableField("heightValue")
private BigDecimal heightValue;
private String position;
private Integer age;
@TableField("playerId")
private String playerId;
}
2.添加依赖
这里依赖的版本最好是与你安装的es版本相同
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.4.2</version>
</dependency>
3.测试ES功能
3.1导入一条数据并获取
@Test
public void addPlayer() throws IOException {
NbaPlayer player = new NbaPlayer();
player.setId(999);
player.setDisplayName("迪丽热巴");
nbaPlayerService.addPlayer(player,"999");
}
@Test
public void getPlayer() throws IOException {
final Map<String, Object> player = nbaPlayerService.getPlayer("999");
System.out.println(player);
}
3.2修改数据
@Test
public void updatePlayer() throws IOException {
final Map<String, Object> player = nbaPlayerService.getPlayer("999");
System.out.println(player);
final NbaPlayer nbaPlayers = JSONObject.parseObject(JSON.toJSONString(player),NbaPlayer.class);
nbaPlayers.setAge(18);
nbaPlayerService.updatePlayer(nbaPlayers,"999");
final Map<String, Object> newPlayer = nbaPlayerService.getPlayer("999");;
System.out.println(newPlayer);
}
3.3删除数据
@Test
public void deletePlayer() throws IOException {
nbaPlayerService.deletePlayer("999");
}
3.4删除全部文档
@Test
public void deleteAllPlayer() throws IOException {
nbaPlayerService.deleteAllPlayer();
}
3.5导入全部文档
@Test
public void importAllPlayer() throws IOException {
nbaPlayerService.importAllPlayer();
}
业务逻辑
接口
public interface INbaPlayerService extends IService<NbaPlayer> {
boolean addPlayer(NbaPlayer player, String id) throws IOException;
Map<String, Object> getPlayer(String id) throws IOException;
boolean updatePlayer(NbaPlayer nbaPlayer, String id) throws IOException;
boolean deletePlayer(String id) throws IOException;
boolean deleteAllPlayer() throws IOException;
boolean importAllPlayer() throws IOException;
List<NbaPlayer> searchMatch(String key,String value) throws IOException;
List<NbaPlayer> searchTerm(String key, String value) throws IOException;
}
实现逻辑
@Service
@RequiredArgsConstructor
public class NbaPlayerServiceImpl extends ServiceImpl<NbaPlayerMapper, NbaPlayer> implements INbaPlayerService {
private final RestHighLevelClient restHighLevelClient;
private final static String NBA_INDEX = "nba_latest";
@Override
public boolean addPlayer(NbaPlayer player, String id) throws IOException {
IndexRequest request = new IndexRequest(NBA_INDEX).id(id).source(beanToMap(player));
final IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
System.out.println(JSONObject.toJSON(response));
return false;
}
@Override
public Map<String, Object> getPlayer(String id) throws IOException {
GetRequest request = new GetRequest(NBA_INDEX,id);
final GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
return response.getSource();
}
@Override
public boolean updatePlayer(NbaPlayer nbaPlayer, String id) throws IOException {
UpdateRequest request = new UpdateRequest(NBA_INDEX,id).doc(beanToMap(nbaPlayer));
final UpdateResponse response = restHighLevelClient.update(request, RequestOptions.DEFAULT);
System.out.println(JSONObject.toJSON(response));
return false;
}
@Override
public boolean deletePlayer(String id) throws IOException {
DeleteRequest request = new DeleteRequest(NBA_INDEX,id);
final DeleteResponse delete = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
return false;
}
@Override
public boolean deleteAllPlayer() throws IOException {
DeleteByQueryRequest request = new DeleteByQueryRequest(NBA_INDEX);
final BulkByScrollResponse response = restHighLevelClient.deleteByQuery(request, RequestOptions.DEFAULT);
return false;
}
@Override
public boolean importAllPlayer() throws IOException {
for (NbaPlayer player : list()) {
addPlayer(player,String.valueOf(player.getId()));
}
return false;
}
@Override
public List<NbaPlayer> searchMatch(String key,String value) throws IOException {
SearchRequest request = new SearchRequest(NBA_INDEX);
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.query(QueryBuilders.matchQuery(key,value));
builder.from(0);
builder.size(100);
request.source(builder);
final SearchResponse search = restHighLevelClient.search(request, RequestOptions.DEFAULT);
List<NbaPlayer> playerList = new LinkedList<>();
for (SearchHit hit : search.getHits().getHits()) {
playerList.add(JSONObject.parseObject(hit.getSourceAsString(),NbaPlayer.class));
}
return playerList;
}
@Override
public List<NbaPlayer> searchTerm(String key,String value) throws IOException {
SearchRequest searchRequest = new SearchRequest(NBA_INDEX);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.termQuery(key,value));
searchSourceBuilder.from(0);
searchSourceBuilder.size(100);
searchRequest.source(searchSourceBuilder);
SearchResponse response = restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);
System.err.println(JSONObject.toJSON(response));
final SearchHit[] hits = response.getHits().getHits();
List<NbaPlayer> list = new LinkedList<>();
for (SearchHit hit : hits) {
NbaPlayer player = JSONObject.parseObject(hit.getSourceAsString(),NbaPlayer.class);
list.add(player);
}
return list;
}
Map beanToMap(NbaPlayer player){
return JSON.parseObject(JSON.toJSONString(player),Map.class);
}
}
|