es

package module
v0.0.0-...-303b45c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2023 License: MIT Imports: 3 Imported by: 0

README

Installation

go get github.com/golangblogs/elasticsearch

Example EsConn

    import "github.com/golangblogs/elasticsearch"

    // 如果不想用默认的链接地址和端口号,请先调用该方法
	//如果不调用该方法,默认是127.0.0.1  9200
    elasticsearch.EsConfig("10.2.227.115", 9200)

Example EsBulkAdd

import "github.com/golangblogs/elasticsearch"

/ EsBulkAdd 批量添加文档
// @router /es/bulk/add [post]
func (this *EsDemoController) EsBulkAdd() {
        //从数据库获取数据 是一个切片
        video := models.GetVideoList()
        //定义存储桶的数据
        var bulkBody []interface{}
        for _, v := range video {
                //拼接要导入的数据 得拼接
                body := map[string]interface{}{
                    "id":                   v.Id,
                    "title":                v.Title,
                    "sub_title":            v.SubTitle,
                    "add_time":             v.AddTime,
                    "img":                  v.Img,
                    "img1":                 v.Img1,
                    "episodes_count":       v.EpisodesCount,
                    "is_end":               v.IsEnd,
                    "channel_id":           v.ChannelId,
                    "status":               v.Status,
                    "region_id":            v.RegionId,
                    "type_id":              v.TypeId,
                    "episodes_update_time": v.EpisodesUpdateTime,
                    "comment":              v.Comment,
                    "user_id":              v.UserId,
                    "is_recommend":         v.IsRecommend,
                }
                bulkBody = append(bulkBody, body)
        }
		//index_name 索引的名字
        elasticsearch.EsBulkAdd("index_name", bulkBody)
        //to do something
}

Example EsSearch

import "github.com/golangblogs/elasticsearch"

// @router /es/search [post]
func (this *EsDemoController) EsSearch() {
	//查询的条件
	title := this.GetString("title")
	//第几页
	page, _ := this.GetInt("page", 1)
	//查几条,每页显示条数
	size, _ := this.GetInt("limit", 10)
	//偏移量
	from := (page - 1) * size
	//拼接查询条件
	query := map[string]interface{}{
		"bool": map[string]interface{}{
			"must": []map[string]interface{}{
				map[string]interface{}{
					"match": map[string]interface{}{
						//搜索条件 title换成自己的搜索条件
						"title": title,
					},
				},
			},
		},
	}

	//排序条件 我这一快默认写了id 倒叙排列,如果没有id则检索不出数据
	sort := []map[string]string{map[string]string{"id": "desc"}}
	//index_name 索引名字
	res := elasticsearch.EsSearch("index_name", query, from, size, sort)
	//转结构体
	//符合搜索条件总条数
	total := res.Hits.Total.Value
	//定义一个视频切片,存放搜索完数据
	var videos []models.VideoData

	for _, v := range res.Hits.Hits {
		tmp := models.VideoData{}
		json.Unmarshal(v.Source, &tmp)
		videos = append(videos, tmp)
	}

	this.Data["json"] = map[string]interface{}{
		"code":  0,
		"total": total,
		"data":  videos,
		"msg":   "ok",
	}
	this.ServeJSON()

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EsAdd

func EsAdd(indexName string, id string, body map[string]interface{}) (bool, error)

EsAdd 添加

func EsBulkAdd

func EsBulkAdd(indexName string, data []interface{}) (bool, error)

EsBulkAdd 实现批量添加数据

func EsConfig

func EsConfig(host string, port int)

如果不想用默认的链接地址和端口号,请先调用该方法

func EsDelete

func EsDelete(indexName string, id string) (bool, error)

EsDelete 删除

func EsEdit

func EsEdit(indexName string, id string, body map[string]interface{}) (bool, error)

EsEdit 修改

Types

type HitsData

type HitsData struct {
	Total TotalData     `json:"total"`
	Hits  []HitsTwoData `json:"hits"`
}

func EsSearch

func EsSearch(indexName string, query map[string]interface{}, from int, size int, sort []map[string]string) (HitsData, error)

搜索功能 sort []map[string]string 根据哪个字段排序 正序还是倒叙

type HitsTwoData

type HitsTwoData struct {
	Source json.RawMessage `json:"_source"`
}

type ReqSearchData

type ReqSearchData struct {
	Hits HitsData `json:"hits"`
}

解析获取到的值

type TotalData

type TotalData struct {
	Value    int
	Relation string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL