spring boot,es版本衆多,經常遇到版本不兼容的問題,現在提供一個兼容的版本springboot-1.5.2.RELEASE,elasticsearch-2.3.5, 密碼:mbzr,官網下載速度很慢,這裏提供一個下載地址。
安裝linux 安裝es:
解壓壓縮包 tar -xvf elasticsearch-2.3.5.tar.gz。
啓動es ./elasticsearch-2.3.5/bin/elasticsearch
訪問 http://localhost:9200/ 看到如下說明啓動成功
es默認只能本機訪問,要取消限制,需修改安裝目錄下/config/elasticsearch.yml文件,network.host: 0.0.0.0
啓動報錯解決:
(1)can not run elasticsearch as root:(es-2.x以上版本,不能使用root用戶啓動)
groupadd es #增加es組
useradd es -g es -p pwd #增加es用戶並附加到es組
chown -R es:es elasticsearch-2.3.5 #給目錄權限
su es #切換到es用戶 ./bin/elasticsearch-2.3.5 -d #後臺運行es
(2)max virtual memory areas vm.max_map_count [65530]is too low, increase to at least [262144]:
修改/etc/sysctl.conf配置文件 添加vm.max_map_count=262144
或者su root
cat /proc/sys/vm/max_map_count
echo 262144 > /proc/sys/vm/max_map_count
(3)max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536]
vi /etc/security/limits.conf
添加如下內容:
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
安裝head插件:
首先安裝node.js
wget https://npm.taobao.org/mirrors/node/latest-v4.x/node-v4.4.7-linux-x64.tar.gz
tar -zxvf node-v4.4.7-linux-x64.tar.gz
配置環境變量:vim /etc/profile
#node.js
export NODE_HOME=/usr/local/node-v4.4.7-linux-x64
export PATH=$PATH:$NODE_HOME/bin
export NODE_PATH=$NODE_HOME/lib/node_modules
執行source /etc/profile 刷新
(npm安裝:curl --silent --location https://rpm.nodesource.com/setup_10.x | bash -)
安裝grunt (head插件需要藉助grunt運行)
npm install -g grunt-cli
檢查是否安裝成功,grunt -version
下載head插件: wget https://github.com/mobz/elasticsearch-head/archive/master.zip
解壓:unzip master.zip
cd /opt/elasticsearch-head-master
npm install --save-dev //執行後會生成node_modules文件夾
修改vim elasticsearch-head-master/Gruntfile.js
修改文件vim elasticsearch-head-master/_site/app.js
grunt server & 後臺啓動
訪問http://ip:9100
出現輸入顯示,vim/elasticsearch-2.3.5/config/elasticsearch.yml,末尾加入
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
http.cors.enabled: true
http.cors.allow-origin: "*"
重啓head插件。
如果單次讀取的行數較多需要修改:(根據實際情況)
index.query.bool.max_clause_count: 10240
下面是spring boot 和elasticsearch整合:
spring boot項目(可以使用idea 創建非常快捷)添加依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>3.0.9</version>
</dependency>
添加es配置:
data:
elasticsearch:
cluster-name: wk-elasticsearch
cluster-nodes: 192.168.0.57:9300
local: false
創建es 實體:
@Document(indexName = "my_index",type = "my_user")
public class UserES implements Serializable{
private static final long serialVersionUID = 6184266621648505324L;
@Field(type = FieldType.Integer)
@Id
private Integer userId;
@Field(type = FieldType.Integer)
private Integer upUserId;
private String name;
@Field(type = FieldType.Integer)
private Integer lv;
@Field(type = FieldType.String)
private String pollCode;
@Field(type = FieldType.String)
private String userCode;
private String createTime;
private List<UserHostES> userHostList;
public Integer getLv() {
return lv;
}
public void setLv(Integer lv) {
this.lv = lv;
}
public Integer getUpUserId() {
return upUserId;
}
public void setUpUserId(Integer upUserId) {
this.upUserId = upUserId;
}
public List<UserHostES> getUserHostList() {
return userHostList;
}
public void setUserHostList(List<UserHostES> userHostList) {
this.userHostList = userHostList;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPollCode() {
return pollCode;
}
public void setPollCode(String pollCode) {
this.pollCode = pollCode;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
@Document註解裏面的幾個屬性,類比mysql的話是這樣:
index –> DB
type –> Table
@Id註解加上後,在Elasticsearch裏相應於該列就是主鍵了
創建Repository:
public interface EsUserRepository extends ElasticsearchRepository<UserES,Integer> {
}
創建UserES 業務類
@Service
public class EsUserServiceImpl implements EsUserService {
@Autowired
EsUserRepository esRepository;
//保存數據
@Override
public void saveUserES(UserES userEs) {
esRepository.save(userEs);
}
//簡單複合查詢:
Override
public List<UserES> findUserByUserIds(ESSearchConditionVO condition) {
BoolQueryBuilder builder = QueryBuilders.boolQuery();
builder.should(QueryBuilders.termQuery("status",2));//相當於or
builder.should(QueryBuilders.termQuery("status",3));
builder.must(QueryBuilders.termQuery("lv",3));//相當於and
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(QueryBuilders.indicesQuery(builder)).build();
return esRepository.search(searchQuery).getContent();
}
//刪除
@Override
public void delUserES(Integer userEsId) {
esRepository.delete(userEsId);
}
}
到此spring boot 整合elasticsearch完畢,初次使用,不足之處歡迎指出。