douzhang6646 2017-08-01 22:47
浏览 526

如何用Go构建Elasticsearch查询?

I'm using olivere's Elasticsearch library for Go - https://github.com/olivere/elastic

I'm not able to properly construct the search query, it keeps returning 0 hits.

termQuery := elasticClient.NewTermQuery("hash", "hashedID")
fmt.Println(termQuery)
searchResult, err := qs.client.Search().Index("someIndex").
    Type("node").
    Query(termQuery).
    Pretty(true).
    Do(ctx)
if err != nil {
    return nil
}

searchResult.Hits.TotalHits gives 0 hits even though there is data. The data is in an Elasticsearch server running on my local machine which I can see if I run a REST API call:

{
   "_index": "someIndex",
   "_type": "node",
   "_id": "hashedID",
   "_score": 1,
   "_source": {
   "node": "test",
   "hash": "hashedID",
   "active": true
}

How can I fix my Search query?

  • 写回答

1条回答 默认 最新

  • doutany76678 2017-08-02 08:59
    关注

    Your go script is just fine and it works. The problem is with the query itself. This is what you are executing against elasticsearch. Try it directly against elasticsearch (via Sense, Kibana or similar tool) and check if it returns what you expect:

    POST someIndex/node/_search
    {
        "query": {
            "term": {
               "hash": "hashedID"
            }
        }
    }
    

    Solution:

    I suspect that hash is of type text (using elasticsearch 5). Then you need to query for hash.keyword on a term query instead.

    评论

报告相同问题?

悬赏问题

  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算