I would like to ask which boss has used the github.com/olivere/elastic package to operate ElasticSearch?

query: = elastic.NewTermQuery ("category_id", categoryId)
result, e: = ES.GetEs (). Search (). Index ("group_category"). Type ("_ doc") .query (query) .sort ("img_date", false) .Do (context.Background ())

in this way, the search returns all the fields. I want to return only the specified fields. How should I set them? For example, I just want to return to the id field and ask for guidance from all of you!


package main

import (
    "fmt"
    "github.com/olivere/elastic"
    "golang.org/x/net/context"
)

func main() {
    client, err := elastic.NewClient(
        elastic.SetURL("your es"),
    )
    if err != nil {
        // Handle error
    }

    fsc := elastic.NewFetchSourceContext(true).Include("field1", "field12") //.Exclude("*.description")
    query := elastic.NewTermQuery("category_id", 1)

    result, err := client.Search().
        Index("yourindex").
        Type("yourtype").
        Query(query).
        FetchSourceContext(fsc).
        Sort("img_date", false).
        Pretty(true).
        Do(context.Background())
    if err != nil {
        panic(err)
    }

    for i, r := range result.Hits.Hits {
        fmt.Println(i, string(*r.Source))
    }
}
Menu