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?
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"
}
}
}
I suspect that hash is of type text
(using elasticsearch 5). Then you need to query for hash.keyword
on a term query instead.