zhangqian
2023-12-18 16e06252079d36be7686a3d006c8b73565973c68
全文搜索支持时间查询
2个文件已修改
69 ■■■■■ 已修改文件
pkg/blevex/bleve.go 32 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/input_history_search.go 37 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pkg/blevex/bleve.go
@@ -5,6 +5,7 @@
    "github.com/blevesearch/bleve/v2"
    "github.com/blevesearch/bleve/v2/mapping"
    "sync"
    "time"
)
// InitAnalyzer 加载自定义分词器(sego)
@@ -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
}
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) {
    // 定义正则表达式
    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