登录
首页 >  Golang >  Go问答

如何重构 JSON/Elasticsearch 响应

来源:stackoverflow

时间:2024-04-18 14:57:33 452浏览 收藏

大家好,我们又见面了啊~本文《如何重构 JSON/Elasticsearch 响应》的内容中将会涉及到等等。如果你正在学习Golang相关知识,欢迎关注我,以后会给大家带来更多Golang相关文章,希望我们能一起进步!下面就开始本文的正式内容~

问题内容

我正在使用 olivere v7。

我正在尝试将 _id_source (来自 elasticsearch 调用,返回 json 响应)合并到重新排序的结构中,并以该格式提供结果。

这就是我的实际代码:

elasticsearch, err := //actual request to es using olivere
fmt.println(elasticsearch.hits.hits)

它成功拨打电话并打印以下内容:

{
  "_score": 11.019884,
  "_index": "test",
  "_type": "_doc",
  "_id": "20",
  "_seq_no": null,
  "_primary_term": null,
  "_source": {
    "name": "michael j.",
    "age": "22"
  }
},
{
  "_score": 11.019884,
  "_index": "test",
  "_type": "_doc",
  "_id": "21",
  "_seq_no": null,
  "_primary_term": null,
  "_source": {
    "name": "michael jae.",
    "age": "18"
  }
},
{
  "_score": 11.019884,
  "_index": "test",
  "_type": "_doc",
  "_id": "52",
  "_seq_no": null,
  "_primary_term": null,
  "_source": {
    "name": "michael jay.",
    "age": "69"
  }
},
{
  "_score": 11.019884,
  "_index": "test",
  "_type": "_doc",
  "_id": "33",
  "_seq_no": null,
  "_primary_term": null,
  "_source": {
    "name": "michael jo.",
    "age": "25"
  }
}

相反,我只想打印 "_id", "name", "age"

因此,基于上述相同查询的所需输出将是:

{
    "id": "20",
    "name": "michael j.",
    "age": "22"
},
{
    "id": "21",
    "name": "michael jae.",
    "age": "18"
},
{
    "id": "52",
    "name": "michael jay.",
    "age": "69"
},
{
    "id": "33",
    "name": "michael jo.",
    "age": "25"
}

我写了这段代码:

elasticsearch, err := //actual request to es using olivere

type structuredresponse struct {
    id             string  `json:"id"`
    email          string `json:"email"`
    name           string `json:"name"`
}

var sr []structuredresponse
for _, hit := range elasticsearch.hits.hits {
    source, err := json.marshal(hit.source)
    if err != nil {
        panic(err)
    }


    var sr2 structuredresponse
    err = json.unmarshal(source, &sr2)
    if err != nil {
        panic(err)
    }

    sr = append(sr, sr2)
}

fmt.println(sr)

但我迷路了,我不确定下一步是什么或从这里开始做什么。

更新:

elasticsearch.hits.hits 指向 olivere 包中的此结构:

// SearchHit is a single hit.
type SearchHit struct {
    Score          *float64                       `json:"_score,omitempty"`   // computed score
    Index          string                         `json:"_index,omitempty"`   // index name
    Type           string                         `json:"_type,omitempty"`    // type meta field
    Id             string                         `json:"_id,omitempty"`      // external or internal
    Uid            string                         `json:"_uid,omitempty"`     // uid meta field (see MapperService.java for all meta fields)
    Routing        string                         `json:"_routing,omitempty"` // routing meta field
    Parent         string                         `json:"_parent,omitempty"`  // parent meta field
    Version        *int64                         `json:"_version,omitempty"` // version number, when Version is set to true in SearchService
    SeqNo          *int64                         `json:"_seq_no"`
    PrimaryTerm    *int64                         `json:"_primary_term"`
    Sort           []interface{}                  `json:"sort,omitempty"`            // sort information
    Highlight      SearchHitHighlight             `json:"highlight,omitempty"`       // highlighter information
    Source         json.RawMessage                `json:"_source,omitempty"`         // stored document source
    Fields         map[string]interface{}         `json:"fields,omitempty"`          // returned (stored) fields
    Explanation    *SearchExplanation             `json:"_explanation,omitempty"`    // explains how the score was computed
    MatchedQueries []string                       `json:"matched_queries,omitempty"` // matched queries
    InnerHits      map[string]*SearchHitInnerHits `json:"inner_hits,omitempty"`      // inner hits with ES >= 1.5.0
    Nested         *NestedHit                     `json:"_nested,omitempty"`         // for nested inner hits
    Shard          string                         `json:"_shard,omitempty"`          // used e.g. in Search Explain
    Node           string                         `json:"_node,omitempty"`           // used e.g. in Search Explain

    // HighlightFields
    // SortValues
    // MatchedFilters
}

解决方案


我认为它应该像这样工作:

var srs []structuredresponse
for _, hit := range elasticsearch.hits.hits {
    var sr structuredresponse
    if err := json.unmarshal(hit.source, &sr); err != nil {
        panic(err)
    }

    sr.id = hit.id

    srs = append(srs, sr)
}

该 id 已在命中中可用。请参阅 https://github.com/olivere/elastic/blob/82129300722c28a88207fd5dfed442668d9e264d/search.go#L749

假设 elasticsearch.hits.hits 的字段已导出,则不需要对输出进行封送。

for _, hit := range elasticsearch.Hits.Hits {

    SR = append(SR, StructuredResponse{ID: hit.ID, Email: hit.Email, Name: Hit.Name})
}

这将为您提供一个包含完整项目集 (sr) 的切片。此时,如果您想要这样做,您应该能够将该切片编组为 json。

以上就是《如何重构 JSON/Elasticsearch 响应》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>