feat:优化es查询规则

This commit is contained in:
henry
2021-12-24 09:13:16 +08:00
parent b62ee9c3d9
commit ebf74435ef
5 changed files with 39 additions and 55 deletions

View File

@ -116,8 +116,9 @@ func (c *Search) Launch(identity int, param, industry string, page, pageSize int
} }
ids := make([]uint64, 0) ids := make([]uint64, 0)
for _, v := range out.([]*service.ESManage) { for _, v := range out.([]interface{}) {
ids = append(ids, v.ID) val := v.(*service.ESManage)
ids = append(ids, val.ID)
} }
handle, has := searchIdentityHandle[identity] handle, has := searchIdentityHandle[identity]

View File

@ -34,15 +34,15 @@ func (this *ESManage) Create() error {
} }
func (this *ESManage) Search(page, pageSize int) (interface{}, error) { func (this *ESManage) Search(page, pageSize int) (interface{}, error) {
mustParams := make(map[string]interface{}, 0) termParams := make(map[string]interface{}, 0)
shouldParams := make(map[string]interface{}, 0) shouldParams := make(map[string]interface{}, 0)
if this.Industry != "" {
termParams["industry"] = this.Industry
}
if this.Title != "" { if this.Title != "" {
shouldParams["title"] = this.Title shouldParams["title"] = this.Title
} }
if this.Industry != "" {
mustParams["title"] = this.Industry
}
if this.Keyword != "" { if this.Keyword != "" {
shouldParams["keyword"] = this.Keyword shouldParams["keyword"] = this.Keyword
} }
@ -50,7 +50,7 @@ func (this *ESManage) Search(page, pageSize int) (interface{}, error) {
shouldParams["research"] = this.Research shouldParams["research"] = this.Research
} }
out, err := es.Search(this, this.Index(), &es.SearchParams{ out, err := es.Search(this, this.Index(), &es.SearchParams{
MustParams: mustParams, TermParams: termParams,
ShouldParams: shouldParams, ShouldParams: shouldParams,
}, page, pageSize) }, page, pageSize)

View File

@ -21,30 +21,25 @@ func (this *ESPatent) Index() string {
} }
func (this *ESPatent) Create() error { func (this *ESPatent) Create() error {
if this.Industry != "" {
this.Title = this.Industry + " - " + this.Title
}
_bytes, _ := json.Marshal(this) _bytes, _ := json.Marshal(this)
return es.Create(this.Index(), _bytes) return es.Create(this.Index(), _bytes)
} }
func (this *ESPatent) Search(page, pageSize int) (interface{}, error) { func (this *ESPatent) Search(page, pageSize int) (interface{}, error) {
termParams := make(map[string]interface{}, 0)
mustParams := make(map[string]interface{}, 0) mustParams := make(map[string]interface{}, 0)
shouldParams := make(map[string]interface{}, 0)
fmt.Println(utils.AnyToJSON(this)) fmt.Println(utils.AnyToJSON(this))
if this.Title != "" { if this.Title != "" {
shouldParams["title"] = this.Title mustParams["title"] = this.Title
} }
if this.Industry != "" { if this.Industry != "" {
mustParams["title"] = this.Industry termParams["title"] = this.Industry
} }
fmt.Println(mustParams)
fmt.Println(shouldParams)
out, err := es.Search(this, this.Index(), &es.SearchParams{ out, err := es.Search(this, this.Index(), &es.SearchParams{
MustParams: mustParams, TermParams: termParams,
ShouldParams: shouldParams, MustParams: mustParams,
}, page, pageSize) }, page, pageSize)
if err != nil { if err != nil {

View File

@ -5,7 +5,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/olivere/elastic/v7" "github.com/olivere/elastic/v7"
"reflect"
"strings" "strings"
"testing" "testing"
) )
@ -22,17 +21,20 @@ type Student struct {
AverageScore float64 `json:"average_score"` AverageScore float64 `json:"average_score"`
} }
func TestNewInstance(t *testing.T) { func _new() *elastic.Client {
client, err := elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(host)) client, err := elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(host))
if err != nil { if err != nil {
panic(err) panic(err)
} }
if _, _, err = client.Ping(host).Do(context.Background()); err != nil { if _, _, err = client.Ping(host).Do(context.Background()); err != nil {
t.Log(err) panic(err)
return
} }
return client
}
func TestNewInstance(t *testing.T) {
client := _new()
//newStudent := Student{ //newStudent := Student{
// Name: "熊,桥,顺", // Name: "熊,桥,顺",
@ -92,44 +94,21 @@ func TestNewInstance1(t *testing.T) {
//t.Log(resp) //t.Log(resp)
} }
type Manage struct {
ID uint64 `json:"id"`
}
func TestNewInstance2(t *testing.T) { func TestNewInstance2(t *testing.T) {
//NewInstance( client := _new()
// WithEsAddress([]string{"http://192.168.0.188:9200"}),
//).Init().Local()
//client.Search(func(request *esapi.SearchRequest) { query := elastic.NewBoolQuery()
// query.Must(elastic.NewMatchQuery("title", "一种"))
//}) query.Must(elastic.NewMatchPhraseQuery("industry", "你是"))
function := func(src interface{}) { service := client.Search().Index("es_patent_index")
obj := reflect.ValueOf(src).Interface()
t.Log(obj)
var a interface{} = &Manage{ID: 123123}
out := make([]interface{}, 0) result, err := service.Pretty(true).Query(query).Do(context.Background())
out = append(out, a)
if err != nil {
panic(err)
} }
for _, v := range result.Hits.Hits {
manage := new(Manage) t.Log(string(v.Source))
function(manage)
var a interface{} = &Manage{ID: 123123}
out := make([]interface{}, 0)
out = append(out, a)
t.Log(reflect.TypeOf(manage))
for _, v := range out {
t.Log(v.(*Manage))
} }
t.Log(a)
} }

View File

@ -10,6 +10,7 @@ import (
type ( type (
// SearchParams 搜索参数 // SearchParams 搜索参数
SearchParams struct { SearchParams struct {
TermParams map[string]interface{}
MustParams map[string]interface{} MustParams map[string]interface{}
ShouldParams map[string]interface{} ShouldParams map[string]interface{}
} }
@ -30,13 +31,21 @@ func Search(src interface{}, index string, params *SearchParams, page, pageSize
query := elastic.NewBoolQuery() query := elastic.NewBoolQuery()
if params != nil { if params != nil {
if params.TermParams != nil && len(params.TermParams) > 0 {
for k, v := range params.TermParams {
// 精确查找
query.Must(elastic.NewMatchPhraseQuery(k, v))
}
}
if params.MustParams != nil && len(params.MustParams) > 0 { if params.MustParams != nil && len(params.MustParams) > 0 {
for k, v := range params.MustParams { for k, v := range params.MustParams {
// 分词匹配
query.Must(elastic.NewMatchQuery(k, v)) query.Must(elastic.NewMatchQuery(k, v))
} }
} }
if params.ShouldParams != nil && len(params.ShouldParams) > 0 { if params.ShouldParams != nil && len(params.ShouldParams) > 0 {
for k, v := range params.ShouldParams { for k, v := range params.ShouldParams {
// 分词匹配,查询到一个即可
query.Should(elastic.NewMatchQuery(k, v)) query.Should(elastic.NewMatchQuery(k, v))
} }
} }