刘凯雄 096d50dcd3 重新定位版本号,并为mongo重新添加监控埋点 | 2 years ago | |
---|---|---|
.. | ||
src | 3 years ago | |
README.md | 3 years ago | |
pom.xml | 2 years ago |
<dependency>
<groupId>com.elab.core</groupId>
<artifactId>elab-es</artifactId>
<version>${project.version}</version>
</dependency>
SpringBoot的启动类上加入@EnableESTools
配置文件中加入
spring.elasticsearch.rest.uris=http://192.168.0.24:9200
如果有多个(集群情况)请用半角逗号,
隔开
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.zxp.esclientrhl.annotation.ESMetaData;
import java.io.Serializable;
import java.util.Date;
/**
* 用户entity
*
* @author : liukx
* @time : 2019/8/1 - 11:40
*/
@ESMetaData(indexName = "索引名称", indexType = "类型名称", number_of_shards = 5, number_of_replicas = 0,
printLog = true)
public class UserTable implements Serializable {
// 对应字段
private String doc_id;
private String title;
private String message;
private Date postDate;
private HighlightField highlightField;
public HighlightField getHighlightField() {
return highlightField;
}
public void setHighlightField(HighlightField highlightField) {
this.highlightField = highlightField;
}
public String getDoc_id() {
return doc_id;
}
public void setDoc_id(String doc_id) {
this.doc_id = doc_id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Date getPostDate() {
return postDate;
}
public void setPostDate(Date postDate) {
this.postDate = postDate;
}
@Override
public String toString() {
return "UserTable{" +
"doc_id='" + doc_id + '\'' +
", title='" + title + '\'' +
", message='" + message + '\'' +
", postDate=" + postDate +
", highlightField=" + highlightField +
'}';
}
}
indexName
: 类似于Mysql的数据库名称
indexType
: 类似于Mysql的表名称
printLog
: 是否打印日志,这个东西会将请求的参数转换成ES的文档搜索查询字符串打印出来
加入模版操作类
@Autowired
DefaultElasticSearchTemplate<Main2,String> elasticsearchTemplate;
这里是引入ES操作的模版类。
Main2 : 映射ES的实体类
String : 主键
@ESMetaData(indexName = "index",indexType = "main", number_of_shards = 5,number_of_replicas = 0)
public class Main2 implements Serializable {
private static final long serialVersionUID = 1L;
@ESID
private String proposal_no;
Main2 main = new Main2();
main.setProposal_no("main2");
main.setAppli_code("123");
main.setAppli_name("456");
elasticsearchTemplate.save(main);
List<Main2> list = new ArrayList<>();
Main2 main1 = new Main2();
main1.setProposal_no("main1");
main1.setAppli_code("123");
main1.setAppli_name("456");
Main2 main2 = new Main2();
main2.setProposal_no("main2");
main2.setAppli_code("123");
main2.setAppli_name("456");
Main2 main3 = new Main2();
main3.setProposal_no("main3");
main3.setAppli_code("123");
main3.setAppli_name("456");
list.add(main1);
list.add(main2);
list.add(main3);
elasticsearchTemplate.save(list);
Main2 main1 = new Main2();
main1.setProposal_no("main1");
main1.setInsured_code("123");
elasticsearchTemplate.update(main1);
Main2 main1 = new Main2();
main1.setProposal_no("main1");
main1.setInsured_code("123");
boolean exists = elasticsearchTemplate.exists("main1",Main2.class);
System.out.println(exists);
原生查询
searchRequest是官方原生查询输入,此方法在工具无法满足需求时使用
SearchRequest searchRequest = new SearchRequest(new String[]{"index"});
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(new MatchAllQueryBuilder());
searchSourceBuilder.from(0);
searchSourceBuilder.size(10);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = elasticsearchTemplate.search(searchRequest);
SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
for (SearchHit hit : searchHits) {
Main2 t = JsonUtils.string2Obj(hit.getSourceAsString(), Main2.class);
System.out.println(t);
}
Client
接口去做的,8.0版本之后会被移除,所以还得看看其他方式1=1
可能还是兼容不够好。但是功能还是挺丰富的,比如高亮、GEO、嵌套等等
可以将这几种结合一下。