简体   繁体   English

Elasticsearch查询使用Go Olivere / elastic过滤value1 == value2的文档

[英]Elasticsearch query to filter documents where value1 == value2 with Go olivere/elastic

I need to build query to get documents where doc.value1 == doc.value2 我需要建立查询来获取文档,其中doc.value1 == doc.value2

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

This what I need to build with olivere/elastic, it work if I send it as POST request. 这是我需要使用Olivere / elastic构建的,如果我将其作为POST请求发送,则可以使用。

In golang I have something like 在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")))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Google Go中使用Olivere / Elastic在ElasticSearch中通过查询更新记录 - Updating a record by query in ElasticSearch using olivere/elastic in google go 解析并验证“ key1:value1; key2:value2”字符串有效地进行结构化? - Parse and validate “key1:value1; key2:value2” string to Go struct efficiently? 在Google Go中使用Olivere / Elastic更新ElasticSearch中的记录 - Updating a record in ElasticSearch using olivere/elastic in google go 如何查询使用... olivere / elastic? - how to query using ... olivere / elastic? 将Elasticsearch DSL转换为gopkg.in/olivere/elastic.v5 - Translate Elasticsearch DSL to gopkg.in/olivere/elastic.v5 golang 中的 Elasticsearch 使用 Olivere/弹性 package 不返回任何结果 - Elasticsearch in golang returns no results using olivere/elastic package 如何使用 Go Olivere/elastic 基于多个字段进行排序 - How to sort based on multiple fields with Go olivere/elastic 如何使用 GoLang mongodb 驱动程序在 mongodb 中搜索文档,其中文档中的值为字符串且过滤器具有字符串片段? - How to search for documents in mongodb using GoLang mongodb driver where the value in document is a string and the filter has a slice of string? Elasticsearch Go嵌套查询 - Elasticsearch Go nested query 在 elasticsearch Olivere Package 中插入批量数据失败 - Failed to insert bulk data in elasticsearch Olivere Package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM