├── CONTRIBUTING.md ├── ElasticFactory.php ├── Example.php ├── README.md └── composer.json /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to a Project 2 | 3 | Now that you’ve found the material for understanding the project, here is how 4 | you can take action. 5 | 6 | ## Create an Issue 7 | 8 | If you find a bug in a project you’re using (and you don’t know how to fix it), have trouble following the documentation or have a question about the project – create an issue! There’s nothing to it and whatever issue you’re having, you’re likely not the only one, so others will find your issue helpful, too. For more information on how issues work, check out our [Issues guide](http://guides.github.com/features/issues). 9 | 10 | ### Issues Pro Tips 11 | 12 | - **Check existing issues** for your issue. Duplicating an issue is slower for both parties so search through open and closed issues to see if what you’re running into has been addressed already. 13 | 14 | - **Be clear** about what your problem is: what was the expected outcome, what happened instead? Detail how someone else can recreate the problem. 15 | 16 | - **Link to demos** recreating the problem on things like [JSFiddle](JSFiddle) or [CodePen](http://codepen.io/). 17 | 18 | - **Include system details** like what the browser, library or operating system you’re using and its version. 19 | 20 | - **Paste error output** or logs in your issue or in a [Gist](http://gist.github.com/). If pasting them in the issue, wrap it in three backticks: ` ``` ` so that it renders nicely. 21 | 22 | ## Pull Request 23 | 24 | If you’re able to patch the bug or add the feature yourself – fantastic, make a pull request with the code! Be sure you’ve read any documents on contributing, understand the license and have signed a CLA if required. Once you’ve submitted a pull request the maintainer(s) can compare your branch to the existing one and decide whether or not to incorporate (pull in) your changes. 25 | 26 | ### Pull Request Pro Tips 27 | 28 | - **[Fork](http://guides.github.com/activities/forking/) the repository** and clone it locally. Connect your local to the original ‘upstream’ repository by adding it as a remote. Pull in changes from ‘upstream’ often so that you stay up to date so that when you submit your pull request, merge conflicts will be less likely. See more detailed instructions [here](https://help.github.com/articles/syncing-a-fork). 29 | 30 | - **Create a [branch](http://guides.github.com/introduction/flow/)** for your edits. 31 | 32 | - **Be clear** about what problem is occurring and how someone can recreate that problem or why your feature will help. Then be equally as clear about the steps you took to make your changes. 33 | 34 | - **It’s best to test**. Run your changes against any existing tests if they exist and create new ones when needed. Whether tests exist or not, make sure your changes don’t break the existing project. 35 | 36 | - **Include screenshots** of the before and after if your changes include differences in HTML/CSS. Drag and drop the images into the body of your pull request. 37 | 38 | - **Contribute in the style of the project** to the best of your abilities. This may mean using indents, semi colons or comments differently than you would in your own repository, but makes it easier for the maintainer to merge, others to understand and maintain in the future. 39 | 40 | ## Open Pull Requests 41 | 42 | Once you’ve opened a pull request a discussion will start around your proposed changes. Other contributors and users may chime in, but ultimately the decision is made by the maintainer(s). You may be asked to make some changes to your pull request, if so, add more commits to your branch and push them – they’ll automatically go into the existing pull request. 43 | 44 | ![](https://guides.github.com/activities/contributing-to-open-source/convo.png) 45 | 46 | If your pull request is merged – great! If it is not, no sweat, it may not be what the project maintainer had in mind, or they were already working on it. This happens, so our recommendation is to take any feedback you’ve received and go forth and pull request again – or create your own open source project. 47 | 48 | [Source Reference](https://guides.github.com/activities/contributing-to-open-source/#contributing) 49 | -------------------------------------------------------------------------------- /ElasticFactory.php: -------------------------------------------------------------------------------- 1 | index = $index; 18 | $this->client = new Elasticsearch\Client(); 19 | 20 | return $this; 21 | } 22 | 23 | 24 | //create a index into my_index/my_type/ 25 | public function create() 26 | { 27 | $params['index'] = $this->index; 28 | $params['type'] = $this->type; 29 | $params['body'] = $this->data; 30 | 31 | return $this->client->index($params); 32 | } 33 | 34 | //update a index into my_index/my_type/my_id 35 | public function update() 36 | { 37 | $params['index'] = $this->index; 38 | $params['type'] = $this->type; 39 | $params['body']['doc'] = $this->data; 40 | $params['id'] = $this->id; 41 | 42 | return $this->client->index($params); 43 | } 44 | 45 | //create a query 46 | public function find($query) 47 | { 48 | if($this->type){ 49 | $params['type'] = $this->type; 50 | } 51 | 52 | $params['body']['query'] = $query; 53 | $params['size'] = $this->size; 54 | 55 | return $this->client->search($params); 56 | } 57 | 58 | public function setIndex($index) 59 | { 60 | $this->index = $index; 61 | } 62 | 63 | public function setType($type) 64 | { 65 | $this->type = $type; 66 | } 67 | 68 | public function setId($id) 69 | { 70 | $this->id = $id; 71 | } 72 | 73 | public function setData($data) 74 | { 75 | $this->data = $data; 76 | } 77 | 78 | public function setSize($size) 79 | { 80 | $this->size = $size; 81 | } 82 | 83 | public function getIndex() 84 | { 85 | return $this->index; 86 | } 87 | 88 | public function getType() 89 | { 90 | return $this->type; 91 | } 92 | 93 | public function getId() 94 | { 95 | return $this->id; 96 | } 97 | 98 | public function getData() 99 | { 100 | return $this->data; 101 | } 102 | 103 | public function getSize() 104 | { 105 | return $this->size; 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /Example.php: -------------------------------------------------------------------------------- 1 | setSize(100); 7 | $ef->setType('users'); 8 | 9 | //elasticsearch auto generate a structure map, age is int 10 | //and born is date, this types are auto created into elasticsearch data mapping 11 | $data = [ 12 | 'name'=>'Waldemar Neto', 13 | 'age'=>24, 14 | 'email'=>'waldemarnt@outlook.com', 15 | 'born'=>'1990/01/23' 16 | ]; 17 | 18 | $ef->setData($data); 19 | $return = $ef->create(); 20 | 21 | //we can access the created index id, getting the return for this 22 | 23 | echo $return['_id'].' has been saved
'; 24 | 25 | //now , we can set a id for this object, and change a field value to update 26 | $ef->setId(1); 27 | 28 | $editData = [ 29 | 'name'=>'Waldemar Neto', 30 | 'age'=>24, 31 | 'email'=>'waldemarnt@outlook.com', 32 | 'born'=>'1990/01/23' 33 | ]; 34 | 35 | $ef->setData($editData); 36 | $updateReturn = $ef->update(); 37 | 38 | echo $updateReturn['_id'].' has been updated
'; 39 | 40 | //find by age using match query type 41 | $matchQuery=[ 42 | 'match'=> [ 43 | 'age'=>24 44 | ] 45 | ]; 46 | 47 | $matchData = $ef->find($matchQuery); 48 | 49 | //if have a result, elastic create two objects named as hits, with searched data. 50 | if(isset($matchData['hits']['hits'])){ 51 | foreach ($matchData['hits']['hits'] as $key => $hit) { 52 | echo $hit['_source']['name'].'
'; 53 | } 54 | } 55 | 56 | echo 'example with filters
'; 57 | 58 | //example with filters 59 | $filter = array(); 60 | $filter['term']['name'] = 'waldemar'; 61 | 62 | $query = array(); 63 | $query['match']['age'] = 24; 64 | 65 | $filteredQuery = [ 66 | 'filtered'=>[ 67 | 'filter'=>$filter, 68 | 'query'=>$query 69 | ] 70 | ]; 71 | 72 | 73 | $filteredData = $ef->find($filteredQuery); 74 | 75 | if(isset($filteredData['hits']['hits'])){ 76 | foreach ($filteredData['hits']['hits'] as $key => $hit) { 77 | echo $hit['_source']['name'].'
'; 78 | } 79 | } 80 | 81 | echo 'example with date range filter
'; 82 | //example with date range filter 83 | $filter = array(); 84 | $filter['range']['born'] =[ 85 | 'gte'=>'1990/01/20', 86 | 'lte'=>'1990/01/24' 87 | ]; 88 | 89 | $query = array(); 90 | $query['match_all'][] = []; 91 | 92 | $filteredByBorn = [ 93 | 'filtered'=>[ 94 | 'filter'=>$filter, 95 | 'query'=>$query 96 | ] 97 | ]; 98 | 99 | 100 | $filteredBorn = $ef->find($filteredByBorn); 101 | 102 | if(isset($filteredBorn['hits']['hits'])){ 103 | foreach ($filteredBorn['hits']['hits'] as $key => $hit) { 104 | echo $hit['_source']['name'].'
'; 105 | } 106 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ElasticSearch com PHP 2 | ========= 3 | 4 | Instalação simples 5 | 6 | - Baixe o repositorio 7 | - Rode o composer 8 | - Teste os exemplos 9 | 10 | ElasticSearch PHP e uma biblioteca oficial do elasticsearch para ser usado com php, criei alguns exemplos usando ela. Link da biblioteca [ElasticSearchPHP] [1]: 11 | 12 | Instalação 13 | -------------- 14 | 15 | ```sh 16 | composer install 17 | 18 | ``` 19 | License 20 | ---- 21 | 22 | MIT 23 | 24 | 25 | **Free Software, Hell Yeah!** 26 | 27 | [1]:http://www.elasticsearch.org/guide/en/elasticsearch/client/php-api/current/ 28 | 29 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "require": { 3 | "elasticsearch/elasticsearch": "~1.0" 4 | } 5 | } --------------------------------------------------------------------------------