Elasticearch Go Subquery

I am using Olivere flexible Go library to run Elastic queries - https://godoc.org/github.com/olivere/elastic#NestedQuery

The data I'm trying to execute is as follows:

"_source": { "field1": "randVal1", "field2": "randVal2", "nestedfield": { "ind1": "val1" } } 

I am trying to run a query on nestedfield by calling NestedQuery from the Elastic Go library like this:

 aquery := elastic.NewTermQuery("ind1", "val1") query := elastic.NestedQuery("nestedfield", aquery) 

But I get an error message:

too many arguments to convert to NestedQuery

I am trying to get all documents where ind1 of nestedfield is val1 . Thank you for your help in building this request.

EDIT:

I changed it to NewNestedQuery and now it does not give this error. However, it does not return any results, even though this document exists in the index, and I can request non-nested fields.

I tried this:

 aquery := elastic.NewTermQuery("ind1", "val1") query := elastic.NewNestedQuery("nestedfield", aquery) 

And this:

 query := elastic.NewNestedQuery("nestedfield", elastic.NewMatchQuery("nestedfield.ind1", "val1")) 

But they both give 0 results. Any idea what I'm doing wrong?

EDIT No. 2

Display:

 "field1": { "type": "string" }, "field2": { "type": "string" }, "nestedfield": { "type": "nested" } 

What ultimately happened:

 query := elastic.NewMatchQuery("nestedfield.ind1", "val1") 

I managed to add additional fields to the "nested field" and make such requests as:

 query := elastic.NewBoolQuery().Filter(elastic.NewMatchQuery("nestedfield.ind1", "val1"), elastic.NewMatchQuery("nestedfield.ind2", "val2")) 
+8
go elasticsearch elasticsearch-plugin elasticsearch-query
source share
1 answer

It looks like it should be:

 q := elastic.NewTermQuery("nestedfield.ind1", value) nq := elastic.NewNestedQuery("nestedfield", q) 
  • NestedQuery is a type, not a function.
  • NewTermQuery needs to accept a value from json, not a string const
  • You need to parse json source code to get value from ind1

Edited to fix NewTermQuery also in accordance with the comments below. If this still does not work, specify the full code that you use to parse the source and get an error, since you are not giving enough details to guess the problem.

+2
source share

All Articles