Commit 0c6e58d4 by 孙龙

up

parent 644a3d90
Showing with 102 additions and 32 deletions
......@@ -7,6 +7,7 @@ import (
"github.com/gomodule/redigo/redis"
"github.com/ichunt2019/logger"
"github.com/syyongx/php2go"
"github.com/tidwall/gjson"
"gopkg.in/olivere/elastic.v5"
"regexp"
"search_server/model"
......@@ -141,6 +142,7 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL
var (
goods_name_arr []map[string]string
keyword string
is_sample bool
)
keyword = ""
goods_name_arr = make([]map[string]string,0)
......@@ -166,20 +168,64 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL
if strings.Contains(k,"/") {
k_temp = strings.Split(k,"/")
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 {
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)
res,_ := elastic.NewSearchRequest().Source(source).Body()
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 (
err error
......@@ -193,13 +239,10 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions
int_field = int_field
searchKey := conditions[0]
searchCondition := conditions[1]
redisConn :=gredis.Conn("search_r")
defer func(){
redisConn.Close()
}()
searchKey = strings.TrimSpace(searchKey)
searchCondition = strings.TrimSpace(searchCondition)
*searchValue = strings.TrimSpace(*searchValue)
......@@ -344,7 +387,6 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions
}
}
query.Must(elastic.NewRangeQuery(searchKey).Gte(paramsRange[0]).Lte(paramsRange[1]))
case "sr":
term_v := make([]string,0)
term_v = strings.Split(*searchValue,",")
......@@ -368,11 +410,48 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions
}
}
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
}
\ No newline at end of file
......@@ -2,34 +2,23 @@ package main
import (
"log"
"os"
"time"
)
func breadthFirst(f func(item string) []string, worklist []string) {
seen := make(map[string]bool)
for len(worklist) > 0 {
items := worklist
worklist = nil
for _, item := range items {
if !seen[item] {
seen[item] = true
worklist = append(worklist, f(item)...)
}
}
}
func main(){
bigSlowOperation()
}
func crawl(url string) []string {
fmt.Println(url)
list, err := links.Extract(url)
if err != nil {
log.Print(err)
}
return list
}
func bigSlowOperation() {
defer trace("bigSlowOperation")() // don't forget the
// ...lots of work…
time.Sleep(10 * time.Second) // simulate slow
func main() {
// Crawl the web breadth-first,
// starting from the command-line arguments.
breadthFirst(crawl, os.Args[1:])
}
func trace(msg string) func() {
start := time.Now()
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