Giter VIP home page Giter VIP logo

elasticsearch-lang-handlebars's Introduction

Elasticsearch Handlebars Lang Plugin

Overview

This plugin add Handlebars language to Elasticsearch.

Version

Version Elasticsearch
master 2.4.X
2.4.0 2.4.0
2.3.0 2.3.1
2.2.0 2.2.2
1.4.1 1.4.0

Issues/Questions

Please file an issue. (Japanese forum is here.)

Installation

Install Handlebars Language Plugin

$ $ES_HOME/bin/plugin install org.codelibs/elasticsearch-lang-handlebars/2.4.0

References

This plugin supports an executable script language(search script is not supported).

Using on Script-based Search Template

Using Script-based Search Template Plugin, you can search by Handlebars template.

GET /_search/template
{
    "lang": "handlebars",
    "template": "{\"query\": {\"match\": {\"title\": \"{{query_string}}\"}}}",
    "params": {
        "query_string": "search for these words"
    }
}

How to Register Helpers

To register your helper, put .js file to $ES_HOME/config/helpers.

$ echo "Handlebars.registerHelper('foobar', function (context) { return \"foobar\";})" > $ES_HOME/config/helpers/foobar.js

elasticsearch-lang-handlebars's People

Contributors

marevol avatar keiichiw avatar

Stargazers

Hossein Taghi-Zadeh avatar  avatar

Watchers

James Cloos avatar  avatar  avatar  avatar  avatar  avatar Matsutani Kenji avatar Koki I. avatar  avatar

elasticsearch-lang-handlebars's Issues

lang: "handlebars" not respected when indexing search templates

When trying to store a handlebars template in the .scripts index the template is validated using mustache which leads to a syntax error.

The following works perfectly:

curl -X POST -d '{{
  "lang": "handlebars",
  "inline": "{\n  \"size\": 0,\n  {{#if filters}}\n  \"query\": {\n    \"filtered\": {\n      \"filter\": {\n        \"bool\": {\n          \"must\": [\n            {{#each filters}}\n            {{#if @index}},
 {{/if}}\n            {{#if value}}\n            { \"term\": { \"{{field}}\": \"{{value}}\" } }\n            {{/if}}\n            {{#if values}}\n            { \"terms\": { \"{{field}}\": [ {{#each values}}{{#if
 @index}},{{/if}}\"{{this}}\"{{/each}} ] } }\n            {{/if}}\n            {{/each}}\n          ]\n        }\n      }\n    }\n  },\n  {{/if}}\n  {{#each aggregationTerms}}\n  {{#if @index}},{{/if}}\n  \"aggs
\": {\n    \"{{this}}\": {\n      \"terms\": {\n        \"field\": \"{{this}}\"\n      }\n  {{/each}}\n  {{#each aggregationTerms}}\n    }\n  }\n  {{/each}}\n}\n",
  "params": {
    "filters": [ { "field": "foo", "values": ["bar"] } ],
    "aggregationTerms": [ "lorum", "epsum" ]
  }
}
}' http://elastic.local:9200/test/test/_search/template

The folllowing return mustache syntax error:

curl -X POST -d '{{
  "lang": "handlebars",
  "template": "{\n  \"size\": 0,\n  {{#if filters}}\n  \"query\": {\n    \"filtered\": {\n      \"filter\": {\n        \"bool\": {\n          \"must\": [\n            {{#each filters}}\n            {{#if @index}},
 {{/if}}\n            {{#if value}}\n            { \"term\": { \"{{field}}\": \"{{value}}\" } }\n            {{/if}}\n            {{#if values}}\n            { \"terms\": { \"{{field}}\": [ {{#each values}}{{#if
 @index}},{{/if}}\"{{this}}\"{{/each}} ] } }\n            {{/if}}\n            {{/each}}\n          ]\n        }\n      }\n    }\n  },\n  {{/if}}\n  {{#each aggregationTerms}}\n  {{#if @index}},{{/if}}\n  \"aggs
\": {\n    \"{{this}}\": {\n      \"terms\": {\n        \"field\": \"{{this}}\"\n      }\n  {{/each}}\n  {{#each aggregationTerms}}\n    }\n  }\n  {{/each}}\n}\n"
}' http://elastic.local:9200/_search/template/test_template
{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Unable to parse [{\n  \"size\": 0,\n  {{#if filters}}\n  \"query\": {\n    \"filtered\": {\n      \"filter\": {\n        \"bool\": {\n          \"must\": [\n            {{#each filters}}\n            {{#if @index}}, {{/if}}\n            {{#if value}}\n            { \"term\": { \"{{field}}\": \"{{value}}\" } }\n            {{/if}}\n            {{#if values}}\n            { \"terms\": { \"{{field}}\": [ {{#each values}}{{#if @index}},{{/if}}\"{{this}}\"{{/each}} ] } }\n            {{/if}}\n            {{/each}}\n          ]\n        }\n      }\n    }\n  },\n  {{/if}}\n  {{#each aggregationTerms}}\n  {{#if @index}},{{/if}}\n  \"aggs\": {\n    \"{{this}}\": {\n      \"terms\": {\n        \"field\": \"{{this}}\"\n      }\n  {{/each}}\n  {{#each aggregationTerms}}\n    }\n  }\n  {{/each}}\n}\n] lang [mustache]"}],"type":"illegal_argument_exception","reason":"Unable to parse [{\n  \"size\": 0,\n  {{#if filters}}\n  \"query\": {\n    \"filtered\": {\n      \"filter\": {\n        \"bool\": {\n          \"must\": [\n            {{#each filters}}\n            {{#if @index}}, {{/if}}\n            {{#if value}}\n            { \"term\": { \"{{field}}\": \"{{value}}\" } }\n            {{/if}}\n            {{#if values}}\n            { \"terms\": { \"{{field}}\": [ {{#each values}}{{#if @index}},{{/if}}\"{{this}}\"{{/each}} ] } }\n            {{/if}}\n            {{/each}}\n          ]\n        }\n      }\n    }\n  },\n  {{/if}}\n  {{#each aggregationTerms}}\n  {{#if @index}},{{/if}}\n  \"aggs\": {\n    \"{{this}}\": {\n      \"terms\": {\n        \"field\": \"{{this}}\"\n      }\n  {{/each}}\n  {{#each aggregationTerms}}\n    }\n  }\n  {{/each}}\n}\n] lang [mustache]","caused_by":{"type":"mustache_exception","reason":"Mismatched start/end tags: if @index != if in query-template:10"}},"status":400}

Short version

lang [mustache]","caused_by":{"type":"mustache_exception","reason":"Mismatched start/end tags: if @index != if in query-template:10"}

The handlebars template

{                                                                                
  "size": 0,                                                                     
  {{#if filters}}                                                                
  "query": {                                                                     
    "filtered": {                                                                
      "filter": {                                                                
        "bool": {                                                                
          "must": [                                                              
            {{#each filters}}                                                    
            {{#if @index}}, {{/if}}                                              
            {{#if value}}                                                        
            { "term": { "{{field}}": "{{value}}" } }                             
            {{/if}}                                                              
            {{#if values}}                                                       
            { "terms": { "{{field}}": [ {{#each values}}{{#if @index}},{{/if}}"{{this}}"{{/each}} ] } }
            {{/if}}                                                              
            {{/each}}                                                            
          ]                                                                      
        }                                                                        
      }                                                                          
    }                                                                            
  },                                                                             
  {{/if}}                                                                        
  {{#each aggregationTerms}}                                                     
  {{#if @index}},{{/if}}                                                         
  "aggs": {                                                                      
    "{{this}}": {                                                                
      "terms": {                                                                 
        "field": "{{this}}"                                                      
      }                                                                          
  {{/each}}                                                                      
  {{#each aggregationTerms}}                                                     
    }                                                                            
  }                                                                              
  {{/each}}                                                                      
}

Anyone have any suggestions on how I investigate this further?

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.