Browse Source

代码整理,输出整理,文档整理,统一API大小写

yll 4 years ago
parent
commit
955b05f992
5 changed files with 79 additions and 37 deletions
  1. 49 8
      README.md
  2. 8 8
      types/search_response.go
  3. 0 5
      wukongd/config.go
  4. 5 16
      wukongd/http.go
  5. 17 0
      wukongd/main.go

+ 49 - 8
README.md

@@ -1,15 +1,56 @@
 wukongd
 ========
-wukongd 是基于 [wukong](https://github.com/huichen/wukong) 的搜索引擎. wukong 是一个注重研究和部分实用性的项目,
-wukongd 在其基础上主要做了如下更新:
+wukongd 是基于 [wukong](https://github.com/huichen/wukong) 的搜索引擎。作为独立的服务进程运行
 
-* 文档 Id (docId) 不再是 uint64 类型, 而是 string 类型.
-	这样就简单地使 Id 有更强的表达能力, 轻易地支持多库多表多字段的索引. 只比 uint64 类型有轻微的性能损失
-* 增加 REST RPC 接口.
-	引擎部分不再作为一个库被使用, 只需要启动 wukongd 调用其接口即可
-* 增加 wukongd 配置文件.
+* 文档 Id (docId) 由 uint64 改为 string 类型。
+* 增加 REST RPC 接口。
+* 增加 wukongd.conf 配置文件 (toml 格式)。
 
-* 如果需要使用 wukong 的代码, 可以直接导航到其 github repo, 或 checkout orig 分支.
+> 如果需要使用 wukong 的代码, 可以直接导航到其 github repo, 或 checkout orig 分支.
+
+wukongd REST API
+================
+
+- POST http://host:port/v1/index 添加索引
+
+参数
+```json
+{
+  "docId":"db:table:column:1",
+  "content":"我爱这个世界", 
+  "forceUpdate":false
+}
+```
+- POST http://host:port/v1/delete_index 删除索引
+- DELETE http://host:port/v1/index 删除索引
+
+参数
+```json
+{
+  "docId":"db:table:column:1"
+}
+```
+
+- GET http://host:port/v1/search?keywords=xxx 搜索
+
+返回
+见 SearchResponse 结构体定义
+
+wukongd 启动参数
+================
+-c <config> 指定配置文件
+-t <config> 测试配置文件
+-v 打印版本
+-h 
+
+wukongd TODO
+============
+
+增加 API
+
+- shutdown
+- stat
+- if index exists already 
 
 TODO
 =====

+ 8 - 8
types/search_response.go

@@ -6,32 +6,32 @@ import (
 
 type SearchResponse struct {
 	// 搜索用到的关键词
-	Tokens []string
+	Tokens []string `json:"tokens"`
 
 	// 搜索到的文档,已排序
-	Docs []ScoredDocument
+	Docs []ScoredDocument `json:"docs"`
 
 	// 搜索是否超时。超时的情况下也可能会返回部分结果
-	Timeout bool
+	Timeout bool `json:"timeout"`
 
 	// 搜索到的文档个数。注意这是全部文档中满足条件的个数,可能比返回的文档数要大
-	NumDocs int
+	NumDocs int `json:"numDocs"`
 }
 
 type ScoredDocument struct {
-	DocId string
+	DocId string `json:"docId"`
 
 	// 文档的打分值
 	// 搜索结果按照Scores的值排序,先按照第一个数排,如果相同则按照第二个数排序,依次类推。
-	Scores []float32
+	Scores []float32 `json:"scores"`
 
 	// 用于生成摘要的关键词在文本中的字节位置,该切片长度和SearchResponse.Tokens的长度一样
 	// 只有当IndexType == LocationsIndex时不为空
-	TokenSnippetLocations []int
+	TokenSnippetLocations []int `json:"tokenSnippetLocations"`
 
 	// 关键词出现的位置
 	// 只有当IndexType == LocationsIndex时不为空
-	TokenLocations [][]int
+	TokenLocations [][]int `json:"tokenLocations"`
 }
 
 // 为了方便排序

+ 0 - 5
wukongd/config.go

@@ -2,7 +2,6 @@ package main
 
 import (
 	"errors"
-	"fmt"
 	"github.com/BurntSushi/toml"
 	"net"
 	"os"
@@ -60,7 +59,6 @@ func checkConfig(c *Config) error {
 		return err
 	}
 	c.Server.Mode = strings.ToLower(c.Server.Mode)
-	fmt.Println(c)
 	if c.Server.Mode != "debug" && c.Server.Mode != "release" {
 		return errors.New("invalid server.Mode config")
 	}
@@ -91,8 +89,5 @@ func checkConfig(c *Config) error {
 
 func exists(fileOrDir string) bool {
 	_, err := os.Stat(fileOrDir)
-	if err != nil {
-		fmt.Println(err)
-	}
 	return err == nil
 }

+ 5 - 16
wukongd/http.go

@@ -15,13 +15,13 @@ var (
 )
 
 type ParamIndex struct {
-	Key         string `json:"key"`
+	DocId         string `json:"docId"`
 	Content     string `json:"content"`
 	ForceUpdate bool   `json:"forceUpdate"`
 }
 
 type ParamDelete struct {
-	Key         string `json:"key"`
+	DocId         string `json:"docId"`
 	ForceUpdate bool   `json:"forceUpdate"`
 }
 
@@ -68,18 +68,7 @@ func stopHttp() {
 }
 
 func jsonBody(w *http.ResponseWriter, r *http.Request, v interface{}) error {
-	//reader, err := r.GetBody()
-	//if err != nil {
-	//	//log
-	//	return err
-	//}
-	//defer reader.Close()
-	//
-	//b, err := ioutil.ReadAll(reader)
-	//if err != nil {
-	//	//log
-	//	return err
-	//}
+	//http.MaxBytesReader(*w, r.Body, 10)
 	return json.NewDecoder(r.Body).Decode(v)
 }
 
@@ -92,7 +81,7 @@ func index_v1(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 	fmt.Println("indexing:", param)
-	searcher.IndexDocumentS(param.Key, types.DocumentIndexData{
+	searcher.IndexDocumentS(param.DocId, types.DocumentIndexData{
 		Content: param.Content,
 	}, param.ForceUpdate)
 
@@ -110,7 +99,7 @@ func delete_index_v1(w http.ResponseWriter, r *http.Request) {
 	}
 	fmt.Println("removing index:", param)
 
-	searcher.RemoveDocumentS(param.Key, param.ForceUpdate)
+	searcher.RemoveDocumentS(param.DocId, param.ForceUpdate)
 
 	w.WriteHeader(http.StatusOK)
 	_, err = w.Write([]byte(`{"ec":0,"result":{}}`))

+ 17 - 0
wukongd/main.go

@@ -51,6 +51,9 @@ func main() {
 		fmt.Printf("check config file %s failed: %v\n", *confile, err)
 		return
 	}
+
+	printBaseInfo()
+
 	// init logger
 	// init engine
 	options := types.RankOptions{
@@ -85,3 +88,17 @@ func main() {
 	searcher.Close()
 	fmt.Println("stopped.")
 }
+
+func printBaseInfo() {
+	fmt.Printf(`%s-%s build %s
+listen address: %s
+running mode: %s
+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)
+}