mariusz gil - sphinx
DESCRIPTION
SPHINXrozproszony i skalowalny full-text searchWebClusters 2011, Mariusz Gil piątek, 10 czerwca 2011piątek, 10 czerwca 2011SELECT * FROM table WHERE content LIKE "%pattern%"piątek, 10 czerwca 2011SELECT * FROM table WHERE MATCH (fields) AGAINST (keywords)Silnik Full-Text Search wbudowany w MySQL funkcjonuje obecnie tylko z engine MyISAMpiątek, 10 czerwca 2011SELECT id, headline(doc, q), rank(vector, q) FROM docs, to_tsquery($1) AS q WHERE vector @TRANSCRIPT
![Page 1: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/1.jpg)
SPHINX
WebClusters 2011, Mariusz Gil <[email protected]>
rozproszony i skalowalny full-text search
piątek, 10 czerwca 2011
![Page 2: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/2.jpg)
piątek, 10 czerwca 2011
![Page 3: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/3.jpg)
SELECT * FROM tableWHERE content
LIKE "%pattern%"
piątek, 10 czerwca 2011
![Page 4: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/4.jpg)
SELECT * FROM tableWHERE MATCH (fields)AGAINST (keywords)
Silnik Full-Text Search wbudowany w MySQL funkcjonuje obecnie tylko z engine MyISAM
piątek, 10 czerwca 2011
![Page 5: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/5.jpg)
SELECT id, headline(doc, q), rank(vector, q) FROM docs, to_tsquery($1) AS q
WHERE vector @@ q ORDER BY rank(vector, q) DESC
Powyższy przykład dotyczy silnika tsearch2 współpracującego z bazą danych PostgreSQL
piątek, 10 czerwca 2011
![Page 6: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/6.jpg)
ALTERNATYWY?
piątek, 10 czerwca 2011
![Page 7: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/7.jpg)
piątek, 10 czerwca 2011
![Page 8: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/8.jpg)
SPHINX
piątek, 10 czerwca 2011
![Page 9: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/9.jpg)
• wysoka wydajność indeksowania i wyszukiwania danych
• zaawansowane narzędzia do indeksacji i odpytywania indeksów
• integracja ze źródłami danych SQL i XML
• proste skalowanie
piątek, 10 czerwca 2011
![Page 10: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/10.jpg)
indexer searchd search
sphinxapi spelldump indextool
piątek, 10 czerwca 2011
![Page 11: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/11.jpg)
ARCHITEKTURA
piątek, 10 czerwca 2011
![Page 12: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/12.jpg)
serwer Sphinx
LINUX
APACHE
MySQL
PHP
{
Przykładowe miejsce osadzenia Sphinxa w aplikacji opartej o stos LAMP
indexer searchd
piątek, 10 czerwca 2011
![Page 13: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/13.jpg)
serwer Sphinxserwer MySQL
id name value
1 lorem 234.34
2 ipsum 6784.21
3 dolor -235.12
Tabela sample_data
.spa .spd
.sph .spi
.spm .spp
Indeks sample_data
piątek, 10 czerwca 2011
![Page 14: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/14.jpg)
source sample_data {type = mysqlsql_host = hostsql_user = database_usersql_pass = database_passwordsql_db = database_namesql_port = 3306
sql_query_pre = SET NAMES UTF8;sql_query = SELECT id, name, value FROM sample_data;sql_query_info = SELECT name, value FROM sample_data WHERE id=$id;
sql_attr_float = value;}
index sample_data {source = sample_datapath = /usr/local/sphinx/var/data/sample_datamorphology = none
min_word_len = 3 min_prefix_len = 3min_infix_len = 0enable_star = 0
}
piątek, 10 czerwca 2011
![Page 15: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/15.jpg)
10:15
1GB
11:15
1GB
12:15
1GB
23:15
1GB
22:15
1GB...
Rozwiązanie 1: reindeksacja całości co X jednostek czasu
piątek, 10 czerwca 2011
![Page 16: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/16.jpg)
04:15
1GB
05:1510 MB
...
Rozwiązanie 2: przebudowa indeksu głównego np. 1 na dobę, a co X jednostek czasu tylko indeksu delta
05:1511 MB
22:1560 MB
23:1564 MB
piątek, 10 czerwca 2011
![Page 17: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/17.jpg)
FLOW
piątek, 10 czerwca 2011
![Page 18: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/18.jpg)
serwer aplikacji
serwer Sphinx serwer data storage
klient
(1) query
(2) query(3) klucze dokumentów
(4) klucze dokumentów
(5) dane
(6) dane
piątek, 10 czerwca 2011
![Page 19: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/19.jpg)
INSTALACJA
piątek, 10 czerwca 2011
![Page 20: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/20.jpg)
LINUX:$ wget http://sphinxsearch.com/files/sphinx-0.9.9.tar.gz$ tar xzvf sphinx-0.9.9.tar.gz$ cd sphinx$ ./configure$ make$ make install
lub
MAC OSX:$ sudo port install sphinx
Podczas instalacji należy wskazać ścieżki do katalogów include/library wybranej bazy danych
piątek, 10 czerwca 2011
![Page 21: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/21.jpg)
MacBook-Pro: mariusz$ indexer -c ./sphinx_1.conf --allSphinx 0.9.9-release (r2117)Copyright (c) 2001-2009, Andrew Aksyonoff
using config file './sphinx_1.conf'...indexing index 'cities_shard_1'...collected 6 docs, 0.0 MBsorted 0.0 Mhits, 100.0% donetotal 6 docs, 30147 bytestotal 0.105 sec, 285261 bytes/sec, 56.77 docs/secdistributed index 'cities' can not be directly indexed; skipping.total 2 reads, 0.000 sec, 11.5 kb/call avg, 0.0 msec/call avgtotal 7 writes, 0.000 sec, 8.7 kb/call avg, 0.0 msec/call avg
MacBook-Pro: mariusz$ searchd -c ./sphinx_1.confSphinx 0.9.9-release (r2117)Copyright (c) 2001-2009, Andrew Aksyonoff
using config file './sphinx_1.conf'...listening on 127.0.0.1:9313listening on all interfaces, port=3313
Fragmenty przykładowej konfiguracji z demo
piątek, 10 czerwca 2011
![Page 22: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/22.jpg)
PRZESZUKIWANIE
piątek, 10 czerwca 2011
![Page 23: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/23.jpg)
• LUB / ORAZ: hello | word, hello & world
• NEGACJA: hello -world
• OPERATOR POLA: @content hello
• FRAZY: „hello world”
• BLISKOŚĆ: „hello world”~10
• ... i jeszcze trochę innych
piątek, 10 czerwca 2011
![Page 24: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/24.jpg)
• wyszukiwania geo-distance
• MVA - multi value attributes
• UNIX Timestamps
• wartości zmiennoprzecinkowe
• wartości całkowitoliczbowe
• dowolne łącznie powyższych z FTS
piątek, 10 czerwca 2011
![Page 25: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/25.jpg)
titleweight: 100
contentweight: 80
tagsweight: 10
indeks bez wsparcia wyszukiwania infiksowego
titleweight: 200
contentweight: 10
tagsweight: 10
indeks obsługujący wyszukiwanie infiksowe na polu title
Indeks B
Indeks A
piątek, 10 czerwca 2011
![Page 26: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/26.jpg)
require ( "sphinxapi.php" );
$cl = new SphinxClient ();
$cl->SetMatchMode ( SPH_MATCH_EXTENDED2 );$cl->SetSortMode ( SPH_SORT_RELEVANCE );$cl->AddQuery ( "the", "index" );$cl->SetSortMode ( SPH_SORT_EXTENDED, "published DESC" );$cl->AddQuery ( "@* the query", "index" );$cl->SetSortMode ( SPH_SORT_EXTENDED, "@relevance DESC, published DESC" );$cl->AddQuery ( "@(title,content) lorem ipsum", "index" );
$res = $cl->RunQueries();
SphinxAPI
SphinxAPI dostępne jest na najpopularniejsze platformy, m.in. PHP, Ruby, Java
piątek, 10 czerwca 2011
![Page 27: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/27.jpg)
CREATE TABLE t1( id INTEGER UNSIGNED NOT NULL, weight INTEGER NOT NULL, query VARCHAR(3072) NOT NULL, group_id INTEGER, INDEX(query)) ENGINE=SPHINX CONNECTION="sphinx://localhost:9312/test";
SELECT * FROM t1 WHERE query='test it;mode=any';
SphinxSE
W przypadku SphinxSE, MySQL nie obsługuje indeksowania i wyszukiwania, jest jedynie proxy do Sphinxa
piątek, 10 czerwca 2011
![Page 28: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/28.jpg)
$ mysql -P 9306Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 1Server version: 0.9.9-dev (r1734)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> SELECT * FROM test1 WHERE MATCH('test') -> ORDER BY group_id ASC OPTION ranker=bm25;+------+--------+----------+------------+| id | weight | group_id | date_added |+------+--------+----------+------------+| 4 | 1442 | 2 | 1231721236 || 2 | 2421 | 123 | 1231721236 || 1 | 2421 | 456 | 1231721236 |+------+--------+----------+------------+3 rows in set (0.00 sec)
SphinxQL
SphinxQL to docelowy główny protokół Sphinxa, nowości będą implementowane w nim w pierwszej kolejności
piątek, 10 czerwca 2011
![Page 29: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/29.jpg)
SKALOWANIE
piątek, 10 czerwca 2011
![Page 30: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/30.jpg)
PARTYCJONOWANIEHORYZONTALNE
piątek, 10 czerwca 2011
![Page 31: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/31.jpg)
serwer Sphinx
index chunk 2
serwer Sphinx
index chunk 3
serwer Sphinx
index chunk N
...
serwer Sphinxz rozproszonym indeksem
index chunk 1
(1) query
(2) zapytania równoległe(3) klucze dokumentów
(4) zapytanie do lokalnego indeksu(5) łączenie wyników i usuwanie duplikatów
(6) odpowiedź
piątek, 10 czerwca 2011
![Page 32: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/32.jpg)
index cities_shard_1{ source = cities_shard_1 path = /path/to/data/sphinx/cities_shard_1 docinfo = extern charset_type = utf-8 charset_table = 0..9, A..Z->a..z, a..z, \ U+0143->U+0144, U+0104->U+0105, U+0106->U+0107, \ U+0118->U+0119, U+0141->U+0142, U+00D3->U+00F3, \ U+015A->U+015B, U+0179->U+017A, U+017B->U+017C, \ U+0105, U+0107, U+0119, U+0142, U+00F3, U+015B, \ U+017A, U+017C, U+0144
min_word_len = 3 enable_star = 0}
index cities{ type = distributed
local = cities_shard_1 agent = localhost:9314:cities_shard_2}
Fragmenty przykładowej konfiguracji z demo
piątek, 10 czerwca 2011
![Page 33: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/33.jpg)
BEZPIECZEŃSTWO
piątek, 10 czerwca 2011
![Page 34: Mariusz Gil - Sphinx](https://reader034.vdocuments.net/reader034/viewer/2022051210/550012574a7959b8508b5ac3/html5/thumbnails/34.jpg)
DEMO
piątek, 10 czerwca 2011