douniang3866
douniang3866
2019-09-04 11:49
浏览 1.0k

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

I need to build query to get documents where 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.

In golang I have something like

"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
    }

图片转代码服务由CSDN问答提供 功能建议

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

  {
“ query”:{
“ bool”:{
“ filter”:[{
“ script”:{
“ script”:{
“ source”:  “ doc ['val1']。value == doc ['val2']。value”,
“ lang”:“无痛” 
} 
} 
}] 
} 
} 
} \  n   
 
 

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

在golang中 我有类似的东西

 “ github.com/olivere/elastic” 
 ... 
 
query:= elastic。  NewBool​​Query()
 //,然后向此查询中添加一些内容或将其保留为空,则效果很好
 //,但是如果我添加
query = query.Filter(elastic.NewBool​​Query()。Must(elastic.NewScript(“ doc。  ['val1']。value == doc。['val2']。value“))))
 //我得到:错误400(错误请求):[源] que  ry格式错误,
 //查询名称后没有start_object [type = parsing_exception] 
 
 //然后像这样运行它:
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 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqwh1209
    dqwh1209 2019-09-04 12:52
    已采纳
    query = query.Filter(elastic.NewScriptQuery(elastic.NewScript("doc['val1'].value == doc['val2'].value")))
    
    点赞 评论

相关推荐