123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- package main
- import (
- "context"
- "encoding/json"
- "fmt"
- "strings"
- "time"
- e "github.com/dabory/abango-rest/etc"
- "github.com/elastic/go-elasticsearch/v7"
- "github.com/elastic/go-elasticsearch/v7/esutil"
- )
- var ( //Prefix
- ES_CLIENT *elasticsearch.Client // 기본 Elastic Client
- )
- type UrlBdPageRet struct {
- Page []SchUrlBd
- }
- type ScrUrlBdPage struct {
- Hits struct {
- Hits []ScrUrlBdRow `json:"hits"`
- MaxScore float64 `json:"max_score"`
- Total struct {
- Relation string `json:"relation"`
- Value int `json:"value"`
- } `json:"total"`
- } `json:"hits"`
- }
- type ScrUrlBdRow struct {
- ID string `json:"_id"`
- Index string `json:"_index"`
- Score float64 `json:"_score"`
- Source SchUrlBd `json:"_source"`
- Type string `json:"_type"`
- }
- type SchUrlBd struct {
- Id string //PdplinkHash
- CreatedAt time.Time `xorm:"not null DATETIME"`
- UpdatedAt time.Time `xorm:"not null DATETIME"`
- HostName string //DomainHash
- TargetPath string
- PdpHtmlHash string
- }
- type SimpleFilters struct {
- SimpleFilters []SimpleFilter
- }
- type SimpleFilter struct {
- Condition string
- Name string
- Value string
- }
- func (t *UrlBdPageRet) PageName() string {
- return e.PageName(t.Page)
- }
- func (t *UrlBdPageRet) RecordName() string { //필수
- return "Id of " + e.NumToStr(t.PageName) + " in " + e.PageName(*t) + " "
- }
- func init() {
- var err error
- ES_CLIENT, _ = elasticsearch.NewClient(elasticsearch.Config{
- Addresses: []string{
- "http://kibana-seoul-c.daboryhost.com:9200",
- },
- Username: "elastic",
- Password: "elasticpassword",
- })
- if err != nil {
- e.LogErr("daslkjlasjfd", "Elastic NewClient", err)
- return
- }
- }
- func main() {
- var t UrlBdPageRet
- // queryStr := "HostName like '*seoulknit.com*'"
- queryStr := " Id like '*259beeae13757f9d411dded768342505*' AND HostName like '*seoulknit.com*' "
- // queryStr := "Id='259beeae13757f9d411dded768342505' AND HostName='seoulknit.com' AND TargetPath='/shop/warp-cardigan/'"
- // queryStr := ""
- // queryStr := "HostName='seoulknit.com'"
- // queryStr = "HostName='seoulknit.com' AND TargetPath='/shop/standard-knit-pants_blacklight-gray/'"
- // queryStr = "TargetPath='/shop/warp-cardigan/'"
- ascStr := "Id"
- descStr := "TargetPath, PdpHtmlHash"
- // ascStr := ""
- // descStr := ""
- queryClauses := ElaQueryComposer(queryStr)
- sortClauses := ElaSortComposer(ascStr + "|" + descStr)
- ret, _ := json.Marshal(sortClauses)
- // fmt.Println("ret", string(ret))
- // queryClauses := map[string]interface{}{
- // "bool": map[string]interface{}{
- // "must": []interface{}{
- // map[string]interface{}{
- // "wildcard": map[string]interface{}{
- // "Id": "*259beeae13757f9d411dded768342505*",
- // },
- // },
- // map[string]interface{}{
- // "wildcard": map[string]interface{}{
- // "HostName": "*seoulknit.com*",
- // },
- // },
- // },
- // },
- // }
- fmt.Println("queryClauses", queryClauses)
- // Create a bool query with two must clauses
- query := map[string]interface{}{
- "query": queryClauses,
- "sort": sortClauses,
- "size": 10,
- "from": 0,
- }
- res, err := ES_CLIENT.Search(
- ES_CLIENT.Search.WithContext(context.Background()),
- ES_CLIENT.Search.WithIndex(t.PageName()),
- ES_CLIENT.Search.WithBody(esutil.NewJSONReader(query)),
- )
- defer res.Body.Close()
- if err != nil {
- fmt.Println("qrlhbdf", "Search Error in index: ", err)
- } else {
- if res.IsError() {
- fmt.Println("qrlhbdf", "Search Error in index: ", res)
- }
- }
- var resJs ScrUrlBdPage
- if err := json.NewDecoder(res.Body).Decode(&resJs); err != nil {
- fmt.Println("qrlhbdf", "Error decoding ")
- }
- for _, row := range resJs.Hits.Hits {
- t.Page = append(t.Page, row.Source)
- }
- // ret, _ = json.Marshal(t)
- ret, _ = json.MarshalIndent(t, "", "\t")
- fmt.Println("result: ", string(ret))
- return
- }
- func ElaQueryComposer(qry string) map[string]interface{} {
- // queryStr := "id='kkk' AND ( solution_code = 'WooCommerce' OR host_name = 'andadd.kr' )"
- qryTmp := strings.Replace(strings.Replace(qry, " and ", " AND ", -1), " or ", " OR ", -1)
- qryTmp = strings.Replace(qry, " like ", " LIKE ", -1)
- dbqry1 := strings.Split(qryTmp, " AND ")
- var t SimpleFilters
- if qry == "" {
- fieldsAndValues := t.SimpleFilters
- var matchClauses []map[string]interface{}
- for _, fieldAndValue := range fieldsAndValues {
- matchClauses = append(matchClauses, map[string]interface{}{
- "match": map[string]interface{}{
- fieldAndValue.Name: fieldAndValue.Value,
- },
- })
- }
- return map[string]interface{}{
- "bool": map[string]interface{}{
- "must": matchClauses,
- },
- }
- }
- for i := 0; i < (len(dbqry1)); i++ {
- clause := dbqry1[i]
- clauses := []string{}
- dbqry2 := strings.Split(strings.Trim(strings.Trim(clause, "("), ")"), " OR ")
- if len(dbqry2) > 1 {
- clauses = append(clauses, dbqry2...)
- } else {
- clauses = append(clauses, clause)
- }
- for k := 0; k < len(clauses); k++ {
- var v SimpleFilter
- c := clauses[k]
- if strings.Contains(c, "=") {
- keyValue := strings.Split(c, "=")
- v.Condition = "match"
- v.Name = strings.Replace(keyValue[0], " ", "", -1)
- v.Value = strings.Replace(strings.Replace(keyValue[1], "'", "", -1), " ", "", -1)
- t.SimpleFilters = append(t.SimpleFilters, v)
- } else if strings.Contains(c, " LIKE ") {
- keyValue := strings.Split(c, " LIKE ")
- v.Condition = "wildcard"
- v.Name = strings.Replace(keyValue[0], " ", "", -1)
- v.Value = strings.Replace(strings.Replace(keyValue[1], "'", "", -1), " ", "", -1)
- t.SimpleFilters = append(t.SimpleFilters, v)
- }
- }
- }
- fieldsAndValues := t.SimpleFilters
- var matchClauses []map[string]interface{}
- for _, fieldAndValue := range fieldsAndValues {
- matchClauses = append(matchClauses, map[string]interface{}{
- fieldAndValue.Condition: map[string]interface{}{
- // "match": map[string]interface{}{
- fieldAndValue.Name: fieldAndValue.Value,
- },
- })
- }
- // fmt.Println(matchClauses)
- return map[string]interface{}{
- "bool": map[string]interface{}{
- "must": matchClauses,
- },
- }
- }
- func ElaSortComposer(sort string) (sortClauses []map[string]interface{}) {
- // fmt.Println(sort)
- // Task 2
- // You need to run in kibana to make the filed as sort acceptable
- // PUT my-index-000001/_mapping
- // {
- // "properties": {
- // "my_field": {
- // "type": "text",
- // "fielddata": true
- // }
- // }
- // }
- // var sortClauses []map[string]interface{}
- // fmt.Println("sort", strings.ReplaceAll(sort, " ", ""))
- sortHandle := strings.Split(strings.ReplaceAll(sort, " ", ""), "|")
- // asc
- asc := string(sortHandle[0])
- fieldsAcs := strings.Split(asc, ",")
- fmt.Println(fieldsAcs)
- if sortHandle[0] != "" {
- for i := 0; i < len(fieldsAcs); i++ {
- sortOne := map[string]interface{}{
- fmt.Sprintf("%v.keyword", fieldsAcs[i]): map[string]interface{}{
- "order": "asc",
- },
- }
- sortClauses = append(sortClauses, sortOne)
- }
- } else {
- sortOne := map[string]interface{}{}
- sortClauses = append(sortClauses, sortOne)
- }
- // desc
- if sortHandle[1] != "" {
- desc := string(sortHandle[1])
- fieldsDecs := strings.Split(desc, ",")
- for i := 0; i < len(fieldsDecs); i++ {
- sortOne := map[string]interface{}{
- fmt.Sprintf("%v.keyword", fieldsDecs[i]): map[string]interface{}{
- "order": "desc",
- },
- }
- sortClauses = append(sortClauses, sortOne)
- }
- } else {
- sortOne := map[string]interface{}{}
- sortClauses = append(sortClauses, sortOne)
- }
- return sortClauses
- }
|