搜索内容 如果书籍id大于0,则表示搜索指定的书籍的文档。否则表示搜索书籍 如果不指定书籍id,则只能搜索
(wd string, p, listRows int, isSearchDoc bool, bookId ...int)
| 190 | // 如果书籍id大于0,则表示搜索指定的书籍的文档。否则表示搜索书籍 |
| 191 | // 如果不指定书籍id,则只能搜索 |
| 192 | func (this *ElasticSearchClient) Search(wd string, p, listRows int, isSearchDoc bool, bookId ...int) (result ElasticSearchResult, err error) { |
| 193 | if !this.On { |
| 194 | return |
| 195 | } |
| 196 | |
| 197 | wd = strings.Replace(wd, "\"", " ", -1) |
| 198 | wd = strings.Replace(wd, "\\", " ", -1) |
| 199 | bid := 0 |
| 200 | if len(bookId) > 0 && bookId[0] > 0 { |
| 201 | bid = bookId[0] |
| 202 | } |
| 203 | |
| 204 | var queryBody string |
| 205 | |
| 206 | // 请求体 |
| 207 | if bid > 0 { // 搜索指定书籍的文档,不限私有和公有 |
| 208 | queryBody = `{"query": {"bool": {"filter": [{ |
| 209 | "term": { |
| 210 | "book_id": {$bookId} |
| 211 | } |
| 212 | }], |
| 213 | "must":{"multi_match" : { |
| 214 | "query": "%v", |
| 215 | "minimum_should_match": "%v", |
| 216 | "fields": [ "title", "keywords","content" ] |
| 217 | } |
| 218 | } |
| 219 | }},"from": %v,"size": %v,"_source":["id"]}` |
| 220 | queryBody = strings.Replace(queryBody, "{$bookId}", strconv.Itoa(bid), 1) |
| 221 | } else { |
| 222 | if isSearchDoc { //搜索公开的文档 |
| 223 | queryBody = `{"query": {"bool": { |
| 224 | "filter": [ |
| 225 | {"range": {"book_id": {"gt": 0}}}, |
| 226 | {"term": {"private": 0}} |
| 227 | ],"must":{ |
| 228 | "multi_match" : { |
| 229 | "query": "%v", |
| 230 | "minimum_should_match": "%v", |
| 231 | "fields": [ "title", "keywords","content" ] |
| 232 | }}}},"from": %v,"size": %v,"_source":["id"]}` |
| 233 | } else { //搜索公开的书籍 |
| 234 | queryBody = `{"query": {"bool": { |
| 235 | "filter": [ |
| 236 | {"term": {"book_id": 0}}, |
| 237 | {"term": {"private": 0}} |
| 238 | ],"must":{ |
| 239 | "multi_match" : { |
| 240 | "query": "%v", |
| 241 | "minimum_should_match": "%v", |
| 242 | "fields": [ "title", "keywords","content" ] |
| 243 | } |
| 244 | }}},"from": %v, "size": %v,"_source":["id"]}` |
| 245 | } |
| 246 | } |
| 247 | percent := GetOptionValue("SEARCH_ACCURACY", "50") |
| 248 | if this.IsRelateSearch { |
| 249 | percent = "10" |
no test coverage detected