CentOS5.7ã§å ¨ææ¤ç´¢ã¨ã³ã¸ã³Apache Solr3.4.0ã®ã¤ã³ã¹ãã¼ã«ããMySQLãã¼ã¿ã®ã¤ã³ãã¼ãã¾ã§
Apache Solr
http://lucene.apache.org/solr/
Apache Solrï¼ã½ã¼ã©ã¼ï¼ã¯Apacheã³ãã¥ããã£ã«ãã£ã¦ãªã¼ãã³ã½ã¼ã¹ã§éçºããã¦ããå
¨ææ¤ç´¢ã¨ã³ã¸ã³ã§ãã
Apache Luceneã®HTTPã©ããã¼ã®ãããªãã®ã§ãã
Luceneã§ã¯å
¨ææ¤ç´¢ãè¡ãçºã«ã¯ãããªãã«ã³ã¼ããæ¸ããªããã°ãããªãã§ãããSolrã使ãã¨Solrã«å¯¾ãã¦HTTPã®ãªã¯ã¨ã¹ããè¡ãã ãã§æå®ãããã¼ã¿ãHTTPã¬ã¹ãã³ã¹ã§è¿ãã¦ãã¦ããã¾ãã
å©ç¨ããã«ã¯Tomcatãªã©ã®ãµã¼ãã¬ããã³ã³ãããå©ç¨ããå¿
è¦ãããã¾ãã
MySQLã®ãã¼ã¿ãã¼ã¹ã«ãããã¼ã¿ãSolrã«ã¤ã³ãã¼ããã¦ãã¼ã¿ãæ¤ç´¢ããã¨ãã£ããã¨ãå¯è½ã§ãã
ç¾æç¹ï¼2011/09/16ï¼ã§ææ°ã®Solrã¯3.4.0ã§ãã1.4.xã®æã¯å梱ããã¦ããJettyã®ãã¼ã¸ã§ã³ã¯6.13ã§ããããSolr3.4.0ã§ã¯6.1.26ã«ãªã£ã¦ãã¾ãã
Solr3.1ã3.3ã¯ã¤ã³ããã·ã³ã°ä¸ã«ãã·ã³ãã¯ã©ãã·ã¥ãããåæ¢ããå ´åãã¤ã³ããã¯ã¹ãç ´å£ããã¦ãã¾ããã¨ãããã®ã§å©ç¨ããªãã»ãããããããã§ãï¼3.4ã§ä¿®æ£)
æ å ±å ï¼第6回Solr勉強会に参加しました。
äºåæºå
JDKã®ãã¦ã³ãã¼ã
http://java.sun.com/javase/ja/6/download.html
Solr3.4ã®ãã¦ã³ãã¼ã
wget http://ftp.kddilabs.jp/infosystems/apache//lucene/solr/3.4.0/apache-solr-3.4.0.tgz
JDBCãã¦ã³ãã¼ã
wget http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.17.zip/from/http://ftp.iij.ad.jp/pub/db/mysql/
JDKã®ã¤ã³ã¹ãã¼ã«
chmod +x /home/work/jdk-6u25-linux-x64-rpm.bin /home/src/jdk-6u25-linux-x64-rpm.bin
Apache Solrã®ã¤ã³ã¹ãã¼ã«
tar zxvf apache-solr-3.4.0.tgz mv apache-solr-3.4.0 /usr/local/ ln -s /usr/local/apache-solr-3.4.0 /usr/local/apache-solr
solrã®èµ·åã¹ã¯ãªãããä½æãã
vi /etc/init.d/solr
#!/bin/sh # chkconfig: 345 90 90 # description: Solr Boot JETTY_HOME_DIR=/usr/local/apache-solr/example/ cd $JETTY_HOME_DIR JAVA="/usr/bin/java" LOG_FILE="/var/log/solr.log" KEY=stopkey CORE=solr cd $JETTY_HOME_DIR start() { $JAVA -Dsolr.solr.home=$CORE -DSTOP.PORT=8079 -DSTOP.KEY=$KEY -jar start.jar >> $LOG_FILE 2>&1 & echo "Solr started!" } stop() { $JAVA -DSTOP.PORT=8079 -DSTOP.KEY=$KEY -jar start.jar --stop echo "Solr stopped!" } case "$1" in start) start ;; stop) stop ;; restart) stop start ;; *) echo "Usage: $0 {start|stop|restart}" exit 1 esac
JDBCã®ã¤ã³ã¹ãã¼ã«
mkdir /usr/local/apache-solr/example/solr/lib unzip mysql-connector-java-5.1.17.zip cp mysql-connector-java-5.1.17/mysql-connector-java-5.1.17-bin.jar /usr/local/apache-solr/example/solr/lib/
èµ·å
chmod 755 /etc/init.d/solr chkconfig --add solr /etc/init.d/solr start
åä½ãã¹ãç¨ã¹ãã¼ãã®ä½æ
mysql> CREATE DATABASE products; mysql >GRANT ALTER,SELECT,INSERT,UPDATE,DELETE ON products.* TO user@localhost IDENTIFIED BY 'password'; mysql> flush privileges; mysql> CREATE TABLE maker ( mid INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, name VARCHAR(16) NOT NULL ) ENGINE=InnoDB; mysql> CREATE TABLE product ( id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, mid INT UNSIGNED NOT NULL, name VARCHAR(16) NOT NULL ) ENGINE=InnoDB; INSERT INTO maker(name) VALUES('au'); INSERT INTO maker(name) VALUES('DoCoMo'); INSERT INTO maker(name) VALUES('Softbank'); INSERT INTO product(mid, name) VALUES (1, 'IS01'); INSERT INTO product(mid, name) VALUES (1, 'CA002'); INSERT INTO product(mid, name) VALUES (1, 'W63K'); INSERT INTO product(mid, name) VALUES (2, 'BlackBerry'); INSERT INTO product(mid, name) VALUES (3, 'iPhone'); INSERT INTO product(mid, name) VALUES (3, 'ããããæºå¸¯');
Apache Solrã®è¨å®
solrconfig.xmlã®è¨å®
以ä¸ã®å
容ã
vi /usr/local/apache-solr/example/solr/conf/solrconfig.xml
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">data-config.xml</str> </lst> </requestHandler>
data-config.xmlã®ä½æ
vi /usr/local/apache-solr/example/solr/conf/data-config.xml
<dataConfig> <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/products" user="db_user" password="****"/> <document> <entity name="product" query="select * from product"> <field column="id" name="id" /> <field column="name" name="product" /> <entity name="maker" query="select * from maker where mid = '${product.mid}'"> <field column="name" name="name" /> </entity> </entity> </document> </dataConfig>
schema.xmlã®è¨å®
vi /usr/local/apache-solr/example/solr/conf/schema.xml
<field name="product" type="text_general" indexed="true" stored="true"/>
<defaultSearchField>product</defaultSearchField>
Solråèµ·å
/etc/init.d/solr restart
ãã¼ã¿ãã¼ã¹ã®å 容ãSolrã«ã¤ã³ããã¯ã¹ããã
http://localhost:8983/solr/dataimport?command=full-import
æ¤ç´¢ç¢ºèª
http://localhost:8983/solr/admin/
管çç»é¢ã«ããQuery String:ã®é¨åã«æ¤ç´¢èªå
¥åãä¾:iphone
ãããã¯
http://localhost:8983/solr/select/?q=iphone
ã«ã¢ã¯ã»ã¹ã帰ã£ã¦ããXMLã«æ¤ç´¢çµæãå«ã¾ãã¦ããã°æåã§ãã
<response> <lst name="responseHeader"> <int name="status">0</int> <int name="QTime">0</int> <lst name="params"> <str name="q">iphone</str> </lst> </lst> <result name="response" numFound="1" start="0"> <doc> <str name="id">5</str> <str name="name">Softbank</str> <str name="product">iPhone</str> </doc> </result> </response>