<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>sorgalla.com &#187; Sphinx</title>
	<atom:link href="http://sorgalla.com/tag/sphinx/feed/" rel="self" type="application/rss+xml" />
	<link>http://sorgalla.com</link>
	<description>PHP, Zend Framework, Datenbanken und was sonst noch so anfällt.</description>
	<lastBuildDate>Tue, 05 Jul 2011 07:20:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Sphinx und SphinxSE ins Zend Framework integrieren</title>
		<link>http://sorgalla.com/2010/01/18/sphinx-und-sphinxse-ins-zend-framework-integrieren/</link>
		<comments>http://sorgalla.com/2010/01/18/sphinx-und-sphinxse-ins-zend-framework-integrieren/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 10:58:05 +0000</pubDate>
		<dc:creator>jan</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Sphinx]]></category>
		<category><![CDATA[SphinxSE]]></category>

		<guid isPermaLink="false">http://sorgalla.com/?p=424</guid>
		<description><![CDATA[Nachdem Sphinx und SphinxSE installiert ist, geht es diesmal um die Integration in ein Zend Framework-Projekt. Dazu gehört die Erstellung einer Konfigurationsdatei und ein Kommandozeilen-Skript zum Starten und Stoppen des Sphinx-Suchdämons und zum Generieren des Suchindexes. Den Code zum Artikel gibts auf github. Das Zend Framework ist nicht enthalten und muss noch nachinstalliert werden. Entweder [...]]]></description>
			<content:encoded><![CDATA[<p><a href="/2009/12/15/sphinx-und-sphinxse-unter-ubuntu-installieren/">Nachdem Sphinx und SphinxSE installiert ist</a>, geht es diesmal um die Integration in ein Zend Framework-Projekt. Dazu gehört die Erstellung einer Konfigurationsdatei und ein Kommandozeilen-Skript zum Starten und Stoppen des Sphinx-Suchdämons und zum Generieren des Suchindexes.</p>
<p>Den Code zum Artikel gibts auf <a href="http://github.com/jsor/sphinx-and-zend-framework">github</a>.</p>
<p><span id="more-424"></span></p>
<p>Das Zend Framework ist nicht enthalten und muss noch nachinstalliert werden. Entweder man <a href="http://framework.zend.com/download/latest">lädt</a> sich die aktuelle Version herunter und kopiert oder linkt den Zend-Ordner ins <code>library</code>-Verzeichnis oder man installiert es per PEAR. Den (inoffiziellen) PEAR Channel gibts unter <a href="http://pear.zfcampus.org/">http://pear.zfcampus.org</a>.</p>
<h3>Die Konfigurationsdatei</h3>
<p>Die Konfigurationsdatei von Sphinx ist die Schaltzentrale und enthält alle Optionen für den <a href="http://www.sphinxsearch.com/docs/current.html#confgroup-searchd">Dämon</a>, den <a href="http://www.sphinxsearch.com/docs/current.html#confgroup-indexer">Indexer</a>, die <a href="http://www.sphinxsearch.com/docs/current.html#confgroup-index">Indizes</a> und deren <a href="http://www.sphinxsearch.com/docs/current.html#confgroup-source">Datenquellen</a>. Für unser Beispielprojekt nutzen wir eine einfache Variante mit Grundeinstellungen. Der Clou ist, dass die Konfigurationsdateien von jeder beliebigen Skriptsprachen verarbeitet werden kann. Einfach am Anfang der Datei den entsprechenden <a href="http://de.wikipedia.org/wiki/Shebang">Shebang</a> setzen.</p>
<p>Die Datei liegt in <code>application/configs/sphinx.conf</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">#!/usr/bin/env php
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Define path to application directory</span>
<span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'APPLICATION_PATH'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">||</span> <span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'APPLICATION_PATH'</span><span style="color: #339933;">,</span> <span style="color: #990000;">realpath</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/..'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Define application environment</span>
<span style="color: #666666; font-style: italic;">//</span>
<span style="color: #666666; font-style: italic;">// Use `export APPLICATION_ENV=development` or edit /etc/environment</span>
<span style="color: #666666; font-style: italic;">// to change the application environment</span>
<span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'APPLICATION_ENV'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">||</span> <span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'APPLICATION_ENV'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">getenv</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'APPLICATION_ENV'</span><span style="color: #009900;">&#41;</span> ? <span style="color: #990000;">getenv</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'APPLICATION_ENV'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'production'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Ensure library/ is on include_path</span>
<span style="color: #990000;">set_include_path</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span>PATH_SEPARATOR<span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #990000;">realpath</span><span style="color: #009900;">&#40;</span>APPLICATION_PATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/../../library'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">get_include_path</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/** Zend_Application */</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'Zend/Application.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create application, bootstrap, and run</span>
<span style="color: #000088;">$application</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Application<span style="color: #009900;">&#40;</span>
    APPLICATION_ENV<span style="color: #339933;">,</span>
    APPLICATION_PATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/configs/application.ini'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$application</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOptions</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
source posts
{
    type                    = mysql
&nbsp;
    sql_host                = <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'resources'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'db'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'params'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'host'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    sql_user                = <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'resources'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'db'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'params'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    sql_pass                = <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'resources'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'db'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'params'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    sql_db                  = <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'resources'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'db'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'params'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dbname'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    # sql_port               = 3306    # optional, default is 3306
&nbsp;
    sql_query_pre           = SET NAMES utf8
&nbsp;
    sql_query               = \
        SELECT posts.id, posts.category_id, posts.title, posts.content, UNIX_TIMESTAMP(posts.created) AS created, \
               categories.name AS category_name, \
               GROUP_CONCAT(tags.tag SEPARATOR ', ') AS tags \
          FROM posts \
          JOIN categories ON (categories.id = posts.category_id) \
     LEFT JOIN posts_tags ON (posts_tags.post_id = posts.id) \
     LEFT JOIN tags ON (tags.id = posts_tags.tag_id) \
      GROUP BY posts.id
&nbsp;
    sql_attr_uint           = category_id
    sql_attr_timestamp      = created
&nbsp;
    sql_query_info          = \
        SELECT posts.id, posts.category_id, posts.title, posts.content, posts.created, \
               categories.name AS category_name, \
               GROUP_CONCAT(tags.tag SEPARATOR ', ') AS tags \
          FROM posts \
          JOIN categories ON (categories.id = posts.category_id) \
     LEFT JOIN posts_tags ON (posts_tags.post_id = posts.id) \
     LEFT JOIN tags ON (tags.id = posts_tags.tag_id) \
         WHERE posts.id=$id
}
&nbsp;
index posts
{
    source                  = posts
    path                    = <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sphinx'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'path'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>/posts
    docinfo                 = extern
    html_strip              = 1
    charset_type            = utf-8
    # Character table for german (from http://www.sphinxsearch.com/forum/view.html?id=19)
    charset_table           = 0..9, A..Z-&gt;a..z, _, a..z, U+C4-&gt;U+E4, U+D6-&gt;U+F6, U+DC-&gt;U+FC, U+DF, U+E4, U+F6, U+FC
}
&nbsp;
indexer
{
    mem_limit               = 32M
}
&nbsp;
searchd
{
    listen                  = <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sphinx'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'searchd'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'listen'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    log                     = <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sphinx'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'searchd'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'log'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    query_log               = <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sphinx'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'searchd'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'query_log'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    read_timeout            = 5
    max_children            = 30
    pid_file                = <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sphinx'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'searchd'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pid_file'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    max_matches             = 1000
    seamless_rotate         = 1
    preopen_indexes         = 0
    unlink_old              = 1
}</pre></div></div>

<p>Was die einzelnen Optionen bedeuten, kann man in der <a href="http://www.sphinxsearch.com/docs/current.html#conf-reference">Dokumentation</a> gut nachlesen. Ich will hier nur auf paar Besonderheiten eingehen:</p>
<ul>
<li>Der PHP-Abschnitt am Anfang der Datei nutzt <code>Zend_Application</code> um die <code>application.ini</code> zu laden.</li>
<li>Im Abschnitt <code>source posts</code> nutzen wir die ausgelesenen Optionen um die Zugangsdaten für die Datenbank zu setzen. Diese können dann global in der <code>application.ini</code> verwaltet werden.</li>
<li>Im Abschnitt <code>index posts</code> und <code>searchd </code>verwenden wir ebenfalls Optionen aus der <code>application.ini</code>. Das sind vor allem Pfadangaben für den Suchindex und Logdateien und an welcher IP und/oder Port der Suchdämon lauschen soll. Diese Angaben können wir somit pro <code>APPLICATION_ENV</code> anpassen.</li>
</ul>
<h3>Das Kommandozeilen-Skript für Sphinx</h3>
<p>Um den Suchindex zu erstellen und den Suchdämon zu starten und zu stoppen, enthält das Projekt das Kommandozeilen-Skript in <code>scripts/sphinx</code>. Der Aufruf erfolgt so:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>scripts<span style="color: #000000; font-weight: bold;">/</span>sphinx COMMAND ENVIRONMENT</pre></div></div>

<h4>COMMAND</h4>
<table class="summary">
<caption>Folgende Kommandos sind möglich:</caption>
<tbody>
<tr>
<th>start</th>
<td>Startet den Suchdämon.</td>
</tr>
<tr>
<th>stop</th>
<td>Stoppt den Suchdämon.</td>
</tr>
<tr>
<th>restart</th>
<td>Startet den Suchdämon neu.</td>
</tr>
<tr>
<th>index-all</th>
<td>Erstellt alle in <code>application/configs/sphinx.conf</code> definierten Suchindizes.</td>
</tr>
</tbody>
</table>
<h4>ENVIRONMENT</h4>
<p>Gibt <code>APPLICATION_ENV</code> an, entspricht also <code>define('APPLICATION_ENV', 'production');</code> in z. B. der <code>index.php</code>. Somit wird der entsprechende Abschnitt in <code>application/configs/application.ini</code> genutzt. Wenn nicht angegeben, wird standardmäßig <code>production</code> verwendet.</p>
<h4>Beispiele</h4>
<p>Startet den Suchdämon mit <code>APPLICATION_ENV=production</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>scripts<span style="color: #000000; font-weight: bold;">/</span>sphinx start production</pre></div></div>

<p>Erstellt alle Suchindizes mit <code>APPLICATION_ENV=development</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>scripts<span style="color: #000000; font-weight: bold;">/</span>sphinx index-all development</pre></div></div>

<p>Somit haben wir nun unser Zend Framework-Projekt präpariert um mit Sphinx zu arbeiten. Auf den Suchindex lässt sich nun ganz einfach, z. B. per <a href="http://framework.zend.com/manual/en/zend.db.select.html">Zend_Db_Select</a>, zugreifen:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Mein Suchbegriff'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$select</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$select</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'posts'</span><span style="color: #009900;">&#41;</span>
       <span style="color: #339933;">-&gt;</span><span style="color: #004000;">joinInner</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sphinx'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'sphinx.id = posts.id'</span><span style="color: #009900;">&#41;</span>
       <span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sphinx.query = ?'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">';mode=any;sort=extended:@weight desc, @id asc;limit=10;offset=0;index=posts'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$select</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Links zum Thema</h3>
<ul>
<li><a href="http://github.com/jsor/sphinx-and-zend-framework">Beispielcode auf github</a></li>
<li><a href="http://www.sphinxsearch.com/docs/current.html#conf-reference">Dokumentation zu den Konfigurationsoptionen von Sphinx</a></li>
</ul>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" class="tt" href="http://twitter.com/intent/tweet?text=Sphinx+und+SphinxSE+ins+Zend+Framework+integrieren+http%3A%2F%2Fsorgalla.com%2F%3Fp%3D424+%2Fby+%40jansor" title="Post to Twitter"><img class="nothumb" src="http://sorgalla.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://delicious.com/post?url=http://sorgalla.com/2010/01/18/sphinx-und-sphinxse-ins-zend-framework-integrieren/&amp;title=Sphinx+und+SphinxSE+ins+Zend+Framework+integrieren" title="Post to Delicious"><img class="nothumb" src="http://sorgalla.com/wp-content/plugins/tweet-this/icons/en/delicious/tt-delicious.png" alt="Post to Delicious" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://sorgalla.com/2010/01/18/sphinx-und-sphinxse-ins-zend-framework-integrieren/&amp;title=Sphinx+und+SphinxSE+ins+Zend+Framework+integrieren" title="Post to Digg"><img class="nothumb" src="http://sorgalla.com/wp-content/plugins/tweet-this/icons/en/digg/tt-digg.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://sorgalla.com/2010/01/18/sphinx-und-sphinxse-ins-zend-framework-integrieren/&amp;t=Sphinx+und+SphinxSE+ins+Zend+Framework+integrieren" title="Post to Facebook"><img class="nothumb" src="http://sorgalla.com/wp-content/plugins/tweet-this/icons/en/facebook/tt-facebook.png" alt="Post to Facebook" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://sorgalla.com/2010/01/18/sphinx-und-sphinxse-ins-zend-framework-integrieren/&amp;title=Sphinx+und+SphinxSE+ins+Zend+Framework+integrieren" title="Post to Reddit"><img class="nothumb" src="http://sorgalla.com/wp-content/plugins/tweet-this/icons/en/reddit/tt-reddit.png" alt="Post to Reddit" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://sorgalla.com/2010/01/18/sphinx-und-sphinxse-ins-zend-framework-integrieren/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<series:name><![CDATA[Schöner suchen mit Sphinx]]></series:name>
	</item>
		<item>
		<title>Sphinx und SphinxSE unter Ubuntu installieren</title>
		<link>http://sorgalla.com/2009/12/15/sphinx-und-sphinxse-unter-ubuntu-installieren/</link>
		<comments>http://sorgalla.com/2009/12/15/sphinx-und-sphinxse-unter-ubuntu-installieren/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 18:18:27 +0000</pubDate>
		<dc:creator>jan</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Sphinx]]></category>
		<category><![CDATA[SphinxSE]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://sorgalla.com/?p=168</guid>
		<description><![CDATA[Wer Projekte mit MySQL als Datenbank-Backend realisiert, wird die Erfahrung schon gemacht haben, dass er bei der Implementierung einer Suchfunktion an Grenzen stößt. Der Fulltext-Index ist nicht der Wahrheit letzter Schluss und andere Lösungen oft nur unbefriedigende Workarounds. Hier kann Sphinx Abhilfe schaffen. Besonders die nahtlose Integration in MySQL sticht hier als Pluspunkt heraus. So [...]]]></description>
			<content:encoded><![CDATA[<p>Wer Projekte mit <a href="http://mysql.com">MySQL</a> als Datenbank-Backend realisiert, wird die Erfahrung schon gemacht haben, dass er bei der Implementierung einer Suchfunktion an Grenzen stößt. Der <a href="http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html">Fulltext-Index</a> ist nicht der Wahrheit letzter Schluss und andere Lösungen oft nur unbefriedigende Workarounds.</p>
<p>Hier kann <a href="http://www.sphinxsearch.com">Sphinx</a> Abhilfe schaffen. Besonders die nahtlose Integration in MySQL sticht hier als Pluspunkt heraus. So kann man den <a href="http://www.sphinxsearch.com/docs/current.html#sphinxse-using">Suchindex direkt über SQL-Abfragen</a> ansprechen und andere Tabellen hinzu-&#8221;joinen&#8221;.  In diesem Artikel wollen wir uns mit der Installation von Sphinx und SphinxSE, der Storage-Engine für MySQL, beschäftigen.<br />
<span id="more-168"></span><br />
Die Installationsanleitung bezieht sich auf Ubuntu 9.10 als Betriebssystem, lässt sich aber im Prinzip auch für andere Systeme/Versionen nachvollziehen. Folgende Versionen waren zum Zeitpunkt der Erstellung dieses Artikels aktuell:</p>
<ul>
<li>MySQL 5.1.37</li>
<li>Sphinx 0.9.9</li>
</ul>
<p>Wir gehen davon aus, dass MySQL bereits installiert ist, falls nicht, schnell nachholen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> mysql-server-<span style="color: #000000;">5.1</span></pre></div></div>

<h3>Sphinx installieren</h3>
<p>Zuerst holen wir uns die Quellen von Sphinx und entpacken Sie:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>www.sphinxsearch.com<span style="color: #000000; font-weight: bold;">/</span>downloads<span style="color: #000000; font-weight: bold;">/</span>sphinx-0.9.9.tar.gz
$ <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xvzf</span> sphinx-0.9.9.tar.gz
$ <span style="color: #7a0874; font-weight: bold;">cd</span> sphinx-0.9.9<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p>Da wir mit <code>libstemmer</code>-Support kompilieren wollen, um deutsches <a href="http://de.wikipedia.org/wiki/Stemming">Stemming</a> nutzen zu können (Standardmäßig unterstützt Sphinx nur Englisch und Russisch), holen und entpacken wir uns noch das entprechende Archiv:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>snowball.tartarus.org<span style="color: #000000; font-weight: bold;">/</span>dist<span style="color: #000000; font-weight: bold;">/</span>libstemmer_c.tgz
$ <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xvzf</span> libstemmer_c.tgz</pre></div></div>

<p>Nun <a href="http://www.sphinxsearch.com/docs/current.html#installing">installieren</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>configure <span style="color: #660033;">--with-libstemmer</span>
$ <span style="color: #c20cb9; font-weight: bold;">make</span>
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>Für die weitere Konfiguration verweise ich jetzt einfach mal auf die <a href="http://www.sphinxsearch.com/docs/">Dokumentation</a> und den <a href="http://www.sphinxsearch.com/docs/current.html#quick-tour">Quickstart</a> von Sphinx.</p>
<h3>SphinxSE installieren</h3>
<p>Wir wechseln zuerst ins Elternverzeichnis und holen uns die Quellen von MySQL:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #7a0874; font-weight: bold;">cd</span> ..<span style="color: #000000; font-weight: bold;">/</span>
$ <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #7a0874; font-weight: bold;">source</span> mysql-server-<span style="color: #000000;">5.1</span>
$ <span style="color: #7a0874; font-weight: bold;">cd</span> mysql-dfsg-<span style="color: #000000;">5.1</span>-5.1.37<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p>Nun kopieren wir die Dateien für die Storage-Engine in das entsprechende Verzeichnis:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #660033;">-R</span> ..<span style="color: #000000; font-weight: bold;">/</span>sphinx-0.9.9<span style="color: #000000; font-weight: bold;">/</span>mysqlse storage<span style="color: #000000; font-weight: bold;">/</span>sphinx</pre></div></div>

<p><code>./configure</code> beschwert sich wegen eines fehlenden <code>Docs/</code>-Ordners. Also schnell das Archiv von der MySQL-Seite holen und den entsprechenden Ordner ins Installationsverzeichnis kopieren:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>downloads.mysql.com<span style="color: #000000; font-weight: bold;">/</span>archives<span style="color: #000000; font-weight: bold;">/</span>mysql-<span style="color: #000000;">5.1</span><span style="color: #000000; font-weight: bold;">/</span>mysql-5.1.37.tar.gz
$ <span style="color: #c20cb9; font-weight: bold;">mkdir</span> temp
$ <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xzvf</span> mysql-5.1.37.tar.gz <span style="color: #660033;">-C</span> temp
$ <span style="color: #c20cb9; font-weight: bold;">mv</span> temp<span style="color: #000000; font-weight: bold;">/</span>mysql-5.1.37<span style="color: #000000; font-weight: bold;">/</span>Docs .
$ <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> temp
$ <span style="color: #c20cb9; font-weight: bold;">rm</span> mysql-5.1.37.tar.gz</pre></div></div>

<p>Nun gehts ans Bauen. Die <code>configure</code>-Flags kommen aus <code>debian/rules</code> und sollten denen entprechen, mit denen die Original-Installation von MySQL gebaut wurde:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ BUILD<span style="color: #000000; font-weight: bold;">/</span>autorun.sh
$ .<span style="color: #000000; font-weight: bold;">/</span>configure <span style="color: #660033;">--prefix</span>=<span style="color: #000000; font-weight: bold;">/</span>usr \
              <span style="color: #660033;">--exec-prefix</span>=<span style="color: #000000; font-weight: bold;">/</span>usr \
              <span style="color: #660033;">--libexecdir</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin \
              <span style="color: #660033;">--datadir</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share \
              <span style="color: #660033;">--localstatedir</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>mysql \
              <span style="color: #660033;">--includedir</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>include \
              <span style="color: #660033;">--infodir</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>info \
              <span style="color: #660033;">--mandir</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">man</span> \
              <span style="color: #660033;">--enable-thread-safe-client</span> \
              <span style="color: #660033;">--enable-local-infile</span> \
              <span style="color: #660033;">--with-pstack</span> \
              <span style="color: #660033;">--with-fast-mutexes</span> \
              <span style="color: #660033;">--with-big-tables</span> \
              <span style="color: #660033;">--with-unix-socket-path</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>run<span style="color: #000000; font-weight: bold;">/</span>mysqld<span style="color: #000000; font-weight: bold;">/</span>mysqld.sock \
              <span style="color: #660033;">--with-mysqld-user</span>=mysql \
              <span style="color: #660033;">--with-libwrap</span> \
              <span style="color: #660033;">--with-ssl</span> \
              <span style="color: #660033;">--without-docs</span> \
              <span style="color: #660033;">--with-extra-charsets</span>=all \
              <span style="color: #660033;">--with-embedded-server</span> \
              <span style="color: #660033;">--with-embedded-privilege-control</span>
$ <span style="color: #c20cb9; font-weight: bold;">make</span></pre></div></div>

<p>Danach finden wir im Verzeichnis <code>storage/sphinx/.libs</code> die ladbaren .so-Dateien für MySQL. Diese speichern wir nun in <code>/usr/lib/mysql/plugin</code></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>mysql<span style="color: #000000; font-weight: bold;">/</span>plugin
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">cp</span> storage<span style="color: #000000; font-weight: bold;">/</span>sphinx<span style="color: #000000; font-weight: bold;">/</span>.libs<span style="color: #000000; font-weight: bold;">/</span>ha_sphinx.so<span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>mysql<span style="color: #000000; font-weight: bold;">/</span>plugin<span style="color: #000000; font-weight: bold;">/</span>
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chown</span> <span style="color: #660033;">-R</span> mysql:mysql <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>mysql<span style="color: #000000; font-weight: bold;">/</span>plugin</pre></div></div>

<p>Den Ort müssen wir nun noch MySQL mitteilen. Wir editieren <code>/etc/mysql/my.cnf</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">vi</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>mysql<span style="color: #000000; font-weight: bold;">/</span>my.cnf</pre></div></div>

<p>Im Abschnitt <code>[mysqld]</code> fügen wir folgende Zeile ein:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">plugin_dir = /usr/lib/mysql/plugin</pre></div></div>

<p>Ubuntu nutzt <a href="http://de.wikipedia.org/wiki/AppArmor">AppArmor</a> um zu regeln, welche Programme ausgeführt bzw. geladen werden dürfen. Also müssen wir AppArmor über den neuen Plugin-Ordner informieren:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">vi</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apparmor.d<span style="color: #000000; font-weight: bold;">/</span>usr.sbin.mysqld</pre></div></div>

<p>Wir fügen folgende Zeile hinzu:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">/usr/lib/mysql/plugin/* mr,</pre></div></div>

<p>Danach AppArmor und MySQL neu starten:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apparmor restart
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>mysql restart</pre></div></div>

<p>Zu guter Letzt installieren wir das Plugin:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ mysql <span style="color: #660033;">-u</span> root <span style="color: #660033;">-p</span>
mysql<span style="color: #000000; font-weight: bold;">&gt;</span> INSTALL PLUGIN sphinx SONAME <span style="color: #ff0000;">'ha_sphinx.so'</span>;
Query OK, <span style="color: #000000;">0</span> rows affected <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">0.00</span> sec<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>Danach sollte Sphinx als Engine auftauchen und die &#8220;Support&#8221;-Spalte sollte &#8220;YES&#8221; enthalten:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">mysql&gt; show engines;
+------------+----------+---------------------------------------------------------+
| Engine     | Support  | Comment                                                 |
+------------+----------+---------------------------------------------------------+
| MyISAM     | DEFAULT  | Default engine as of MySQL 3.23 with great performance  |
  ...
| SPHINX     | YES      | Sphinx storage engine 0.9.9                             |
  ...
+------------+----------+---------------------------------------------------------+
13 rows in set (0.00 sec)</pre></div></div>

<h3>Links zum Thema</h3>
<ul>
<li><a href="http://www.sphinxsearch.com">Sphinx</a></li>
<li><a href="http://www.sphinxsearch.com/docs/current.html#sphinxse">SphinxSE Dokumentation</a></li>
<li><a href="http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html">MySQL Fulltext-Index</a></li>
</ul>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" class="tt" href="http://twitter.com/intent/tweet?text=Sphinx+und+SphinxSE+unter+Ubuntu+installieren+http%3A%2F%2Fsorgalla.com%2F%3Fp%3D168+%2Fby+%40jansor" title="Post to Twitter"><img class="nothumb" src="http://sorgalla.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://delicious.com/post?url=http://sorgalla.com/2009/12/15/sphinx-und-sphinxse-unter-ubuntu-installieren/&amp;title=Sphinx+und+SphinxSE+unter+Ubuntu+installieren" title="Post to Delicious"><img class="nothumb" src="http://sorgalla.com/wp-content/plugins/tweet-this/icons/en/delicious/tt-delicious.png" alt="Post to Delicious" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://sorgalla.com/2009/12/15/sphinx-und-sphinxse-unter-ubuntu-installieren/&amp;title=Sphinx+und+SphinxSE+unter+Ubuntu+installieren" title="Post to Digg"><img class="nothumb" src="http://sorgalla.com/wp-content/plugins/tweet-this/icons/en/digg/tt-digg.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://sorgalla.com/2009/12/15/sphinx-und-sphinxse-unter-ubuntu-installieren/&amp;t=Sphinx+und+SphinxSE+unter+Ubuntu+installieren" title="Post to Facebook"><img class="nothumb" src="http://sorgalla.com/wp-content/plugins/tweet-this/icons/en/facebook/tt-facebook.png" alt="Post to Facebook" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://sorgalla.com/2009/12/15/sphinx-und-sphinxse-unter-ubuntu-installieren/&amp;title=Sphinx+und+SphinxSE+unter+Ubuntu+installieren" title="Post to Reddit"><img class="nothumb" src="http://sorgalla.com/wp-content/plugins/tweet-this/icons/en/reddit/tt-reddit.png" alt="Post to Reddit" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://sorgalla.com/2009/12/15/sphinx-und-sphinxse-unter-ubuntu-installieren/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<series:name><![CDATA[Schöner suchen mit Sphinx]]></series:name>
	</item>
	</channel>
</rss>

