Created
May 2, 2023 07:18
-
-
Save Ja7ad/f8ef6821598f17dd2ca184a07aaa2568 to your computer and use it in GitHub Desktop.
elastic example query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"github.com/elastic/go-elasticsearch/v7" | |
"log" | |
"strings" | |
) | |
func main() { | |
es, err := elasticsearch.NewDefaultClient() | |
if err != nil { | |
log.Fatalf("Error creating the client: %s", err) | |
} | |
res, err := es.Search( | |
es.Search.WithContext(context.Background()), | |
es.Search.WithIndex("user", "role"), | |
es.Search.WithBody(strings.NewReader(` | |
{ | |
"query" : { | |
"query_string" : { | |
"query" : "abc*", | |
"fields" : ["public_key", "title"] | |
} | |
} | |
} | |
`)), | |
es.Search.WithTrackTotalHits(true), | |
es.Search.WithPretty(), | |
) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
defer res.Body.Close() | |
fmt.Println(res) | |
} | |
/* | |
[200 OK] { | |
"took" : 1, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 2, | |
"successful" : 2, | |
"skipped" : 0, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : { | |
"value" : 1, | |
"relation" : "eq" | |
}, | |
"max_score" : 1.0, | |
"hits" : [ | |
{ | |
"_index" : "user", | |
"_type" : "_doc", | |
"_id" : "6400c05463ebd97f7afe32f7", | |
"_score" : 1.0, | |
"_source" : { | |
"created_at" : "2023-03-02T15:27:16.338Z", | |
"public_key" : "abcdefgh", | |
"roles" : [ | |
"6400bec2dba089507f853e47" | |
], | |
"status" : 1, | |
"type" : 1 | |
} | |
} | |
] | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment