Git Product home page Git Product logo

elastic2builder's Introduction

elastic2builder

Elasticsearch v2 nodejs query builder.

Install

npm install elastic2builder --save

Usage

var elastic2builder = require('elastic2builder')
var builder = new elastic2builder() // A builder instance.
builder.query('match', 'message', 'this is a test')
var body= builder.returnBody();
// body == {
//   query: {
//     match: {
//      message: {
//			query:'this is a test'
//		}
//     }
//   }
// }

Examples

  • MultiMatch queries
var elastic2builder = require('elastic2builder')
var builder = new elastic2builder() // A builder instance.
builder.query('multimatch',['name','age'], 'this is a test',{type:'best_fields',tie_breaker:0.3})
var body= builder.returnBody();
// {
// 	"query": {
// 		"multi_match": {
// 			"query": "this is a test",
// 			"fields": [
// 				"name",
// 				"age"
// 			],
// 			"type": "best_fields",
// 			"tie_breaker": 0.3
// 		}
// 	}
// }
  • Support for attributes
var elastic2builder = require('elastic2builder')
var builder = new elastic2builder() // A builder instance.
builder.query('match', 'message', 'this is a test',{operator:'and'})
var body= builder.returnBody();
// {
// 	"query": {
// 		"match": {
// 			"message": {
// 				"query": "this is a test",
// 				"operator": "and"
// 			}
// 		}
// 	}
// }
  • Filter
var elastic2builder = require('elastic2builder')
var builder = new elastic2builder() // A builder instance.
builder.filter('match', 'message', 'this is a test',{operator:'and'})
var body= builder.returnBody();

// {
// 	"query": {
// 		"filtered": {
// 			"filter": {
// 				"bool": {
// 					"must": [
// 						{
// 							"match": {
// 								"message": {
// 									"query": "this is a test",
// 									"operator": "and"
// 								}
// 							}
// 						}
// 					],
// 					"should": [],
// 					"must_not": []
// 				}
// 			}
// 		}
// 	}
// }
  • OrFilter and NotFilter
var elastic2builder = require('elastic2builder')
var builder = new elastic2builder() // A builder instance.
builder.filter('match', 'message', 'this is a test')
.orFilter('match', 'name', 'Oliver')
.notFilter('match', 'language', 'french')
var body= builder.returnBody();

// {
// 	"query": {
// 		"filtered": {
// 			"filter": {
// 				"bool": {
// 					"must": [
// 						{
// 							"match": {
// 								"message": {
// 									"query": "this is a test"
// 								}
// 							}
// 						}
// 					],
// 					"should": [
// 						{
// 							"match": {
// 								"name": {
// 									"query": "Oliver"
// 								}
// 							}
// 						}
// 					],
// 					"must_not": [
// 						{
// 							"match": {
// 								"language": {
// 									"query": "french"
// 								}
// 							}
// 						}
// 					]
// 				}
// 			}
// 		}
// 	}
// }
  • Range query
var elastic2builder = require('elastic2builder')
var builder = new elastic2builder() // A builder instance.
builder.filter('range', 'age', {gt:18})
var body= builder.returnBody();
// {
// 	"query": {
// 		"filtered": {
// 			"filter": {
// 				"bool": {
// 					"must": [
// 						{
// 							"range": {
// 								"age": {
// 									"gt": 18
// 								}
// 							}
// 						}
// 					],
// 					"should": [],
// 					"must_not": []
// 				}
// 			}
// 		}
// 	}
// }
  • Aggregation support
var elastic2builder = require('elastic2builder')
var builder = new elastic2builder() // A builder instance.
builder
.query('match', 'message', 'this is a test')
.aggregation('user-aggs','term', 'user',{size:10})
var body= builder.returnBody();
// {
// 	"query": {
// 		"match": {
// 			"message": {
// 				"query": "this is a test"
// 			}
// 		}
// 	},
// 	"aggs": {
// 		"user-aggs": {
// 			"term": {
// 				"field": "user",
// 				"size": 10
// 			}
// 		}
// 	}
// }
  • Sort support
var elastic2builder = require('elastic2builder')
var builder = new elastic2builder() // A builder instance.
builder
.query('match', 'message', 'this is a test')
.sort('name','desc')
var body= builder.returnBody();
// {
// 	"query": {
// 		"match": {
// 			"message": {
// 				"query": "this is a test"
// 			}
// 		}
// 	},
// 	"sort": {
// 		"name": {
// 			"order": "desc"
// 		}
// 	}
// }
  • Pagination support
var elastic2builder = require('elastic2builder')
var builder = new elastic2builder() // A builder instance.
builder
.query('match', 'message', 'this is a test')
.page(0,10) // OR .page(0),since ,10 is default; means page number 0 and page size of 10; equivalent to .from(0).size(10)
var body= builder.returnBody();
// {
// 	"query": {
// 		"match": {
// 			"message": {
// 				"query": "this is a test"
// 			}
// 		}
// 	},
// 	"size": 10,
// 	"from": 0
// }

elastic2builder's People

Contributors

utkarsh85 avatar

Watchers

James Cloos avatar  avatar Mathew avatar

elastic2builder's Issues

.filter vs .query error

I am getting an invalid query when using the .filter with "match"

this.builder.filter('match', 'message', 'this is a test')
results in this output from returnBody(). see the empty {} where "this is a test" should be.

{"query":{"filtered":{"filter":{"bool":{"must":[{"match":{"message":{} }}],"should":[],"must_not":[]}}}}}

Using query instead of filter results in a desired output:
this.builder.query('match', 'message', 'this is a test')

{"query":{"match":{"message":{"query":"this is a test"}}}}

I need to use .filter for some more advanced queries to include range with the match. Is there something I am missing?

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.