Share
Embed
Transcript
71 slides
-
Searching • Most of us start browsing the Internet with
a search engine – that's how natural searching is -
Searching • Most of us start browsing the Internet with
a search engine – that's how natural searching is • Unless your site has very little information, or everything is reachable with a few clicks, searching becomes a vital feature -
Searching • Poor man's solution: • You have to expose
the data to Google -
Searching • Poor man's solution: • You have to expose
the data to Google • Guess what? Ads -
Searching in examples • We'll demonstrate and compare SQL database
searches and searching with Solr -
Searching in examples • We'll demonstrate and compare SQL database
searches and searching with Solr • We'll use the same example database in all cases -
Example database • Extracted from a real-life web application •
Contains blog posts and culinary recipes scraped from cooking blogs -
Example database • Extracted from a real-life web application •
Contains blog posts and culinary recipes scraped from cooking blogs • 22 tables, 69300 records in posts table, 550 MB of textual data -
Example database posts id blog_id title body ... posts id
blog_id title body ... blogs id name url ... blogs id name url ... -
Example database posts id blog_id title body ... posts id
blog_id title body ... recipes id post_id title ... recipes id post_id title ... blogs id name url ... blogs id name url ... -
Example database posts id blog_id title body ... posts id
blog_id title body ... recipes id post_id title ... recipes id post_id title ... ingredients id name ... ingredients id name ... recipes_ingredients recipe_id ingredient_id amount ... recipes_ingredients recipe_id ingredient_id amount ... blogs id name url ... blogs id name url ... -
Naïve approach with SQL LIKE • SELECT posts.id FROM recipes
INNER JOIN posts ON posts.id = recipes.post_id INNER JOIN blogs ON blogs.id = posts.blog_id WHERE posts.title LIKE '%cake%' OR posts.body LIKE '%cake%' OR recipes.title LIKE '%cake%'; -
Naïve approach with SQL LIKE • SELECT posts.id FROM recipes
INNER JOIN posts ON posts.id = recipes.post_id INNER JOIN blogs ON blogs.id = posts.blog_id WHERE posts.title LIKE '%cake%' OR posts.body LIKE '%cake%' OR recipes.title LIKE '%cake%'; 949 rows in set (10.07 sec) -
Naïve approach with SQL LIKE • Slow • Very bad
effect on database performance -
Naïve approach with SQL LIKE • Slow • Very bad
effect on database performance • Problems with whitespace/punctuation (e.g.: LIKE '% cake %' against “cake!”) -
Naïve approach with SQL LIKE • Slow • Very bad
effect on database performance • Problems with whitespace/punctuation (e.g.: LIKE '% cake %' against “cake!”) • Really, a bad idea -
Searching features built into RDBMS • MySQL – full text
search indexes -
Searching features built into RDBMS • MySQL – full text
search indexes CREATE TABLE posts ( id INT NOT NULL AUTO INCREMENT, title VARCHAR(200), body TEXT, ... FULLTEXT (title, body), ... -
Searching features built into RDBMS SELECT posts.id FROM recipes INNER
JOIN posts ON posts.id = recipes.post_id INNER JOIN blogs ON blogs.id = posts.blog_id WHERE MATCH (posts.title, posts.body) AGAINST ('cake') OR MATCH (recipes.title) AGAINST ('cake'); -
Searching features built into RDBMS SELECT posts.id FROM recipes INNER
JOIN posts ON posts.id = recipes.post_id INNER JOIN blogs ON blogs.id = posts.blog_id WHERE MATCH (posts.title, posts.body) AGAINST ('cake') OR MATCH (recipes.title) AGAINST ('cake'); 1042 rows in set (0.22 sec) -
Searching features built into RDBMS • Much faster • But
still not blazingly fast -
Searching features built into RDBMS • Much faster • But
still not blazingly fast • Used to only be available for MyISAM tables -
Searching features built into RDBMS • Much faster • But
still not blazingly fast • Used to only be available for MyISAM tables (not anymore with MySQL 5.6) -
Searching features built into RDBMS • Similar features in other
RDBMS: – PostgreSQL: Full Text Searching – MSSQL: Full-Text Search – Oracle: Oracle Text – SQLite: FTS5 extension -
Enter Solr • Based on Lucene, a high-performance, full- featured
text search engine library -
Enter Solr • Based on Lucene, a high-performance, full- featured
text search engine library • Notable features: -
Enter Solr • Based on Lucene, a high-performance, full- featured
text search engine library • Notable features: – ranked searching -
Enter Solr • Based on Lucene, a high-performance, full- featured
text search engine library • Notable features: – ranked searching – flexible faceting, highlighting, joins and result grouping -
Enter Solr • Based on Lucene, a high-performance, full- featured
text search engine library • Notable features: – ranked searching – flexible faceting, highlighting, joins and result grouping – allows simultaneous update and searching -
Enter Solr • Based on Lucene, a high-performance, full- featured
text search engine library • Notable features: – ranked searching – flexible faceting, highlighting, joins and result grouping – allows simultaneous update and searching – support for filters -
How Solr works • Importing data from a source (e.g.
SQL database) -
How Solr works • Importing data from a source (e.g.
SQL database) – full import -
How Solr works • Importing data from a source (e.g.
SQL database) – full import – delta import -
How Solr works • Importing data from a source (e.g.
SQL database) – full import – delta import • Indexing imported data (with index filters) -
How Solr works • Importing data from a source (e.g.
SQL database) – full import – delta import • Indexing imported data (with index filters) • Searching (with query filters) -
Solr configuration <document name="blogs_solr"> <entity name="item" query=" SELECT r.id AS
id, r.post_id AS post_id, r.title AS recipe_title, ... FROM recipes AS r INNER JOIN posts AS p ON p.id = r.post_id INNER JOIN blogs AS b ON b.id = p.blog_id ORDER BY posted_on DESC " -
Solr configuration deltaQuery=" SELECT r.id AS id, r.post_id AS post_id,
r.title AS recipe_title, ... FROM recipes AS r INNER JOIN posts AS p ON p.id = r.post_id INNER JOIN blogs AS b ON b.id = p.blog_id WHERE p.added_on > '${dataimporter.last_index_time}' ORDER BY posted_on DESC " -
Solr configuration <entity name=”item”> <field column="id" name="id" /> <field column="post_id"
name="post_id" /> <field column="recipe_title" name="recipe_title" /> ... <entity name="ingredients" query="SELECT i.name AS ingredient_name FROM ingredients AS i ... WHERE ri.recipe_id = '${item.id}'"> <field column="ingredient_name" name="ingredients" /> -
Solr in action • Delta import: curl http://host:port/solr/database/dataimport \ --data
'command=delta-import' -
Solr in action • Delta import: • Search: curl http://host:port/solr/database/dataimport
\ --data 'command=delta-import' curl http://host:port/solr/database/select? q=post_body%3Acake -
Solr in action { "responseHeader": { ... "QTime": 2, "params":
{ ... "q": "post_body:cake", ... } }, "response": { "numFound": 1009, ... -
Solr in action { "responseHeader": { ... "QTime": 2, "params":
{ ... "q": "post_body:cake", ... } }, "response": { "numFound": 1009, ... Blazingly fast -
Using Solr in application code • As simple as making
an HTTP request: $phrase = 'cake'; $url = 'http://host/solr/database/select?q=' . urlencode('post_body:' . $phrase); $data = json_decode(file_get_contents($url), TRUE); -
Using Solr in application code • There are client libraries/language
bindings available for all popular languages -
Filters • Transform and normalize the search data • Examples:
– LowerCaseFilterFactory – lower case tokens -
Filters • Transform and normalize the search data • Examples:
– LowerCaseFilterFactory – lower case tokens – ASCIIFoldingFilterFactory – converts >127 unicode characters into their ASCII equivalents -
Filters • Transform and normalize the search data • Examples:
– LowerCaseFilterFactory – lower case tokens – ASCIIFoldingFilterFactory – converts >127 unicode characters into their ASCII equivalents – External: MorfologikFilterFactory – filter to use with Morfologik stemmer