Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
PHP & ElasticSearch
Search
Rudolf Vavruch
June 11, 2014
Programming
3
130
PHP & ElasticSearch
Describes the process of working with ElasticSearch & PHP.
Rudolf Vavruch
June 11, 2014
Tweet
Share
More Decks by Rudolf Vavruch
See All by Rudolf Vavruch
Domain Driven Design in PHP
rvavruch
0
54
Harnessing Elasticsearch with PHP
rvavruch
1
110
Other Decks in Programming
See All in Programming
STUNMESH-go: Wireguard NAT穿隧工具的源起與介紹
tjjh89017
0
300
ゲームの物理
fadis
3
930
SQLアンチパターン第2版 データベースプログラミングで陥りがちな失敗とその対策 / Intro to SQL Antipatterns 2nd
twada
PRO
38
11k
マイコンでもRustのtestがしたい その2/KernelVM Tokyo 18
tnishinaga
2
1.8k
202507_ADKで始めるエージェント開発の基本 〜デモを通じて紹介〜(奥田りさ)The Basics of Agent Development with ADK — A Demo-Focused Introduction
risatube
PRO
6
1.4k
『リコリス・リコイル』に学ぶ!! 〜キャリア戦略における計画的偶発性理論と変わる勇気の重要性〜
wanko_it
1
410
Strands Agents で実現する名刺解析アーキテクチャ
omiya0555
1
120
なぜあなたのオブザーバビリティ導入は頓挫するのか
ryota_hnk
5
580
Vibe coding コードレビュー
kinopeee
0
430
CEDEC 2025 『ゲームにおけるリアルタイム通信への QUIC導入事例の紹介』
segadevtech
3
820
Scale out your Claude Code ~自社専用Agentで10xする開発プロセス~
yukukotani
9
1.8k
SwiftでMCPサーバーを作ろう!
giginet
PRO
2
230
Featured
See All Featured
The Straight Up "How To Draw Better" Workshop
denniskardys
235
140k
Statistics for Hackers
jakevdp
799
220k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
183
54k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
110
19k
Reflections from 52 weeks, 52 projects
jeffersonlam
351
21k
Mobile First: as difficult as doing things right
swwweet
223
9.9k
How to Ace a Technical Interview
jacobian
278
23k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
The Cost Of JavaScript in 2023
addyosmani
51
8.8k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3k
Being A Developer After 40
akosma
90
590k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Transcript
Apache Lucene ElasticSearch
Prepare repository, install Java & ElasticSearch # echo "deb http://packages.elasticsearch.org/elasticsearch/1.0/debian
\ stable main" > /etc/apt/sources.list.d/elasticsearch.list # wget -O - http://packages.elasticsearch.org/GPG-KEY-elasticsearch | \ apt-key add - # aptitude update && aptitude -y install elasticsearch openjdk-7-jre-headless # update-rc.d elasticsearch defaults 95 10 http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/setup-repositories.html http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/setup-service.html 2 20 /
Configure # vim /etc/elasticsearch/elasticsearch.yml node.name: "Lake Silencio" bootstrap.mlockall: true path.logs:
/var/log/elasticsearch path.data: /var/data/elasticsearch # mkdir -p /var/data/elasticsearch # chown -R elasticsearch:elasticsearch /var/data/elasticsearch http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/setup-configuration.html 3 20 /
Start & test # service elasticsearch start # curl localhost:9200/_nodes/process?pretty
{ "cluster_name" : "elasticsearch", "nodes" : { "EBPb3dI5SbydDNIPLcRVFA" : { "name" : "Lake Silencio", "transport_address" : "inet[/10.0.2.15:9300]", "host" : "debian-7", "ip" : "127.0.1.1", "version" : "1.2.1", "build" : "6c95b75", "http_address" : "inet[/10.0.2.15:9200]", "process" : { "refresh_interval_in_millis" : 1000, "id" : 2049, "max_file_descriptors" : 65535, "mlockall" : true } } } } 4 20 /
None
PHP Install # aptitude update && aptitude -y install php5-curl
# curl -s http://getcomposer.org/installer | php # vim composer.json { "require": { "elasticsearch/elasticsearch": "~1.0.x" } } # php composer.phar install http://www.elasticsearch.org/guide/en/elasticsearch/client/php-api/current/_installation_2.html 6 20 /
Add an index http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-create-index.html require 'vendor/autoload.php'; $params = array(); $params['index']
= 'ducks'; $client = new Elasticsearch\Client(); $result = $client->indices()->create($params); # curl -XPUT 'http://localhost:9200/ducks/' http://www.elasticsearch.org/guide/en/elasticsearch/client/php-api/current/_index_operations.html 7 20 /
$mapping = array( '_source' => array( 'enabled' => true ),
'properties' => array( 'fullname' => array( 'type' => 'string', 'index' => 'no', 'store' => true, ), 'gender' => array('type' => 'string'), 'plumage' => array( 'type' => 'object', 'properties' => array( 'h' => array('type' => 'integer'), 's' => array('type' => 'integer'), 'l' => array('type' => 'integer'), ), ), 'location' => array('type' => 'geo_point'), 'glasses' => array('type' => 'boolean'), 'birthday' => array( 'type' => 'date', 'format' => 'yyyy-MM-dd', ), 'height' => array('type' => 'float'), ) ); $params = array(); $params['index'] = 'ducks'; $params['type'] = 'duck'; $params['body']['duck'] = $mapping; $client = new Elasticsearch\Client(); $result = $client->indices()->putMapping($params); # curl -XPUT 'http://localhost:9200/twitter/tweet/_mapping' -d ' { "tweet" : { "properties" : { "message" : { "type" : "string", "store" : true } } } } ' Map a type 8 20 /
20 9 20 /
Index a document $data = array( 'fullname' => 'Rory The
Duck', 'gender' => 'male', 'plumage' => array( 'h' => 50, 's' => 100, 'l' => 50), 'location' => "-34.086508, 18.45499", 'glasses' => true, 'birthday' => '2012-08-13', 'height' => 30.56, ); $params = array(); $params['body'] = $data; $params['index'] = 'ducks'; $params['type'] = 'duck'; //$params['id'] = 'ducky_id'; $client = new Elasticsearch\Client(); $result = $client->index($params); http://www.elasticsearch.org/guide/en/elasticsearch/client/php-api/current/_index_operations.html array(5) { ["_index"]=> string(5) "ducks" ["_type"]=> string(4) "duck" ["_id"]=> string(22) "OdcAUa75TbaRa3DhRxOS-A" ["_version"]=> int(1) ["created"]=> bool(true) } 10 20 /
Update a document $data = array( 'fullname' => 'Rory The
Duck', 'gender' => 'male', 'plumage' => array( 'h' => 50, 's' => 100, 'l' => 50), 'location' => "-34.086508, 18.45499", 'glasses' => false, 'birthday' => '2012-08-13', 'height' => 30.56, ); $params = array(); $params['body'] = $data; $params['index'] = 'ducks'; $params['type'] = 'duck'; $params['id'] = 'OdcAUa75TbaRa3DhRxOS-A'; $client = new Elasticsearch\Client(); $result = $client->index($params); array(5) { ["_index"]=> string(5) "ducks" ["_type"]=> string(4) "duck" ["_id"]=> string(22) "OdcAUa75TbaRa3DhRxOS-A" ["_version"]=> int(2) ["created"]=> bool(false) } 11 20 /
12 20 /
Perform a simple query $params = array(); $params['index'] = 'ducks';
$params['type'] = 'duck'; $range = array(); $range['gte'] = 30; $range['lte'] = 60; $params['body']['query']['range']['height'] = $range; $client = new Elasticsearch\Client(); $result = $client->search($params); array(4) { ["took"]=> int(122) ... ["hits"]=> array(3) { ... ["hits"]=> array(10) { [0]=> array(5) { ["_index"]=> string(5) "ducks" ["_type"]=> string(4) "duck" ["_id"]=> string(22) "5XH1fhPjRxiHUo7-cVFT-g" ["_score"]=> float(1) ["_source"]=> array(7) { ["fullname"]=> string(14) "Peter The Duck" ... ["height"]=> float(50.14) } [1]=> ... } } } } 13 20 /
14 20 /
15 20 /
Perform a complex query - Queries $params['index'] = 'ducks'; $params['type']
= 'duck'; //$params['body']['from'] = 0; $params['body']['size'] = 1000; $queries = array(); $query = array(); $query['term'] = array(); $query['term']['gender'] = 'female'; $queries[] = $query; $query = array(); $query['range']['plumage.h']['gte'] = 10; $query['range']['plumage.h']['lte'] = 20; $query['range']['plumage.s']['gte'] = 70; $query['range']['plumage.s']['lte'] = 80; $query['range']['plumage.l']['gte'] = 40; $query['range']['plumage.l']['lte'] = 50; $queries[] = $query; $query = array(); $query['range']['birthday']['gte'] = '2011-01-01'; $query['range']['birthday']['lte'] = '2014-01-01'; $queries[] = $query; $query = array(); $query['range']['height']['gte'] = 30; $query['range']['height']['lte'] = 60; $queries[] = $query; $params['body']['query']['filtered']['query']['bool']['must'] = $queries; 16 20 /
Perform a complex query - Filters $filters = array(); $filter
= array(); $filter['term']['glasses'] = true; $filters[] = $filter; $filter = array(); $filter['geo_distance']['distance'] = '2km'; $filter['geo_distance']['location'] = "-34.086, 18.454"; $filters[] = $filter; $params['body']['query']['filtered']['filter']['and'] = $filters; $client = new Elasticsearch\Client(); $result = $client->search($params); http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html 17 20 /
18 20 /
None
Rudolf Vavruch
[email protected]
http://ekaya.co 20 20 /