初始化全文搜索客户端,包括检查索引是否存在,mapping设置等
()
| 138 | |
| 139 | //初始化全文搜索客户端,包括检查索引是否存在,mapping设置等 |
| 140 | func (this *ElasticSearchClient) Init() (err error) { |
| 141 | if !this.On { //未开启ElasticSearch,则不初始化 |
| 142 | return |
| 143 | } |
| 144 | //检测是否能ping同 |
| 145 | if err = this.ping(); err == nil { |
| 146 | //检测索引是否存在;索引不存在,则创建索引;如果索引存在,则直接跳过初始化 |
| 147 | if err = this.existIndex(); err != nil { |
| 148 | //创建索引成功 |
| 149 | if err = this.createIndex(); err == nil { |
| 150 | //创建mapping |
| 151 | js := `{ |
| 152 | "properties": { |
| 153 | "title": { |
| 154 | "type": "text", |
| 155 | "analyzer": "ik_max_word", |
| 156 | "search_analyzer": "ik_smart" |
| 157 | }, |
| 158 | "keywords": { |
| 159 | "type": "text", |
| 160 | "analyzer": "ik_max_word", |
| 161 | "search_analyzer": "ik_smart" |
| 162 | }, |
| 163 | "description": { |
| 164 | "type": "text", |
| 165 | "analyzer": "ik_max_word", |
| 166 | "search_analyzer": "ik_smart" |
| 167 | }, |
| 168 | "vcnt": { |
| 169 | "type": "integer" |
| 170 | }, |
| 171 | "is_book": { |
| 172 | "type": "integer" |
| 173 | } |
| 174 | } |
| 175 | }` |
| 176 | if orm.Debug { |
| 177 | beego.Debug(" ==== ElasticSearch初始化mapping ==== ") |
| 178 | beego.Info(js) |
| 179 | beego.Debug(" ==== ElasticSearch初始化mapping ==== ") |
| 180 | } |
| 181 | api := this.Host + this.Index + "/" + this.Type + "/_mapping" |
| 182 | err = utils.HandleResponse(this.post(api).Body(js).Response()) |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | return |
| 187 | } |
| 188 | |
| 189 | //搜索内容 |
| 190 | // 如果书籍id大于0,则表示搜索指定的书籍的文档。否则表示搜索书籍 |
nothing calls this directly
no test coverage detected