我正在尝试从我的索引中删除具有特定产品 ID 的文档。
示例代码如下:
package main
import (
"encoding/json"
"log"
"time"
"fmt"
"gopkg.in/mgo.v2/bson"
elastic "gopkg.in/olivere/elastic.v3"
)
func main() {
client, err := elastic.NewClient(elastic.SetSniff(false), elastic.SetURL("http://localhost:9200"))
if err != nil {
log.Fatal("Cannot create ES client:", err)
}
boolQuery := elastic.NewBoolQuery().Must(elastic.NewTermQuery("productId", "1503368"))
searchQuery := client.Search().Query(boolQuery).
Index("magento1").Type("catalog")
result, err := searchQuery.Do()
for _, hit := range result.Hits.Hits {
var data bson.M
_ = json.Unmarshal(*hit.Source, &data)
fmt.Println("SEARCH RESPONSE\n", data)
}
result2, err2 := elastic.NewDeleteByQueryService(client).
Index("magento1").
Type("catalog").
Query(boolQuery).
Do()
fmt.Println("DELETE RESPONSE 2: \n", result2, err2)
}
SearchQuery 给出了正确的响应并向我返回了提供的 productId 的文档(我这样做只是为了验证文档是否存在)。
现在是删除,我不知道代码到底出了什么问题,或者 API 中是否缺少任何东西,或者必须添加一些额外的东西(matchAll 等),但是这个 deleteQuery 只是没有删除索引并且是总是给我回应:
error elastic: Error 404 (Not Found)
我搜索了所有博客/文档/官方图书馆的 GitHub tests案例等,但没有解决我的问题。虽然我已经决定扫描/滚动和批量删除,但我仍然想知道为什么这在官方文档中不起作用。
这是映射:
"productId": bson.M{"type": "string", "store": true, "index": "not_analyzed"},
ES 版本:
5.3.1
谢谢。
最佳答案
问题是因为您使用的是 elastic.v3 targets ES 2.x .
由于您使用的是 ES 5.3.1,因此需要使用 elastic.v5 , 所以只需替换这一行
elastic "gopkg.in/olivere/elastic.v3"
由
elastic "gopkg.in/olivere/elastic.v5"
你应该没问题。
关于elasticsearch - DeleteByQuery ElasticSearch Golang 错误 elastic : Error 404 (Not Found),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45379453/