From 16e06252079d36be7686a3d006c8b73565973c68 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期一, 18 十二月 2023 21:04:08 +0800
Subject: [PATCH] 全文搜索支持时间查询
---
pkg/blevex/bleve.go | 32 ++++++++++++++++
service/input_history_search.go | 37 +++++++++++++++++-
2 files changed, 66 insertions(+), 3 deletions(-)
diff --git a/pkg/blevex/bleve.go b/pkg/blevex/bleve.go
index 08fe999..9b3e612 100644
--- a/pkg/blevex/bleve.go
+++ b/pkg/blevex/bleve.go
@@ -5,6 +5,7 @@
"github.com/blevesearch/bleve/v2"
"github.com/blevesearch/bleve/v2/mapping"
"sync"
+ "time"
)
// InitAnalyzer 鍔犺浇鑷畾涔夊垎璇嶅櫒锛坰ego锛�
@@ -129,3 +130,34 @@
}
return results
}
+
+func TimeSearch(indexName string, t time.Time, conditions map[string]interface{}, from, size int) (ids []string, total uint64, err error) {
+ index, err := LoadIndex(indexName)
+ if err != nil {
+ return nil, 0, err
+ }
+ startDate := t
+ endDate := t.Add(time.Hour * 24).Add(-time.Second * 1)
+ timeRangeQuery := bleve.NewDateRangeQuery(startDate, endDate)
+ boolQuery := bleve.NewBooleanQuery()
+ boolQuery.AddMust(timeRangeQuery)
+ for key, val := range conditions {
+ query := bleve.NewQueryStringQuery(fmt.Sprintf("%v:%v", key, val))
+ boolQuery.AddMust(query)
+ }
+
+ req := bleve.NewSearchRequest(boolQuery)
+ req.From = from
+ req.Size = size
+ res, err := index.Search(req)
+ if err != nil {
+ return nil, 0, err
+ }
+ if res == nil {
+ return
+ }
+ for _, ret := range dealResult(res) {
+ ids = append(ids, ret.Id)
+ }
+ return ids, res.Total, nil
+}
diff --git a/service/input_history_search.go b/service/input_history_search.go
index 45803c5..e81a10d 100644
--- a/service/input_history_search.go
+++ b/service/input_history_search.go
@@ -1,9 +1,12 @@
package service
import (
+ "fmt"
"github.com/spf13/cast"
"gorm.io/gorm"
+ "regexp"
"strconv"
+ "time"
"wms/constvar"
"wms/models"
"wms/pkg/blevex"
@@ -20,7 +23,7 @@
OperationTypeName string `json:"operationTypeName,omitempty"` //涓氬姟鍚嶇О
FromLocation string `json:"fromLocation,omitempty"` //婧愪綅缃悕绉�
ToLocation string `json:"toLocation,omitempty"` //鐩爣浣嶇疆鍚嶇О
- Date string `json:"date,omitempty"` //鏃ユ湡
+ Date time.Time `json:"date,omitempty"` //鏃ユ湡
Company string `json:"company,omitempty"` // 渚涘簲鍟�/瀹㈡埛
Carrier string `json:"carrier,omitempty"` //鎵胯繍鍟嗗悕绉�
WaybillNumber string `json:"waybillNumber"` //杩愬崟鍙�
@@ -68,11 +71,39 @@
}
return
}
+func parseDateString(input string) (time.Time, error) {
+ // 瀹氫箟姝e垯琛ㄨ揪寮�
+ regex := regexp.MustCompile(`(\d{4})-(\d{2})-(\d{2})|(\d{4})/(\d{2})/(\d{2})|(\d{8})`)
+
+ // 鍖归厤瀛楃涓�
+ matches := regex.FindStringSubmatch(input)
+ if len(matches) == 0 {
+ return time.Time{}, fmt.Errorf("Invalid date format")
+ }
+ // 鎻愬彇骞存湀鏃�
+ year := matches[1] + matches[4] + matches[7]
+ month := matches[2] + matches[5]
+ day := matches[3] + matches[6]
+
+ // 鏋勫缓鏃堕棿瀵硅薄
+ parsedTime, err := time.ParseInLocation("20060102", fmt.Sprintf("%s%s%s", year, month, day), time.Local)
+ if err != nil {
+ return time.Time{}, err
+ }
+
+ return parsedTime, nil
+}
func SearchHistoryReport(keyword string, operationType constvar.BaseOperationType, page, pageSize int) (recordIds []int, total uint64, err error) {
var ids []string
from := (page - 1) * pageSize
- ids, total, err = blevex.ComplexSearch(HistoryReportIndexName, keyword, map[string]interface{}{"baseOperationType": operationType}, from, pageSize)
+ t, err := parseDateString(keyword)
+ if err != nil && t.IsZero() {
+ ids, total, err = blevex.ComplexSearch(HistoryReportIndexName, keyword, map[string]interface{}{"baseOperationType": operationType}, from, pageSize)
+ } else {
+ ids, total, err = blevex.TimeSearch(HistoryReportIndexName, t, map[string]interface{}{"baseOperationType": operationType}, from, pageSize)
+ }
+
if err != nil {
return
}
@@ -94,7 +125,7 @@
return
}
- report.Date = record.UpdatedAt.Format("2006-01-02")
+ report.Date = record.UpdatedAt
report.Carrier = operation.LogisticCompany.Name
report.Company = operation.CompanyName
report.WaybillNumber = operation.WaybillNumber
--
Gitblit v1.8.0