-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Golang struct to elastic mapping #694
Comments
There is no support for that in Elastic, but you could of course do that with something like However, I have doubts about this approach. The problem is that mapping can get rather complex, and you need to put all that subtleties into a struct field. Therefore, I typically take a different approach where I prefill all the different types of fields into a dynamic mapping/template, and use suffixes to make ES automatically pick up the correct type. Let me know if you need more information about that approach. |
Any solution for this? @olivere Will you please elaborate this - 'dynamic mapping/template' |
@kashid16 I don't see a necessity for doing this. In all but the very basic mappings, the Go structs for the results are typically very different to the mapping. Your mapping is typically rather complex, uses one field with different types, different analyzers, is configured to copy the content to some other field etc. In most of my use cases there's not even a 1:1 mapping between the field in the result set and the field in the mapping. ES is not a database. It's rather convenient to simply pass the mapping into the raw body, maybe configured by a Go template, like illustrated here. |
|
@ablegao Reflection-based mapping breaks down as soon as you don't have a simple 1:1 mapping. E.g. how do you map a field that is just a tiny bit more complex, like: "name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 100
}
}
} I still think the best way today is to have a "handwritten" mapping that uses Go templates, like I used here. On top of that, please see how to specify a list of re-usable fields with |
Is there any way to convert a golang struct to elastic mapping probably using some custom tags or something?
The text was updated successfully, but these errors were encountered: