我需要建立查询来获取文档,其中doc.value1 == doc.value2

{
    "query": {
        "bool" : {
            "filter" : [{
                "script" : {
                    "script" : {
                        "source": "doc['val1'].value == doc['val2'].value",
                        "lang": "painless"
                     }
                }
            }]
        }
    }
}

这是我需要用Olivere / elastic构建的,如果我将其作为POST请求发送,则可以使用。

在golang我有类似的东西

"github.com/olivere/elastic"
...

query := elastic.NewBoolQuery()
// then add something to this query or leave it empty it works fine
// but if I add
query = query.Filter(elastic.NewBoolQuery().Must(elastic.NewScript("doc.['val1'].value == doc.['val2'].value")))
// I'm getting: Error 400 (Bad Request): [source] query malformed,
// no start_object after query name [type=parsing_exception]

// Then i run it like:
client, err := elastic.NewClient()
if err != nil {
    fmt.Println(err)
    return
}
resp, err := client.Search("myIndex").Type("myDoc").Query(query).TrackTotalHits(true).Size(limit).Do(context.Background())
    if err != nil {
        fmt.Println(err)
        return
    }

最佳答案

query = query.Filter(elastic.NewScriptQuery(elastic.NewScript("doc['val1'].value == doc['val2'].value")))

关于go - Elasticsearch查询使用Go Olivere/elastic过滤value1 == value2的文档,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57787772/

10-11 09:06