Browse Source

add: 增加搜索结果分页参数

yll 4 years ago
parent
commit
01ffa8d361
4 changed files with 27 additions and 15 deletions
  1. 5 5
      README.md
  2. 2 0
      wukongd/build.sh
  3. 15 5
      wukongd/http.go
  4. 5 5
      wukongd/main.go

+ 5 - 5
README.md

@@ -31,17 +31,17 @@ wukongd REST API
 }
 ```
 
-- GET http://host:port/v1/search?keywords=xxx 搜索
+- GET http://host:port/v1/search?keywords=xxx&offset=1&limit=10 搜索
 
 返回
 见 SearchResponse 结构体定义
 
 wukongd 启动参数
 ================
--c <config> 指定配置文件
--t <config> 测试配置文件
--v 打印版本
--h 
+- -c <config> 指定配置文件
+- -t <config> 测试配置文件
+- -v 打印版本
+- -h 
 
 wukongd TODO
 ============

+ 2 - 0
wukongd/build.sh

@@ -7,4 +7,6 @@ BRANCH=$(git branch --show-current)
 GOVERSION=$(go version)
 BUILDTIME=$(date +"%Y-%m-%d %H:%M:%S %z")
 
+go fmt *.go
+
 go build -tags purego -ldflags "-X 'main.gitCommit=$COMMITID' -X 'main.gitBranch=$BRANCH' -X 'main.buildTime=$BUILDTIME' -X 'main.goVersion=$GOVERSION'"

+ 15 - 5
wukongd/http.go

@@ -7,6 +7,7 @@ import (
 	"github.com/gorilla/mux"
 	"github.com/huichen/wukong/types"
 	"net/http"
+	"strconv"
 	"time"
 )
 
@@ -15,13 +16,13 @@ var (
 )
 
 type ParamIndex struct {
-	DocId         string `json:"docId"`
+	DocId       string `json:"docId"`
 	Content     string `json:"content"`
 	ForceUpdate bool   `json:"forceUpdate"`
 }
 
 type ParamDelete struct {
-	DocId         string `json:"docId"`
+	DocId       string `json:"docId"`
 	ForceUpdate bool   `json:"forceUpdate"`
 }
 
@@ -40,7 +41,8 @@ func startHttp(addr string) error {
 	v1.HandleFunc("/index", index_v1).Methods("POST")
 	v1.HandleFunc("/index", delete_index_v1).Methods("DELETE")
 	v1.HandleFunc("/delete_index", delete_index_v1).Methods("POST")
-	v1.HandleFunc("/search", search_v1).Methods("GET").Queries("keywords", "{keywords}")
+	v1.HandleFunc("/search", search_v1).Methods("GET").Queries("keywords", "{keywords}",
+		"offset", "{offset:[0-9]+}", "limit", "{offset:[0-9]+}")
 	v1.HandleFunc("/shutdown", shutdown).Methods("POST")
 	router.Use(mux.CORSMethodMiddleware(router))
 
@@ -109,8 +111,16 @@ func delete_index_v1(w http.ResponseWriter, r *http.Request) {
 func search_v1(w http.ResponseWriter, r *http.Request) {
 	params := mux.Vars(r)
 	keywords := params["keywords"]
-	fmt.Println("searching:", params, keywords)
-	resp := searcher.Search(types.SearchRequest{Text: keywords})
+	offset, _ := strconv.ParseInt(params["offset"], 10, 64)
+	limit, _ := strconv.ParseInt(params["limit"], 10, 64)
+	fmt.Println("searching:", params)
+	resp := searcher.Search(types.SearchRequest{
+		Text: keywords,
+		RankOptions: &types.RankOptions{
+			OutputOffset: int(offset),
+			MaxOutputs:   int(limit),
+		},
+	})
 	sresp, err := json.Marshal(resp)
 	if err != nil {
 		w.WriteHeader(http.StatusInternalServerError)

+ 5 - 5
wukongd/main.go

@@ -97,8 +97,8 @@ db location: %s
 log location: %s
 log level: %s
 `, name, version, gitCommit, config.Server.Addr,
-config.Server.Mode,
-config.Engine.PersistentFolder,
-config.Logger.File,
-config.Logger.Level)
-}
+		config.Server.Mode,
+		config.Engine.PersistentFolder,
+		config.Logger.File,
+		config.Logger.Level)
+}