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)
for _, v := range out.([]*service.ESManage) {
ids = append(ids, v.ID)
for _, v := range out.([]interface{}) {
val := v.(*service.ESManage)
ids = append(ids, val.ID)
}
handle, has := searchIdentityHandle[identity]

View File

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

View File

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

View File

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

View File

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