美文网首页
es基础,bboss使用

es基础,bboss使用

作者: kingTao_ | 来源:发表于2021-12-06 10:48 被阅读0次

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>

5.es 更多待完善

相关文章

  • es基础,bboss使用

    1.创建索引 (1)如何只存储不索引:"index": false (2)如何只索引不存储:"excludes":...

  • OpenGL ES for iOS - 6

    OpenGL ES 设计指南 现在您已经掌握了在 iOS 应用程序中使用 OpenGL ES 的基础知识,请使用本...

  • Elasticsearch 使用 RESTful API 操作索

    之前我们已经了解了 ES 中一些比较重要的概念,也搭建好了使用 ES 所需的基础环境,那现在就可以开始使用 ES ...

  • OpenGL ES for iOS - 6

    OpenGL ES设计指南 现在,您已经掌握了在iOS应用程序中使用OpenGL ES的基础知识,请使用本章中的信...

  • ES的基础使用

    索引模板 如果更改了模板不能对已存在索引生效 创建索引 下面创建一个名字为 laravel_es 的模板,mapp...

  • React native 入门之es6语法小结

    在进行react native开发中,官方建议使用es6语法作为前端开发的支撑,此文档是以es6为基础,因为es6...

  • 《JS原理、方法与实践》- object类型对象

    ES中一共由两种对象,function和object。object类型对象时ES的基础,它主要通过属性使用。 创建...

  • es6-selfnote

    ECMAScript6(ES6)基础知识及核心原理 使用Babel编译ES6 一、下载安装Babel环境:需要电脑...

  • 19class的基本语法

    Class 是 es6 的语法糖,es5 也能通过其他方式实现 基础 Class 内部的方法不使用 functio...

  • ES 集群上,业务单点如何优化升级?

    ES 基础 ES 集群 ES 集群上业务优化 一、ES 基础 ES 的安装下载,网上一大片,我这边不在重复。可以看...

网友评论

      本文标题:es基础,bboss使用

      本文链接:https://www.haomeiwen.com/subject/whioxrtx.html