Skip to content

Compile protobuf messages to Elasticsearch mappings

License

Notifications You must be signed in to change notification settings

benwebber/protosearch

Repository files navigation

protosearch

Compile protobuf messages to Elasticsearch document mappings.

Example

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"
        }
      }
    }
  }
}

Install

  1. Download latest release of the plugin for your system, or build from source.
  2. Install protoc-gen-protosearch to your $PATH.
  3. Copy protosearch/protosearch.proto to your Protobuf path.

Usage

  1. Annotate your messages. (See reference.)

  2. 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