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
53
Harnessing Elasticsearch with PHP
rvavruch
1
100
Other Decks in Programming
See All in Programming
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
3
700
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.7k
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
300
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
610
Click-free releases & the making of a CLI app
oheyadam
2
120
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
200
EMになってからチームの成果を最大化するために取り組んだこと/ Maximize team performance as EM
nashiusagi
0
100
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
100
OnlineTestConf: Test Automation Friend or Foe
maaretp
0
120
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.5k
Featured
See All Featured
Designing Experiences People Love
moore
138
23k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
130
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
The Language of Interfaces
destraynor
154
24k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
720
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
How to train your dragon (web standard)
notwaldorf
88
5.7k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.2k
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
The Cult of Friendly URLs
andyhume
78
6k
Six Lessons from altMBA
skipperchong
27
3.5k
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 /