4

I am trying to Get data with specific fields from an Index. I am using olivere elastic client. I tried the following code to select Bedrooms,rooms and location fields from property Index.

    query := elastic.NewBoolQuery()

    query = query.Must(elastic.NewTermQuery("rooms", "1"))
    query = query.Must(elastic.NewTermQuery("bedrooms", "1"))

    get1, err := client.Search("Bedrooms", "rooms", "location").
        Index("property").
        //Type("doc").
        Query(query).
        Sort("id", true).
        Size(5000).
        Do(ctx)
    if err != nil {
        // Handle error
        panic(err)
    }

2 Answers 2

4

You can achieve this by using the Fields

query := elastic.NewBoolQuery()

query = query.Must(elastic.NewTermQuery("rooms", "1"))
query = query.Must(elastic.NewTermQuery("bedrooms", "1"))

get1, err := client.Search("Bedrooms", "rooms", "location").
    Index("property").
    //Type("doc").
    Query(query).
    Sort("id", true).
    Fields("field1","field2")
    Size(5000).
    Do(ctx)
if err != nil {
    // Handle error
    panic(err)
}
Sign up to request clarification or add additional context in comments.

2 Comments

Helpful answer, just a version update: Fields is the 1.x/2.x(?) term. DocValueFields is the term that works in 6.x/7.x
Jeez i should read the comments ... I was trying to get the Fields working. So i confirm DocValueFields works with v7
0

I'm using Olivere/elastic v7.0.32 and I couldn't get it to work with DocValueFields.

This worked for me:

query := elastic.NewBoolQuery()
// specify query that you need
query.Must(elastic.NewRangeQuery("expires").Gt(time.Now()))

// provide fields in .Include() function that you wish to return
fsc := elastic.NewFetchSourceContext(true).Include("image", "name")

// create new builder
builder := elastic.NewSearchSource().Query(query).FetchSourceContext(fsc)

searchResults, err := client.
    Search().
    Index("index").
    SearchSource(builder).
    Do(ctx)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.