1.创建索引
PUT interact
{
"settings": {
"number_of_shards": 5,
"index.refresh_interval": "5s"
},
"mappings": {
"comment": {
"properties": {
"id": {
"type": "long"
},
"pid": {
"type": "long"
},
"pUserId": {
"type": "long"
},
"mainId": {
"type": "long"
},
"mainUserId": {
"type": "long"
},
"mainInvalid": {
"type": "long",
"index": false
},
"tname": {
"type": "integer"
},
"top": {
"type": "integer"
},
"pkSide": {
"type": "integer"
},
"userId": {
"type": "long"
},
"entityId": {
"type": "long"
},
"hidden": {
"type": "integer"
},
"deleted": {
"type": "integer"
},
"visitTime": {
"type": "long"
},
"updateStamp": {
"type": "long"
},
"createStamp": {
"type": "long"
}
}
}
}
}
(1)如何只存储不索引:"index": false
PUT interact/comment/_mapping
{
"properties": {
"perfumeTime": {"type": "long", "index": false},
}
}
(2)如何只索引不存储:"excludes": ["field1"]
PUT interact/comment/_mapping
{
"_source":{
"excludes":["updateStamp"]
},
"properties": {
"perfumeTime": {"type": "long", "index": false},
"updateStamp": {"type": "long"},
}
}
2.es索引核心
倒排索引:关键字->documentId,documentId,documentId
3.es基础语法
# match_all查询
GET interact/comment/_count
{
"query": {"match_all": {}}
}
# es distinct
GET interact/comment/_search
{
"query": {"match_all": {}},
"collapse": {
"field": "tname"
}
}
# es count + distinct
SELECT COUNT(DISTINCT(user_id)) FROM table WHERE user_id_type = 3;
GET interact/comment/_search
{
"query": {
"term": {
"tname": 200
}
},
"aggs": {
"count": {
"cardinality": {
"field": "id"
}
}
}
}
# es count + group by
SELECT COUNT(user_id) FROM table GROUP BY user_id_type;
GET interact/comment/_search
{
"aggs": {
"user_type": {
"terms": {
"field": "tname"
}
}
}
}
# 分页查询
GET interact/comment/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"mainId": 38
}
}
]
}
},
"from":1,
"size":10,
"sort":{
"updateStamp":{
"order":"desc"
}
}
}
# 聚合统计
GET interact/comment/_search
{
"aggs": {
"user_type": {
"terms": {
"field": "tname"
}
}
}
}
# terms查询
GET interact/comment/_count
{
"query": {
"terms": {
"tname":[1,2,3,4,5,6,30,14,16,17]
}}
}
# bool-must查询
GET interact/comment/_search
{
"query": {
"bool": {
"must": [
{"match": {"content": "kingtao"}}
]
}
}
}
# aggs聚合查询
GET interact/comment/_search
{
"aggs": {
"user_type": {
"terms": {
"field": "tname"
}
}
}
}
4.es-bboss接入使用
pom配置
<dependency>
<groupId>com.bbossgroups.plugins</groupId>
<artifactId>bboss-elasticsearch-rest-jdbc</artifactId>
<version>6.3.3</version>
</dependency>
<dependency>
<groupId>com.bbossgroups.plugins</groupId>
<artifactId>bboss-elasticsearch-spring-boot-starter</artifactId>
<version>6.3.3</version>
</dependency>
yml配置
data:
elasticsearch:
bboss:
# elasticUser: elastic
# elasticPassword: changeme
elasticsearch:
rest:
# http client connection, use 9200 port
hostNames: 192.168.11.117:9200
dateFormat: yyyy.MM.dd
timeZone: Asia/Shanghai
ttl: 2d
showTemplate: true
discoverHost: false
dslfile:
refreshInterval: -1
http:
timeoutConnection: 5000
timeoutSocket: 5000
connectionRequestTimeout: 5000
retryTime: 1
maxLineLength: -1
maxHeaderCount: 200
maxTotal: 400
defaultMaxPerRoute: 200
soReuseAddress: false
soKeepAlive: false
timeToLive: 3600000
keepAlive: 3600000
keystore:
keyPassword:
hostnameVerifier:
实体
@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ESComment {
/**
* @ESId
* readSet = true, es指定主键
* persistent = true, 序列化保留
*/
@ESId(readSet = true, persistent = true)
private Integer id;
/** 父评论id */
private Integer pid = 0;
/** 父评论用户id */
private Long pUserId;
/** 主评论id */
private Integer mainId;
/** 主评论userId */
private Long mainUserId;
/** 主评论状态:0-正常,1-失效 */
private Integer mainInvalid = 0;
/** 详细类型 */
private Integer tname;
/** 置顶 */
private Integer top = 0;
}
DAO
@Repository
public class ESCommentDAO {
private final String ES_INDEX = "interact";
private final String ES_TYPE = "comment";
private final String COMMENT_SCRIPT = "esmapper/escomment.xml";
@Autowired
private BBossESStarter bBossESStarter;
public void updateInsertBatch(List<ESComment> commentList) {
bBossESStarter.getRestClient().addDocuments(ES_INDEX, ES_TYPE, commentList);
}
// Map<String,Object> params = new HashMap<String,Object>();
// params.put("hasParam", 0);
// // commentState
// params.put("hidden", null);
// params.put("deleted", null);
// // authorType
// params.put("userIdIn", null);
// params.put("userIdList", null);
// // level
// params.put("pid", null);
// // searchType ...
// params.put("tname", null);
// // isContent、searchVal ...
// params.put("content", null);
// params.put("entityId", null);
// params.put("id", null);
// params.put("level", level);
// params.put("startTime", publishStartTime);
// params.put("endTime", publishEndTime);
// params.put("page", pager);
// params.put("pageSize", pageSize);
public ESDatas<ESComment> listByParams(Map<String, Object> paramMap) {
ESDatas<ESComment> searchList = bBossESStarter.getConfigRestClient(COMMENT_SCRIPT).searchList(ES_INDEX + "/" + ES_TYPE + "/_search", "searchPageDatas", paramMap, ESComment.class);
return searchList;
}
public ESComment findById(Integer id) {
ESComment document = bBossESStarter.getRestClient().getDocument(ES_INDEX, ES_TYPE, id.toString(), ESComment.class);
return document;
}
// Map<String,Object> params = new HashMap<String,Object>();
// params.put("id", null);
// params.put("startTime", updateTime);
public ESDatas<ESComment> listByParamsAll(Map<String, Object> paramMap) {
ESDatas<ESComment> searchList = bBossESStarter.getConfigRestClient(COMMENT_SCRIPT).searchList(ES_INDEX + "/" + ES_TYPE + "/_search", "searchPageDatasAll", paramMap, ESComment.class);
return searchList;
}
// Map<String,Object> params = new HashMap<String,Object>();
// params.put("mainId", 23);
// params.put("hidden", 1);
// params.put("content", null);
// params.put("page", pager);
// params.put("pageSize", pageSize);
public ESDatas<ESComment> listSecondCommentByParams(Map<String, Object> paramMap) {
ESDatas<ESComment> searchList = bBossESStarter.getConfigRestClient(COMMENT_SCRIPT).searchList(ES_INDEX + "/" + ES_TYPE + "/_search", "searchPageSecondDatas", paramMap, ESComment.class);
return searchList;
}
es sql xml
<properties>
<!--分页查询-->
<property name="searchPageDatas">
<![CDATA[{
"query": {
#if($hasParam == 1)
"bool": {
"must": [
{
"range": {
"id": {
"gt": 0
}
}
}
#if($hidden)
,{
"term": {
"hidden": #[hidden]
}
}
#end
#if($deleted)
,{
"term": {
"deleted": #[deleted]
}
}
#end
#if($userIdIn == 1 && $userIdList.size() > 0)
,{
"terms": {
"userId": [
#foreach($userId in $userIdList)
#if($velocityCount > 0),#end "$userId"
#end
]
}
}
#end
#if($level && $level == 1)
,{
"term": {
"pid": 0
}
}
#end
#if($level && $level == 2)
,{
"range": {
"pid": {
"gt": 0
}
}
}
#end
#if($tname && $tname.size() > 0)
,{
"terms": {
"tname": [
#foreach($it in $tname)
#if($velocityCount > 0),#end "$it"
#end
]
}
}
#end
#if($content)
,{
"match": {
"content": #[content]
}
}
#end
#if($entityId)
,{
"term": {
"entityId": #[entityId]
}
}
#end
#if($id)
,{
"term": {
"id": #[id]
}
}
#end
#if($startTime)
,{
"range": {
"createStamp": {
"gte": #[startTime],
"lt": #[endTime]
}
}
}
#end
]
#if($userIdIn == 0 && $userIdList.size() > 0)
,"must_not": [
{
"terms": {
"userId": [
#foreach($userId in $userIdList)
#if($velocityCount > 0),#end "$userId"
#end
]
}
}
]
#end
}
#end
#if($hasParam == 0)
"match_all": {}
#end
},
## page
"from":#[page],
## pageSize
"size":#[pageSize],
"sort":{
"updateStamp":{
"order":"desc"
}
}
}]]>
</property>
<!--条件查询-->
<property name="searchPageDatasAll">
<![CDATA[{
"query": {
"bool": {
"must": [
{
"term": {
"hidden": 0
}
}
,{
"term": {
"deleted": 0
}
}
#if($id)
,{
"range": {
"id": {
"gt": #[id]
}
}
}
#end
,{
"range": {
"updateStamp": {
"gt": #[startTime]
}
}
}
]
}
},
"sort":{
"updateStamp":{
"order":"asc"
}
}
}]]>
</property>
</properties>
网友评论