Commit 0c6e58d4 by 孙龙

up

parent 644a3d90
Showing with 103 additions and 33 deletions
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"github.com/gomodule/redigo/redis" "github.com/gomodule/redigo/redis"
"github.com/ichunt2019/logger" "github.com/ichunt2019/logger"
"github.com/syyongx/php2go" "github.com/syyongx/php2go"
"github.com/tidwall/gjson"
"gopkg.in/olivere/elastic.v5" "gopkg.in/olivere/elastic.v5"
"regexp" "regexp"
"search_server/model" "search_server/model"
...@@ -141,6 +142,7 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL ...@@ -141,6 +142,7 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL
var ( var (
goods_name_arr []map[string]string goods_name_arr []map[string]string
keyword string keyword string
is_sample bool
) )
keyword = "" keyword = ""
goods_name_arr = make([]map[string]string,0) goods_name_arr = make([]map[string]string,0)
...@@ -166,20 +168,64 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL ...@@ -166,20 +168,64 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL
if strings.Contains(k,"/") { if strings.Contains(k,"/") {
k_temp = strings.Split(k,"/") k_temp = strings.Split(k,"/")
if len(k_temp) == 2 { if len(k_temp) == 2 {
isok := makeQuery(query,source,k_temp,&goods_temp_status,&v,&keyword) isok := makeQuery(query,source,k_temp,&goods_temp_status,&v,&keyword,&is_sample)
if !isok { if !isok {
continue; continue;
} }
} }
} }
} }
if goods_temp_status <= 0{
query.Must(elastic.NewTermQuery("goods_status",1))
}
//聚合属性
if aType == 1 || aType == 2{
source.Aggregation("brand_id",elastic.NewTermsAggregation().Field("brand_id").Size(1000))
source.Aggregation("encap",elastic.NewTermsAggregation().Field("encap").Size(1000))
source.Aggregation("packing",elastic.NewTermsAggregation().Field("packing").Size(1000))
if zy{
source.Aggregation("attrs",elastic.NewNestedAggregation().Path("attrs").
SubAggregation("attr_name",elastic.NewTermsAggregation().Field("attrs.attr_name").Size(5000).
SubAggregation("attr_value",elastic.NewTermsAggregation().Field("attrs.attr_value").Size(5000))))
}else{
aggClassIdField := ""
if !is_sample{
aggClassIdField = "class_id2"
}else{
aggClassIdField = "sample_class_id"
}
source.Aggregation(aggClassIdField,elastic.NewTermsAggregation().Field(aggClassIdField).Size(1000))
}
}
//排序
//有货排序
if avail_rank,ok :=req["avail_rank"];ok && strings.TrimSpace(avail_rank) == "1"{
avail_rank = strings.TrimSpace(avail_rank)
source.Sort("sort",true)
query.Must(elastic.NewTermQuery("sort",31))
}
//价格排序
if single_rank,ok :=req["single_rank"];ok && single_rank != "" {
switch {
case single_rank == "1":
source.Sort("single_price",false) //asc
case single_rank == "2":
source.Sort("single_price",true) //desc
}
}
//库存排序
} }
source.Query(query) source.Query(query)
res,_ := elastic.NewSearchRequest().Source(source).Body() res,_ := elastic.NewSearchRequest().Source(source).Body()
fmt.Println(res) fmt.Println(res)
} }
func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions []string,goods_temp_status *int,searchValue *string,keyword *string) bool{ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions []string,goods_temp_status *int,searchValue *string,keyword *string,is_sample *bool) bool{
var ( var (
err error err error
...@@ -193,13 +239,10 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions ...@@ -193,13 +239,10 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions
int_field = int_field int_field = int_field
searchKey := conditions[0] searchKey := conditions[0]
searchCondition := conditions[1] searchCondition := conditions[1]
redisConn :=gredis.Conn("search_r") redisConn :=gredis.Conn("search_r")
defer func(){ defer func(){
redisConn.Close() redisConn.Close()
}() }()
searchKey = strings.TrimSpace(searchKey) searchKey = strings.TrimSpace(searchKey)
searchCondition = strings.TrimSpace(searchCondition) searchCondition = strings.TrimSpace(searchCondition)
*searchValue = strings.TrimSpace(*searchValue) *searchValue = strings.TrimSpace(*searchValue)
...@@ -344,7 +387,6 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions ...@@ -344,7 +387,6 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions
} }
} }
query.Must(elastic.NewRangeQuery(searchKey).Gte(paramsRange[0]).Lte(paramsRange[1])) query.Must(elastic.NewRangeQuery(searchKey).Gte(paramsRange[0]).Lte(paramsRange[1]))
case "sr": case "sr":
term_v := make([]string,0) term_v := make([]string,0)
term_v = strings.Split(*searchValue,",") term_v = strings.Split(*searchValue,",")
...@@ -368,11 +410,48 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions ...@@ -368,11 +410,48 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions
} }
} }
case "nested": case "nested":
case "eq":
}
if attrs_matchs,ok := gjson.Parse(*searchValue).Value().(map[string]interface{});ok{
fmt.Println(attrs_matchs)
for attr_name,attr_value:=range attrs_matchs{
filterNestedAttrsBoolQuery := elastic.NewBoolQuery()
fmt.Println(attr_name)
fmt.Println(attr_value)
filterNestedAttrsBoolQuery.Must(elastic.NewTermQuery("attrs.attr_name",attr_name),elastic.NewTermQuery("attrs.attr_value",attr_value))
query.Must(elastic.NewNestedQuery("attrs",filterNestedAttrsBoolQuery))
}
}
case "eq":
switch searchKey {
case "goods_name","brand_name":
reg := regexp.MustCompile(`[[:^alnum:]]+`)
*searchValue = reg.ReplaceAllString(*searchValue,"")
*searchValue = strings.ReplaceAll(*searchValue,"-","")
newTermQueryName := ""
if searchKey == "goods_name"{
newTermQueryName = "auto_goods_name.raw"
}else if(searchKey == "brand_name"){
newTermQueryName = "auto_brand_name2.raw"
}
query.Must(elastic.NewTermQuery(newTermQueryName,*searchValue))
case "sample_max_number","sample_status", "sample_class_id":
*is_sample = true
query.Must(elastic.NewTermQuery(searchKey,*searchValue))
case "supplier_id":
term_v = strings.Split(*searchValue,",")
termsQuerySlice := make([]int64,0)
for _,v:=range term_v{
if term_v1_int,err := strconv.ParseInt(v, 10, 64);err == nil{
termsQuerySlice = append(termsQuerySlice,term_v1_int)
}
}
query.Must(elastic.NewTermsQuery("supplier_id",termsQuerySlice))
}
}
return true return true
} }
\ No newline at end of file
...@@ -2,34 +2,23 @@ package main ...@@ -2,34 +2,23 @@ package main
import ( import (
"log" "log"
"os" "time"
) )
func breadthFirst(f func(item string) []string, worklist []string) { func main(){
seen := make(map[string]bool) bigSlowOperation()
for len(worklist) > 0 {
items := worklist
worklist = nil
for _, item := range items {
if !seen[item] {
seen[item] = true
worklist = append(worklist, f(item)...)
}
}
}
} }
func crawl(url string) []string { func bigSlowOperation() {
fmt.Println(url) defer trace("bigSlowOperation")() // don't forget the
list, err := links.Extract(url) // ...lots of work…
if err != nil { time.Sleep(10 * time.Second) // simulate slow
log.Print(err)
}
return list
}
func main() { }
// Crawl the web breadth-first, func trace(msg string) func() {
// starting from the command-line arguments. start := time.Now()
breadthFirst(crawl, os.Args[1:]) log.Printf("enter %s", msg)
return func() {
log.Printf("exit %s (%s)", msg,time.Since(start))
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment