I have the following elastic search index definition:
{
"mappings":{
"users":{
"properties":{
"version":{
"type":"text"
},
"event": {
"type": "nested",
"properties": {
"event_type": {"type":"text"},
"timestamp": {"type":"text"}
}
},
"user": {
"type": "nested",
"properties": {
"email": {"type":"text"},
"username": {"type":"text"},
}
},
"details": {"type":"text"}
}
}
}
}
There are no issue when I'm trying to get all the documents, here the code:
searchResult, err := l.client.Search().
Index(INDEX_NAME). // search in index "twitter"
Type("log").
// From(0).Size(10). // take documents 0-9
// Pretty(true). // pretty print request and response JSON
Do(c) // execute
if err != nil {
log.Println("Error while getting results: ", err)
return &HistoryResponse{}, err
}
var lg LogElasticStruct
for _, item := range searchResult.Each(reflect.TypeOf(lg)) {
log.Println(item)
if t, ok := item.(LogElasticStruct); ok {
log.Println("LogStruct by: ", t.User.Email, t.Event.EventType)
}
}
It outputs correclty the data I have inside the index. The issue comes when I try to get only the documents that match with the email property.
I've tried the following solution, using BoolQuery
query := elastic.NewBoolQuery()
query = query.Must(elastic.NewTermQuery("user.email", evt.Email))
searchResult, err := l.client.Search().
Index(INDEX_NAME). // search in index "twitter"
Type("log").
Query(query). // specify the query
// Pretty(true). // pretty print request and response JSON
Do(c) // execute
It returns zero document even if there are document that match with the Email inside the evt struct. Any idea?
I've also printed the query that the go package has built:
{
"bool": {
"must": {
"term": {
"user.email": "an@ema.il"
}
}
}
Forogt to says: I'm using the olivere/elastic
package