golang 使用 github.com/olivere/elastic 来进行经纬度搜索,范围查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package services

import (
	"context"
	"data-dict-api/dao"
	"github.com/olivere/elastic"
)

// SearchGeoParams ...
type SearchGeoParams struct {
	Model    interface{}
	Index    string
	DocType  string
	Lat      float64
	Lon      float64
	Distance string
}

func SearchGeoList(searchGeoParams SearchGeoParams) (*elastic.SearchResult, error) {
	ctx := context.Background()

	q := elastic.NewGeoDistanceQuery("location").Distance(searchGeoParams.Distance).Lat(searchGeoParams.Lat).Lon(searchGeoParams.Lon)

	sorter := elastic.NewGeoDistanceSort("location").Point(searchGeoParams.Lat, searchGeoParams.Lon).Order(true).Unit("km").SortMode("min")

	boolQ := elastic.NewBoolQuery()
	boolQ.Must(elastic.NewMatchAllQuery())
	boolQ.Filter(q)

	searchResult, err := dao.ESClient.Search(searchGeoParams.Index).Type(searchGeoParams.DocType).Size(9999).Query(boolQ).SortBy(sorter).Do(ctx)

	if err != nil {
		return nil, err
	}

	return searchResult, nil
}