ElasticSearch + Go:索引失败(名称无功能)

我正在尝试使用 v1.4x 中的完成建议器让 ElasticSearch 为我的自动完成服务索引内容。我正在听从ElasticSearch 的建议- 你完成我并且正在使用 Go Client Olivere/elastic。


我的索引方法看起来有点像这样:


func IndexVehicle(client *elastic.Client, vehicle Vehicle) (bool, error) {

    // See if it exists already

    fetch, err := client.Get().

        Index(vehicleIndex).

        Type("vehicle").

        Id(vehicle.Id).

        Do()

    if err != nil || fetch.Found {

        return false, err

    }


    vehicleName := fmt.Sprintf("%s %s (%s) %s", vehicle.Make, vehicle.Model, vehicle.Model_year, vehicle.Primary_fuel)


    suggest := elastic.NewSuggestField()

    suggest.Input(vehicle.Make, vehicle.Model, vehicle.Primary_fuel, vehicle.Model_year).

        Output(vehicleName).

        Payload(vehicle)


    // Go forth and save

    put, err := client.Index().

        Index(vehicleIndex).

        Type("vehicle").

        Id(vehicle.Id).

        Debug(true).Pretty(true).

        BodyJson(indexBody{Name: vehicleName, Suggest: suggest}).

        Do()

    if err != nil {

        return false, err

    }

    return put.Created, nil

}

现在奇怪的是,在我的一些测试中,这种方法工作正常,项目将被索引。在擦除和重建索引后的其他测试中,测试将失败:


早期的测试在调试中看起来像这样:


2014/12/15 14:11:37 PUT /vehicle/vehicle/369f96459b340507c4688740da3bfe1a?pretty=true HTTP/1.1

Host: localhost:9200

User-Agent: elastic/1.3.1 (darwin-amd64)

Transfer-Encoding: chunked

Accept: application/json

Content-Type: application/json

Accept-Encoding: gzip


{"name":"American Motors Corporation Eagle 4WD (1986) regular","suggest":{"input":["American Motors Corporation","Eagle 4WD","regular","1986"],"output":"American Motors Corporation Eagle 4WD (1986) regular","payload":{"make":"American Motors Corporation","model_year":"1986","model":"Eagle 4WD","primary_fuel":"regular","vehicle_class":"Special Purpose Vehicle 4WD","transmission":"Automatic 3-spd","displacement":"4.2","drive":"4-Wheel or All-Wheel Drive","city_mpg":"15.0","highway_mpg":"19.0","comb_mpg":"17.0"}}}


2014/12/15 14:11:37 HTTP/1.1 201 Created

Content-Length: 134

Content-Type: application/json; charset=UTF-8

慕尼黑的夜晚无繁华
浏览 273回答 2
2回答

慕工程0101907

只是为了完整性:当我用“post”而不是“POST”初始化我的请求方法时,我遇到了同样的错误像这样:req, _ := http.NewRequest("post", url, query)// returns: Error 400: ElasticsearchIllegalArgumentException[No feature for name [_bulk]]req, _ := http.NewRequest("POST", url, query)// works just fine

红糖糍粑

该异常仅在一个地方触发,即在 Get Index API 调用期间。这意味着您的车辆 ID 在此处必须为空:fetch, err := client.Get().&nbsp; &nbsp; &nbsp; &nbsp; Index(vehicleIndex).&nbsp; &nbsp; &nbsp; &nbsp; Type("vehicle").&nbsp; &nbsp; &nbsp; &nbsp; Id(vehicle.Id).&nbsp; &nbsp; &nbsp; &nbsp;//<-- this&nbsp; &nbsp; &nbsp; &nbsp; Do()您正在尝试执行 Get Document API,它遵循GET /{index}/{type}/{id}. 但是,您的客户端不会区分 Get Document 和 Get Index API 调用……并且它不会验证您的参数是否为非空。因此,如果将 nullvehicle.Id传递给 Get 方法,则您的最终 URL 实际上将是GET /{index}/{type}/从 Elasticsearch 的角度来看,这不再是一个 Get Document API 调用……它实际上是一个 Get Index 调用,其格式如下: GET /{index}/{feature}. 特征可以是以下之一:_settings,_mappings,_aliases或_warmers。因为vehicle不是这些特性之一,ES 正在抛出异常并喷涌而出。您可以从控制台验证这一点:curl -XGET localhost:9200/my_index/vehicle/
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go