Compile protobuf messages to Elasticsearch document mappings.
Imagine you have a protobuf message representing a search document.
message Article {
message Author {
optional string uid = 1;
optional string name = 2;
}
optional string uid = 1;
optional string title = 2;
repeated Author authors = 3;
}Annotate the message with protosearch.mapping options to map its fields to mapping field types.
import "protosearch/protosearch.proto";
message Article {
message Author {
string uid = 1 [(protosearch.mapping) = {}];
string name = 2 [(protosearch.mapping).field.type = "text"];
}
string uid = 1 [(protosearch.mapping) = {}];
string title = 2 [(protosearch.mapping).field = {
type: "text"
fields: {
key: "en"
value: {
type: "text"
analyzer: "english"
}
}
}];
repeated Author authors = 3 [
(protosearch.mapping).name = "author",
(protosearch.mapping).field.type = "nested",
];
}Then use protoc-gen-protosearch to compile this to a document mapping:
protoc -I proto/ --plugin=protoc-gen-protosearch --protosearch_out=. proto/article.proto
{
"properties": {
"uid": {
"type": "keyword"
},
"title": {
"type": "text",
"fields": {
"en": {
"type": "text",
"analyzer": "english"
}
}
},
"author": {
"type": "nested",
"properties": {
"uid": {
"type": "keyword"
},
"name": {
"type": "text"
}
}
}
}
}- Download latest release of the plugin for your system, or build from source.
- Install
protoc-gen-protosearchto your$PATH. - Copy
protosearch/protosearch.prototo your Protobuf path.
-
Annotate your messages. (See reference.)
-
Compile a Protobuf file to mappings. The plugin will produce one JSON file for each message type.
protoc -I proto/ --plugin=protoc-gen-protosearch --protosearch_out=. proto/example.proto