删除查询 ElasticSearch Golang 错误 elastic: 错误 404 (未找到)

huangapple go评论100阅读模式
英文:

DeleteByQuery ElasticSearch Golang error elastic: Error 404 (Not Found)

问题

我正在尝试使用特定的产品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测试等,但没有解决我的问题。尽管我已经找到了使用Scan/Scroll和Bulk delete的解决方法,但仍然想知道为什么官方文档中的方法不起作用。

以下是映射:

"productId": bson.M{"type": "string", "store": true, "index": "not_analyzed"},

ES版本:
5.3.1

谢谢。

英文:

I'm trying to delete the documents from my Index with specific productIDs.

Here's the sample code:

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)

}

The SearchQuery gives the correct response and returns me the document with the provided productId (I DID THIS JUST TO VERIFY IF THE DOCUMENT EXISTS OR NOT).

Now the deletion, I don't know what on earth is wrong with the code or if there's anything missing in the API or something extra has to be added (matchAll etc) but this deleteQuery is just not deleting the Index and is always giving me the response:

error elastic: Error 404 (Not Found)

I've searched all the blogs/docs/official library's GitHub tests cases etc but none resolved my problem. Although I've come to a resolution to Scan/Scroll and Bulk delete still wonder why this is not working if it's in the official documentation.

Here's the mapping:

"productId": bson.M{"type": "string", "store": true, "index": "not_analyzed"},

ES VERSION:
5.3.1

Thanks.

答案1

得分: 1

问题出在你使用了elastic.v3,它是针对ES 2.x版本的(参考链接:https://github.com/olivere/elastic#elastic-30)。

由于你使用的是ES 5.3.1版本,所以你需要使用elastic.v5,只需将以下这行代码:

elastic "gopkg.in/olivere/elastic.v3"

替换为:

elastic "gopkg.in/olivere/elastic.v5"

这样就可以解决问题了。

英文:

The issue comes from the fact that your using elastic.v3 which targets ES 2.x.

Since you're using ES 5.3.1, you need to use elastic.v5, so simply replace this line

elastic "gopkg.in/olivere/elastic.v3"

by

elastic "gopkg.in/olivere/elastic.v5"

and you should be fine.

huangapple
  • 本文由 发表于 2017年7月29日 01:41:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/45379453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定