dongmou3615 2017-09-26 18:48
浏览 150

Elasticsearch Go嵌套查询

I'm using olivere's elastic Go library to run Elastic queries - https://godoc.org/github.com/olivere/elastic#NestedQuery

The data I'm trying to query on looks like this:

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

I'm trying to run a query on the nestedfield using the NestedQuery call from the Elastic Go library like so:

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

But I get an error stating:

too many arguments to conversion to NestedQuery

I'm trying to retrieve all documents where the ind1 of nestedfield is val1. Would appreciate any help in constructing this query.

EDIT:

I changed it to NewNestedQuery and now it doesn't give that error. However, it is not returning any results, even though that document exists in the index and I am able to query on the 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 #2

The mapping is:

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

What eventually worked was this:

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

I was able to add additional fields to 'nestedfield' and do queries like:

query := elastic.NewBoolQuery().Filter(elastic.NewMatchQuery("nestedfield.ind1", "val1"), elastic.NewMatchQuery("nestedfield.ind2", "val2"))
  • 写回答

1条回答 默认 最新

  • douyanning3724 2017-09-26 20:42
    关注

    Looks like that should be:

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

    Edited to fix NewTermQuery too as per comments below. If that still doesn't work provide the full code you're using to parse source and get the error as you don't give enough detail here to guess at the problem.

    评论

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接