Git Product home page Git Product logo

php-elasticsearch's Introduction

php-es

ES, ElasticSearch

a simple tool for elasticsearch operation

使用示例:

  1. 查询 商店中作者 "king" 和 "bible" 两人价格为 50~100的 含有"Elasticsearch" 的书
$es = new ES;
$es->index("store")
->type("books")
->must(function(ES $es) {
		$es->where("author", "in", ["king", "bible"]);
		$es->must(function(ES $es) {
			$es->where("price", "<=", 100);
			$es->where("price", ">=", 50);
			});
		})
->should(function(ES $es) {
		$es->match("title", "Elasticsearch", 2);    //标题最重要
		$es->match("subtitle", "Elasticsearch");    //子标题其次
		$es->should(function(ES $es) {
			$es->match("desc", "Elasticsearch");    //描述 和 内容没那么重要
			$es->match("content", "Elasticsearch");
			});
		})
->sort_score()             //按照内容重要性排序
	->sort('date', 'desc')    //再按发布时间排序
	->search();               //查询数据
  1. 统计 商店中作者名不包含"king"的书的数量
$es = new ES;
$es->index("store")
->type("books")
->must_not(function(ES $es) {
	$es->where("author", "=", "king");
})
->count();
  1. 批量update 和 insert 商店中的书籍信息
// 'id' is a unique key
$data = [
	["id" => 1, "name" => "Elastic Search 中文指南", "author" => "king"],
	["id" => 2, "name" => "Elastic Search 进阶", "author" => "adom"],
	["id" => 3, "name" => "精通 Elastic", "author" => "adom"],
];
$es = new ES;
$es->index("store")
->type("books")
->bulk_upsert($data, "id");
  1. 批量update 和 insert 商店中的书籍信息(组合_id键)
// 'id','store_id' 一起成为一个唯一键
$data = [
	["id" => 1, "store_id"=>1, "name" => "Elastic Search 中文指南", "author" => "king"],
	["id" => 1, "store_id"=>2, "name" => "Elastic Search 进阶", "author" => "adom"],
	["id" => 2, "store_id"=>1, "name" => "精通 Elastic", "author" => "adom"],
];
$es = new ES;
$es->index("store)
->type("books")
->bulk_upsert($data, ["id", "store_id"]);
  1. 获取商店中所有书名
$es = new ES;
$es->index("store")
->type("books")
->select("name as book_name")
->search();
  1. nested query
$es = new ES;
$data = [
	[
		"id" => 1, 
		"tags" => [
			["name" => "tech", "type" => 1],
			["name" => "manual", "type" => 2],
			["name" => "chinese", "type" => 3],
		], 
		"name" => "Elasticsearch 中文指南",
	],
	[
		"id" => 2, 
		"tags" => [
			["name" => "tech", "type" => 1],
			["name" => "manual", "type" => 2],
			["name" => "english", "type" => 3],
		], 
		"name" => "Elasticsearch manual",
	],
];
$es->index("store")->type("books")
->bulk_upsert($data, "id");

$es->nested("tags", function($es) {
	$es->where("tags.name", "=", "tech");
	$es->where("tags.type", "=", "2");
});
  1. 使用别名功能
$es = new ES;
$es->index("fortest_v1")->alias("fortest");  //给fortest_v1创建别名fortest
$es->index("fortest")->re_alias("fortest_v1", "fortest_v2"); //将别名fortest 从 fortest_v1 移除,指向 fortest_v2

php-elasticsearch's People

Contributors

aweneagle avatar

Watchers

James Cloos avatar  avatar

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.