演示地址
代码地址
…
服务端elasticsearch版本:
elasticsearch-6.7.0
elasticsearch-analysis-ik-6.7.0
springboot版本:
2.1.3.RELEASE
实体类org.springframework.boot spring-boot-starter-parent 2.1.3.RELEASE org.springframework.boot spring-boot-starter-freemarker org.springframework.boot spring-boot-starter-data-elasticsearch org.elasticsearch elasticsearch org.springframework.data spring-data-elasticsearch org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-web
对字段content, title 使用Ik分词
package com.example.demo.entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import org.springframework.data.elasticsearch.annotations.document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@document(indexName = "mysite")
public class EsModel {
@javax.persistence.Id
@GeneratedValue(strategy = GenerationType.AUTO)
private String id;
@Field(type = FieldType.Text, analyzer="ik_max_word")
private String content;
@Field(type = FieldType.Text, analyzer="ik_max_word")
private String title;
...
}
Service接口配置
package com.example.demo.service; import org.springframework.data.repository.CrudRepository; import com.example.demo.entity.EsModel; public interface EsRepository extends org.springframework.data.repository.Repositoryapplication.properties配置, CrudRepository { }
spring.elasticsearch.rest.uris=http://localhost:9200 spring.data.elasticsearch.cluster-name=elasticsearch spring.data.elasticsearch.cluster-nodes=localhost:9300 # 设定模板的后缀. spring.freemarker.suffix=.ftl # 设定模板的加载路径,多个以逗号分隔,默认: spring.freemarker.template-loader-path=classpath:/templates/利用freemark拼装查询语句查询
@GetMapping("query")
public JSonObject query(@RequestParam("name") String name,
@RequestParam(name = "page", defaultValue = "0") Integer page,
@RequestParam(name = "size", defaultValue = "10") Integer size) {
Map params = new HashMap<>();
params.put("name", name);
params.put("size", size);
params.put("from", page * size);
String querySql = FreemarkUtil.parseTpl("EsQuery", params);
Request request = new Request("GET", "/mysite/_search");
HttpEntity entity = new NStringEntity(querySql, ContentType.APPLICATION_JSON);
request.setEntity(entity);
Response response;
try {
response = client.performRequest(request);
String result = EntityUtils.toString(response.getEntity());
JSonObject json = JSON.parseObject(result);
JSonObject jsonObject = new JSONObject();
jsonObject.put("success", true);
jsonObject.put("data", json);
return jsonObject;
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
JSonObject jsonObject = new JSONObject();
jsonObject.put("success", false);
return jsonObject;
}
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "${name}",
"fields": [
"title",
"content"
]
}
}
]
}
},
"highlight": {
"pre_tags" : [""color:red">"],
"post_tags" : [""],
"fields": {
"title": {},
"content": {}
}
},
"size": ${size},
"from": ${from}
}
关于windows server 服务器elasticsearch配置
1、配置好jdk环境
2、把ik分词组件解压至plugins目录
3、 修改配置elasticsearch.yml
修改network.host对应值为0.0.0.0,否则只能本机访问。
第一种启动方式可以直接双击bin目录下的elasticsearch.bat
第二种以服务方式启动:
cmd进入对应bin目录,执行命令 elasticsearch-service.bat install
然后 执行 net start elasticsearch-service-x64



