Code Monkey home page Code Monkey logo

golang-struct-to-elastic-mapping's Introduction

Golang struct to elastic mapping

Terms of use[?]

By using this project or its source code, for any purpose and in any shape or form, you grant your implicit agreement to all the following statements:

  • You condemn Russia and its military aggression against Ukraine
  • You recognize that Russia is an occupant that unlawfully invaded a sovereign state
  • You support Ukraine's territorial integrity, including its claims over temporarily occupied territories of Crimea and Donbas
  • You reject false narratives perpetuated by Russian state propaganda

To learn more about the war and how you can help, click here. Glory to Ukraine! πŸ‡ΊπŸ‡¦

Examples

package main

type Alias struct {
    Alias string `json:"alias" es:"type:keyword,index:true"`
    Name  string `json:"name" es:"type:text"`
}

type Company struct {
    ID    int    `json:"id" es:"index:true"`
    Alias string `json:"alias" es:"type:keyword,index:true"`
    Name  string `json:"name" es:"type:text"`
}

type Vacancy struct {
	ID              int      `json:"id" es:"index:true"`
	Title           string   `json:"title" es:"type:text"`
	Description     string   `json:"description" es:"type:text"`
	Company         *Company `json:"company"`
	RequiredSkills  []Alias  `json:"required_skills"`
	PreferredSkills []Alias  `json:"preferred_skills"`
	DesiredSkills   []Alias  `json:"desired_skills"`
}
{
  "mappings": {
    "properties": {
      "id": {
        "type": "integer",
        "index": true
      },
      "title": {
        "type": "text"
      },
      "description": {
        "type": "text"
      },
      "company": {
        "type": "nested",
        "properties": {
          "id": {
            "type": "integer",
            "index": true
          },
          "alias": {
            "type": "keyword",
            "index": true
          },
          "name": {
            "type": "text"
          }
        }
      },
      "required_skills": {
        "type": "nested",
        "properties": {
          "alias": {
            "type": "keyword",
            "index": true
          },
          "name": {
            "type": "text"
          }
        }
      },
      "preferred_skills": {
        "type": "nested",
        "properties": {
          "alias": {
            "type": "keyword",
            "index": true
          },
          "name": {
            "type": "text"
          }
        }
      },
      "desired_skills": {
        "type": "nested",
        "properties": {
          "alias": {
            "type": "keyword",
            "index": true
          },
          "name": {
            "type": "text"
          }
        }
      }
    }
  }
}
package main

import (
	"testing"

	"github.com/YaroslavPodorvanov/golang-struct-to-elastic-mapping/generator"

	"github.com/stretchr/testify/require"
)

func TestExample(t *testing.T) {
	// language=JSON
	const expected = `...`

	var result, err = generator.Generate(&Vacancy{})

	require.NoError(t, err)
	require.Equal(t, expected, string(result))
}

Original

Elasticsearch Guide

golang-struct-to-elastic-mapping's People

Contributors

resilientecho avatar valllabh avatar yaroslavpodorvanov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

golang-struct-to-elastic-mapping's Issues

Generate mapping like https://github.com/uber-go/mock

Example

testmocks:
	# go install go.uber.org/mock/mockgen@latest
	mockgen \
		-build_flags=--mod=mod \
		-destination=internal/mocks/mock_any_repository.go \
		-package mocks github.com/readytotouch-yaaws/yaaws-go/internal/domain AnyRepository

Kind converter

var kindConverter = NewKindConverter(map[reflect.Kind]string{
	reflect.Bool:                       "boolean",
	reflect.Int:                        "integer",
	reflect.Int8:                       "byte", // A signed 8-bit integer with a minimum value of -128 and a maximum value of 127.
	reflect.Int16:                      "short",
	reflect.Int32:                      "integer",
	reflect.Int64:                      "long",
	reflect.Uint:                       "unsigned_long",
	reflect.Uint8:                      "short",
	reflect.Uint16:                     "integer",
	reflect.Uint32:                     "unsigned_long",
	reflect.Uint64:                     "unsigned_long",
	reflect.Float32:                    "float",
	reflect.Float64:                    "double",
	reflect.String:                     "text",
	reflect.TypeOf(time.Time{}).Kind(): "date",
})

var generator = generator.NewGenerator(kindConverter)

generator.Generate(&Tweet{})

Generate mapping for []int

Now got error

type Company struct {
	ID            int      `json:"id" es:"index:true"`
	Alias         string   `json:"alias" es:"type:keyword,index:true"`
	Name          string   `json:"name" es:"type:text"`
	Description   string   `json:"description" es:"type:text"`
	EmployeeCount int      `json:"employee_count" es:"index:false"`
	URLs          []string `json:"urls"`
}

Expected:

{
  "mappings": {
    "properties": {
      "id": {
        "type": "integer",
        "index": true
      },
      "alias": {
        "type": "keyword",
        "index": true
      },
      "name": {
        "type": "text"
      },
      "description": {
        "type": "text"
      },
      "employee_count": {
        "type": "integer",
        "index": false
      },
      "urls": {
        "type": "text"
      }
    }
  }
}

Nested field type

type Alias struct {
	Alias string `json:"alias"`
	Name  string `json:"name"`
}

type Company struct {
	ID    int    `json:"id"`
	Alias string `json:"alias"`
	Name  string `json:"name"`
}

type Vacancy struct {
	ID              int     `json:"id"`
	Title           string  `json:"title"`
	Description     string  `json:"description"`
	Company         Company `json:"company"`
	RequiredSkills  []Alias `json:"required_skills"`
	PreferredSkills []Alias `json:"preferred_skills"`
	DesiredSkills   []Alias `json:"desired_skills"`
}
{
  "mappings": {
    "properties": {
      "id": {
        "type": "integer"
      },
      "title": {
        "type": "text"
      },
      "description": {
        "type": "text"
      },
      "company": {
        "type": "nested",
        "properties": {
          "id": {
            "type": "integer"
          },
          "alias": {
            "type": "text"
          },
          "name": {
            "type": "text"
          }
        }
      },
      "required_skills": {
        "type": "nested",
        "properties": {
          "alias": {
            "type": "text"
          },
          "name": {
            "type": "text"
          }
        }
      },
      "preferred_skills": {
        "type": "nested",
        "properties": {
          "alias": {
            "type": "text"
          },
          "name": {
            "type": "text"
          }
        }
      },
      "desired_skills": {
        "type": "nested",
        "properties": {
          "alias": {
            "type": "text"
          },
          "name": {
            "type": "text"
          }
        }
      }
    }
  }
}

Tags

type Company struct {
	ID          int    `json:"id" es:"name:id,type:integer,index:true"`
	Alias       string `json:"alias" es:"name:alias,type:keyword,index:true"`
	Name        string `json:"name" es:"name:name,type:text,index:true"`
	Description string `json:"description" es:"name:description,type:text,index:true"`
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.