Slide 1

Slide 1 text

better searching with elasticsearch

Slide 2

Slide 2 text

richard miller @mr_r_miller richarddmiller.co.uk sensiolabs uk

Slide 3

Slide 3 text

why worry about search?

Slide 4

Slide 4 text

ok, so what might someone want from search?

Slide 5

Slide 5 text

Given I am a hungry person When I search for somewhere to eat Then I should see relevant results And the results should be ordered by relevance

Slide 6

Slide 6 text

yep, no problem

Slide 7

Slide 7 text

Given I am a hungry person When I search for somewhere to eat Then I should see results for relevant reviews

Slide 8

Slide 8 text

sure

Slide 9

Slide 9 text

Given I am a hungry person When I search for somewhere to eat Then I should see results for similar words

Slide 10

Slide 10 text

yeah, we can do that do that

Slide 11

Slide 11 text

Given I am a hungry person When I search for somewhere to eat near me Then I should see results close to my location

Slide 12

Slide 12 text

that’s certainly possible

Slide 13

Slide 13 text

Given I am a hungry person When I typo entering my search terms Then I should see “did you mean?” suggestions

Slide 14

Slide 14 text

well other sites do that, so why not?

Slide 15

Slide 15 text

Given I am a hungry person When I start entering my search terms Then I should see suggestions straight away

Slide 16

Slide 16 text

ok, ok

Slide 17

Slide 17 text

Given I am a hungry person When I start searching for somewhere to eat Then I should be able to filter my results

Slide 18

Slide 18 text

now this we can do

Slide 19

Slide 19 text

Given I am a hungry person When I start view details of somewhere to eat Then I should see suggestions of similar eateries

Slide 20

Slide 20 text

yep, yep

Slide 21

Slide 21 text

Given I am a restaurateur When I upload my PDF menu Then hungry people should be able to search it

Slide 22

Slide 22 text

hmm...

Slide 23

Slide 23 text

well let’s get the easy stuff out of the way

Slide 24

Slide 24 text

Given I am a hungry person When I search for somewhere to eat Then I should see relevant results And the results should be ordered by relevance

Slide 25

Slide 25 text

SELECT * FROM eateries WHERE NAME LIKE “%searchterm%”

Slide 26

Slide 26 text

multiple searchterms?

Slide 27

Slide 27 text

...WHERE ( name LIKE “%multiple%” OR name LIKE “%searchterm%”)

Slide 28

Slide 28 text

but wait...

Slide 29

Slide 29 text

but wait... ...multiple fields

Slide 30

Slide 30 text

...WHERE ( name LIKE “%multiple%” OR name LIKE “%searchterm%”...

Slide 31

Slide 31 text

...OR desc LIKE “%multiple%” OR desc LIKE “%searchterm%”)

Slide 32

Slide 32 text

Given I am a hungry person When I search for somewhere to eat Then I should see relevant results And the results should be ordered by relevance

Slide 33

Slide 33 text

ORDER?

Slide 34

Slide 34 text

Given I am a hungry person When I search for somewhere to eat Then I should see relevant results And the results should be ordered by relevance

Slide 35

Slide 35 text

the

Slide 36

Slide 36 text

the with

Slide 37

Slide 37 text

the with be

Slide 38

Slide 38 text

the but with be

Slide 39

Slide 39 text

the but with be it

Slide 40

Slide 40 text

it the but with be food

Slide 41

Slide 41 text

oh ok,

Slide 42

Slide 42 text

oh ok, full text search...

Slide 43

Slide 43 text

SELECT * FROM eateries MATCH (name, desc) AGAINST( “multiple searchterms”)

Slide 44

Slide 44 text

that should do!

Slide 45

Slide 45 text

what was left?

Slide 46

Slide 46 text

Given I am a hungry person When I search for somewhere to eat Then I should see results for relevant reviews

Slide 47

Slide 47 text

...LEFT JOIN...

Slide 48

Slide 48 text

Given I am a hungry person When I search for somewhere to eat Then I should see results for similar words

Slide 49

Slide 49 text

grilled

Slide 50

Slide 50 text

grilling grilled

Slide 51

Slide 51 text

grilling grilled grills

Slide 52

Slide 52 text

grill grilling grilled grills

Slide 53

Slide 53 text

Given I am a hungry person When I search for somewhere to eat near me Then I should see results close to my location

Slide 54

Slide 54 text

elasticsearch

Slide 55

Slide 55 text

distributed, schemaless, document oriented, Lucene based search engine with a REST API

Slide 56

Slide 56 text

github

Slide 57

Slide 57 text

stackoverflow

Slide 58

Slide 58 text

gov.uk

Slide 59

Slide 59 text

foursquare

Slide 60

Slide 60 text

soundcloud

Slide 61

Slide 61 text

wget https://download.elasticsearch.org/ elasticsearch/elasticsearch/ elasticsearch-0.90.5.tar.gz tar -zxvf elasticsearch-0.90.5.tar.gz cd elasticsearch-0.90.5

Slide 62

Slide 62 text

bin/elasticsearch

Slide 63

Slide 63 text

Lucene based

Slide 64

Slide 64 text

request curl -X GET http://localhost:9200/

Slide 65

Slide 65 text

{ "ok" : true, "status" : 200, "name" : "Mr. Wu", "version" : { "number" : "0.90.5", "build_hash" : "c8714e8...dedee", "build_timestamp" : "2013-09-17T12:50:20Z", "build_snapshot" : false, "lucene_version" : "4.4" }, "tagline" : "You Know, for Search" } response

Slide 66

Slide 66 text

database index

Slide 67

Slide 67 text

table type

Slide 68

Slide 68 text

record document

Slide 69

Slide 69 text

distributed

Slide 70

Slide 70 text

0 1 2 3 4 Instance

Slide 71

Slide 71 text

0 1 Instance 1 Instance 2 Instance 3 2 3 4

Slide 72

Slide 72 text

0 1 2 3 4 Instance 1 Instance 2 Instance 3 0 1 2 3 4

Slide 73

Slide 73 text

document oriented

Slide 74

Slide 74 text

REST API

Slide 75

Slide 75 text

curl -XPOST 'http://localhost:9200/eatly/eateries/' -d '{ "name" : "Jeff''s Burgers", "desc" : "Blah Blah dirty burgers..." }'

Slide 76

Slide 76 text

curl -XPOST 'http://localhost:9200/eatly/eateries/' -d '{ "name" : "Jeff''s Burgers", "desc" : "Blah Blah dirty burgers..." }'

Slide 77

Slide 77 text

curl -XPOST 'http://localhost:9200/eatly/eateries/' -d '{ "name" : "Jeff''s Burgers", "desc" : "Blah Blah dirty burgers..." }'

Slide 78

Slide 78 text

{"ok":true,"_index":"eatly","_type":"eateries","_id":"EGHS ABBxQkaPR_DJxPxXZA","_version":1}

Slide 79

Slide 79 text

?pretty=true

Slide 80

Slide 80 text

{ "ok" : true, "_index" : "eatly", "_type" : "eateries", "_id" : "cmElnobYSy6386TOUVbGZQ", "_version" : 1 }

Slide 81

Slide 81 text

curl -XPUT 'http://localhost:9200/eatly/eateries/1' -d '{ "name" : "Jeff''s Burgers", "desc" : "Blah Blah dirty burgers..." }'

Slide 82

Slide 82 text

{ "ok" : true, "_index" : "eatly", "_type" : "eateries", "_id" : "1", "_version" : 1 }

Slide 83

Slide 83 text

curl -XGET 'http://localhost:9200/eatly/eateries/1'

Slide 84

Slide 84 text

{ "_index" : "eatly", "_type" : "eateries", "_id" : "1", "_version" : 1, "exists" : true, "_source" : { "name" : "Jeff's Burgers", "desc" : "Blah Blah dirty burgers..." } }

Slide 85

Slide 85 text

curl -XPUT 'http://localhost:9200/eatly/eateries/1' -d '{ "name" : "Jeff''s Burger Joint" }'

Slide 86

Slide 86 text

{ "ok" : true, "_index" : "eatly", "_type" : "eateries", "_id" : "1", "_version" : 2 }

Slide 87

Slide 87 text

curl -XGET 'http://localhost:9200/eatly/eateries/1'

Slide 88

Slide 88 text

{ "_index" : "eatly", "_type" : "eateries", "_id" : "1", "_version" : 2, "exists" : true, "_source" : { "name" : "Jeff's Burger Joint" } }

Slide 89

Slide 89 text

curl -XDELETE 'http://localhost:9200/eatly/eateries/1'

Slide 90

Slide 90 text

curl -XGET 'http://localhost:9200/eatly/eateries/1'

Slide 91

Slide 91 text

{ "_index" : "eatly", "_type" : "eateries", "_id" : "1", "_version" : 2, "exists" : false }

Slide 92

Slide 92 text

search engine

Slide 93

Slide 93 text

curl -XGET '.../eatly/eateries/_search?q=name:Burger'

Slide 94

Slide 94 text

{ "took" : 7, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 0.15342641, "hits" : [ { "_index" : "eatly", "_type" : "eateries", "_id" : "1", "_score" : 0.15342641, "_source" : {"name" : "Jeff's Burger Joint"} } ] } }

Slide 95

Slide 95 text

http://localhost:9200/eatly/cafes,pubs/_search?q=Burger http://localhost:9200/eatly/_search?q=name:Burger http://localhost:9200/eatly,cookr/_search?q=name:Burger http://localhost:9200/_all/pubs/_search?q=name:Burger http://localhost:9200/_search?q=name:Burger'

Slide 96

Slide 96 text

schemaless?

Slide 97

Slide 97 text

schemaless?

Slide 98

Slide 98 text

curl -XPUT 'http://localhost:9200/eatly/' -d '{ "settings" : { "index" : { "number_of_shards" : 4 "number_of_replicas" : 2 } } }'

Slide 99

Slide 99 text

Given I am a hungry person When I search for somewhere to eat Then I should see relevant results And the results should be ordered by relevance

Slide 100

Slide 100 text

name > desc

Slide 101

Slide 101 text

curl -XPUT '.../eatly/eateries/_mapping' -d '{ "eateries" : { "properties" : { "name" : {"type" : "string", "boost" : "1.5"}, "desc" : {"type" : "string"} } } }'

Slide 102

Slide 102 text

Given I am a hungry person When I search for somewhere to eat Then I should see results for relevant reviews

Slide 103

Slide 103 text

curl -XPUT '.../eatly/eateries/_mapping' -d '{ "eateries" : { "properties" : { "name" : {"type" : "string", "boost" : "1.5"}, "desc" : {"type" : "string"} "reviews" : { "properties" : { "review" : {"type" : "string"}, "reviewer" : {"type" : "string"} } } } } }'

Slide 104

Slide 104 text

curl -XPOST 'http://localhost:9200/eatly/eateries/' -d '{ "name" : "Jeff''s Burgers", "desc" : "Blah Blah dirty burgers...", "reviews" : [ { "review" : "Yadda, yadda, yadda", "reviewer" : "John Smith" }, { "review" : "na na na", "reviewer" : "Billy Badger" } ] }'

Slide 105

Slide 105 text

curl -XGET '.../eateries/_search?q=reviews.review:Burger'

Slide 106

Slide 106 text

Given I am a hungry person When I search for somewhere to eat Then I should see results for similar words

Slide 107

Slide 107 text

burgers I really enjoyed Jeff's Burger Joint, the burgers were excellent! can't fault it standard tokenizer I really enjoyed Jeff's Burger Joint the were excellent can't fault it

Slide 108

Slide 108 text

excellent! burgers I really enjoyed Jeff's Burger Joint, the burgers were excellent! can't fault it whitespace tokenizer I really enjoyed Jeff's Burger Joint, the were can't fault it

Slide 109

Slide 109 text

burgers I really enjoyed Jeff's Burger Joint, the burgers were excellent! can't fault it letter tokenizer I really enjoyed Jeff Burger Joint the were excellent can fault it s t

Slide 110

Slide 110 text

burgers I really enjoyed Jeff's Burger Joint, the burgers were excellent! can't fault it standard tokenizer + lowercase filter i really enjoyed jeff's burger joint the were excellent can't fault it

Slide 111

Slide 111 text

burgers I really enjoyed Jeff's Burger Joint, the burgers were excellent! can't fault it standard tokenizer + lowercase filter + stop filter i really enjoyed jeff's burger joint were excellent can't fault

Slide 112

Slide 112 text

burger I really enjoyed Jeff's Burger Joint, the burgers were excellent! can't fault it standard tokenizer + lowercase filter + stop filter + stemmer filter i realli enjoi jeff burger joint were excel can't fault

Slide 113

Slide 113 text

curl -XPUT 'http://localhost:9200/eatly/' -d '{ "settings" : { "index" : { "analysis" : { "analyzer" : { "snowball_analyzer" : { "type" : "snowball" } } } } } }'

Slide 114

Slide 114 text

curl -XPUT '.../eatly/eateries/_mapping' -d '{ "eateries" : { "properties" : { "name" : {"type" : "string", "boost" : "1.5"}, "desc" : { "type" : "string", "analyzer": "snowball_analyzer" } } } }'

Slide 115

Slide 115 text

curl -XPUT 'http://localhost:9200/eatly/' -d '{ "settings" : { "index" : { "analysis" : { "analyzer" : { "custom_analyzer" : { "type" : "custom", "tokenizer" : "lowercase" "filter" : ["stop", "extra_stop"] } }, "filter" : { "extra_stop":{ "type" : "stop", "stopwords" : ["food"] } } } } } }'

Slide 116

Slide 116 text

curl -XPUT '.../eatly/eateries/_mapping' -d '{ "eateries" : { "properties" : { "name" : {"type" : "string", "boost" : "1.5"}, "desc" : { "type" : "string", "analyzer": "custom_analyzer" } } } }'

Slide 117

Slide 117 text

query DSL

Slide 118

Slide 118 text

curl -XGET '.../eatly/eateries/_search' -d '{ "query" : { "match" : { "name" : "burger" } } }'

Slide 119

Slide 119 text

curl -XGET '.../eatly/eateries/_search' -d '{ "query" : { "match" : { "name" : "burger bar" } } }'

Slide 120

Slide 120 text

curl -XGET '.../eatly/eateries/_search' -d '{ "query" : { "match" : { "name" : { "query" : "burger bar", "operator" : "and" } } } }'

Slide 121

Slide 121 text

curl -XGET '.../eatly/eateries/_search' -d '{ "query" : { "multi_match" : { "query" : "burger bar", "fields" : ["name","desc","reviews.review"] } } }'

Slide 122

Slide 122 text

curl -XGET '.../eatly/eateries/_search' -d '{ "query" : { "multi_match" : { "query" : "burger bar", "fields" : ["name^2","desc","reviews.review"] } } }'

Slide 123

Slide 123 text

filters

Slide 124

Slide 124 text

Given I am a hungry person When I start searching for somewhere to eat Then I should be able to filter my results

Slide 125

Slide 125 text

curl -XPUT 'http://localhost:9200/eatly/eateries/1' -d '{ "name" : "Jeff''s Burger Joint", "desc" : "Blah Blah dirty burgers...", "tags" : ["greasy", "retro"] }'

Slide 126

Slide 126 text

curl -XGET '.../eatly/eateries/_search' -d '{ "query" : { "multi_match" : { "query" : "burger bar", "fields" : ["name^2", "desc", "reviews.review"] } }, "filter" : { "not" : { "terms" : { "tags" : ["greasy", "meaty"]} } } }'

Slide 127

Slide 127 text

query or filter?

Slide 128

Slide 128 text

Given I am a hungry person When I search for somewhere to eat near me Then I should see results close to my location

Slide 129

Slide 129 text

curl -XPUT '.../eatly/eateries/_mapping' -d '{ "eateries" : { "properties" : { "name" : {"type" : "string", "boost" : "1.5"}, "desc" : {"type" : "string"} "reviews" : { "properties" : { "review" : {"type" : "string"}, "reviewer" : {"type" : "string"} } }, "location" : { "type" : "geo_point" } } } }'

Slide 130

Slide 130 text

curl -XPOST 'http://localhost:9200/eatly/eateries/' -d '{ "name" : "Jeff''s Burgers", "desc" : "Blah Blah dirty burgers...", "reviews" : [ { "review" : "Yadda, yadda, yadda", "reviewer" : "John Smith" }, { "review" : "na na na", "reviewer" : "Billy Badger" } ], "location" : { "lat" : 41.12, "lon" : -71.34 } }'

Slide 131

Slide 131 text

curl -XGET '.../eatly/eateries/_search' -d '{ "sort" : [ { "_geo_distance" : { "eatery.location" : [-40, 70], "order" : "asc", "unit" : "miles" } } ], "query" : { "multi_match" : { "query" : "burger bar", "fields" : ["name^2","desc","reviews.review"] } } }'

Slide 132

Slide 132 text

sense (chrome extension)

Slide 133

Slide 133 text

head (elasticsearch plugin)

Slide 134

Slide 134 text

elastica

Slide 135

Slide 135 text

sherlock elasticsearch elasticsearch-php (official)

Slide 136

Slide 136 text

{ "require": { "ruflin/elastica": "v0.90.2.0" } }

Slide 137

Slide 137 text

$client = new \Elastica\Client();

Slide 138

Slide 138 text

$index = $client->getIndex("eatly"); $type = $index->getType("eateries");

Slide 139

Slide 139 text

$type->addDocument( new \Elastica\Document( null, array( "name" => "Jeff's Burger Joint", "desc" => "Blah Blah..." ) ) );

Slide 140

Slide 140 text

$type->addDocument( new \Elastica\Document( $id, array( "id" => $id, "name" => "Jeff's Burger Joint", "desc" => "Blah Blah..." ) ) );

Slide 141

Slide 141 text

$type->addDocuments($documents); $type->getDocument($id); $type->deleteId($id); $type->deleteDocument($document); $type->delete();

Slide 142

Slide 142 text

$type->search("burger"); $type->search("burger", 10); $type->count("burger");

Slide 143

Slide 143 text

$multiMatchQuery = new \Elastica\Query\MultiMatch(); $multiMatchQuery ->setQuery("burgers bar") ->setFields( array("name^2", "desc", "reviews.review") ) ; $query = new \Elastica\Query($multiMatchQuery); $query->setFilter( new \Elastica\Filter\BoolNot( new \Elastica\Filter\Terms( "tags", array("greasy", "meaty") ) ) ); $results = $type->search($query);

Slide 144

Slide 144 text

$mapping = new \Elastica\Type\Mapping(); $mapping->setType($type); $mapping->setProperties( array( "name" => array( "type" => "string" ), "desc" => array( "type" => "string", "boost" => 1.5 ) ) ); $mapping->send();

Slide 145

Slide 145 text

Given I am a hungry person When I search for somewhere to eat Then I should see relevant results And the results should be ordered by relevance ✔

Slide 146

Slide 146 text

Given I am a hungry person When I search for somewhere to eat Then I should see results for relevant reviews ✔

Slide 147

Slide 147 text

Given I am a hungry person When I search for somewhere to eat Then I should see results for similar words ✔

Slide 148

Slide 148 text

Given I am a hungry person When I search for somewhere to eat near me Then I should see results close to my location ✔

Slide 149

Slide 149 text

Given I am a hungry person When I start searching for somewhere to eat Then I should be able to filter my results ?

Slide 150

Slide 150 text

$facet = new \Elastica\Facet\Terms("tags"); $facet->setField("tags"); $facet->setSize(10); $facet->setOrder("reverse_count"); $query->addFacet($facet);

Slide 151

Slide 151 text

$facets = $resultSet->getFacets(); foreach ($facets["tags"]["terms"] as $facet) { printf( "%s: %s\n", $facet["term"], $facet["count"] ); } thai: 4 italian: 8 greek:4

Slide 152

Slide 152 text

Given I am a hungry person When I start view details of somewhere to eat Then I should see suggestions of similar eateries

Slide 153

Slide 153 text

$type->moreLikeThis($document);

Slide 154

Slide 154 text

$type->moreLikeThis( new \Elastica\Document($id) );

Slide 155

Slide 155 text

Given I am a restaurateur When I upload my PDF menu Then hungry people should be able to search it

Slide 156

Slide 156 text

bin/plugin -install \ elasticsearch/elasticsearch-mapper-attachments/1.9.0

Slide 157

Slide 157 text

$propertyMapping = array( //... "menu" => array("type" => "attachment"), )

Slide 158

Slide 158 text

$document->addFile('menu', $filepath);

Slide 159

Slide 159 text

Given I am a hungry person When I typo entering my search terms Then I should see “did you mean?” suggestions

Slide 160

Slide 160 text

$query = new \Elastica\Query( array( "query" => array(...), "suggest" => array( "check1" => array( "text" => "berger", "term" => array( "field" => "name" ) ) ) ) );

Slide 161

Slide 161 text

$data = $results->getResponse()->getData(); print_r($data["suggest"]);

Slide 162

Slide 162 text

Array ( [check1] => Array ( [0] => Array ( [text] => berger [offset] => 0 [length] => 6 [options] => Array ( [0] => Array ( [text] => burger [score] => 0.8333333 [freq] => 1 ) ) ) ) )

Slide 163

Slide 163 text

Given I am a hungry person When I start entering my search terms Then I should see suggestions straight away

Slide 164

Slide 164 text

$mapping->setProperties( array( "name" => array( "type" => "string" ), "desc" => array( "type" => "string", "boost" => 1.5 ), "name_suggest" => array( "type" => "completion" ) ) );

Slide 165

Slide 165 text

new \Elastica\Document( $id, array( "id" => $id, "name" => "Jeff's Burger Joint", "name_suggest" => "Jeff's Burger Joint", "desc" => "Blah Blah..." ) );

Slide 166

Slide 166 text

$query = array( "eateries" => array( "text" => 'j', "completion" => array( "field" => "name_suggest", ) ) );

Slide 167

Slide 167 text

$response = $index->request( "_suggest", \Elastica\Request::GET, $query ); $data = $response->getData(); print_r($data["eateries"]);

Slide 168

Slide 168 text

Array ( [0] => Array ( [text] => j [offset] => 0 [length] => 1 [options] => Array ( [0] => Array ( [text] => Jeffs Burger Joint [score] => 1 ) ) ) )

Slide 169

Slide 169 text

scripts

Slide 170

Slide 170 text

percolator scripts

Slide 171

Slide 171 text

percolator river scripts

Slide 172

Slide 172 text

data analysis percolator river scripts

Slide 173

Slide 173 text

data analysis percolator logstash river scripts

Slide 174

Slide 174 text

thank you! questions? @mr_r_miller