Solr本地服務器搭建及查詢

0.安裝solr以前,確保已安裝好java8,  java -version 查看是否安裝

1.新建本地目錄solr1 並 解壓兩個壓縮包文件

tar -xf apache-tomcat-7.0.59.tar.gz
tar -xf solr-4.10.3.tgz
mv apache-tomcat-7.0.59 tomcat7

2.將CATALINA_HOME寫入到~/.bashrc

echo "export CATALINA_HOME=$basepath/tomcat7" >> ~/.bashrc
source ~/.bashrc

3.驗證tomcat是否能夠啓動

./tomcat7/bin/startup.sh
curl http://localhost:8080

若是OK,則會出現首頁源碼,點擊網址會以下:java

 

4.配置solr,拷貝必要的包

mkdir -p solr-test
cp -r solr-4.10.3/example/solr solr-test
cp solr-4.10.3/dist/solr-4.10.3.war solr-test/solr/solr.war
cp solr-4.10.3/example/lib/*.jar tomcat7/lib
cp solr-4.10.3/example/lib/ext/*.jar tomcat7/lib

5.配置本地solr.xml文件

mkdir -p ./tomcat7/conf/Catalina/localhost

touch ./tomcat7/conf/Catalina/localhost/solr.xml

echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>" > tomcat7/conf/Catalina/localhost/solr.xml
echo " <Context docBase=\"$basepath/solr-test/solr/solr.war\" debug=\"0\" crossContext=\"true\">" >> tomcat7/conf/Catalina/localhost/solr.xml
echo " <Environment name=\"solr/home\" type=\"java.lang.String\" value=\"$basepath/solr-test/solr\" override=\"true\" >" >> tomcat7/conf/Catalina/localhost/solr.xml
echo " </Context>" >> tomcat7/conf/Catalina/localhost/solr.xml

6.配置solr-test/solr/collection1/conf/schema.xml文件

這個能夠用xml parser來解決shell

### 1. 把全部除id外的field所有註釋掉,例如:
### <!-- field name="_root_" type="string" indexed="true" stored="false"/-->
### 2. 修改id的屬性以下:
### <field name="id" type="string" indexed="true" stored="false" required="true" multiValued="false" />
### 3. 增長一個field叫pat:
### <field name="pat" type="text_general" indexed="true" stored="true" required="true" multiValued="False"/>
### 4. 把全部copyField註釋掉,例如:
### <!-- copyField source="title" dest="text"/–>

7.配置solr-test/solr/collection1/conf/solrconfig.xml

### 1. 註釋掉updateLog:
### <!-- updateLog>
### <str name="dir">${solr.ulog.dir:}</str>
### </updateLog-->
### 2. 修改update/csv的屬性
### <requestHandler name="/update/csv" class="solr.UpdateRequestHandler">
### <lst name="defaults">
### <str name="separator">;</str>
### <str name="header">true</str>
### <str name="encapsulator">"</str>
### <str name="stream.contentType">text/csv</str>
### </lst>
### </requestHandler>

8.導入數據,開始幹活

寫一個shell腳本,例如:vim sss.shapache

裏面內容:vim

#!/bin/bash
basepath=$(cd `dirname $0`; pwd)
patpath=$1

sort -u $patpath -o pat.unique
awk 'BEGIN{print "id;pat"} {print $0";\""$0"\"";}' pat.unique > pat.csv
./tomcat7/bin/shutdown.sh
./tomcat7/bin/startup.sh
curl "http://localhost:8080/solr/collection1/update/csv?commit=true&stream.file=$basepath/pat.csv&stream.contentType=text/csv;charset=UTF-8"

保存腳本內容tomcat

運行:./sss.sh  +  pat文件bash

例: ./sss.sh  gushi.v4.pat服務器

9.完成,準備檢索pat

curl "http://localhost:8080/solr/collection1/select?q=pat:故事名&rows=5"   (能夠對查的內容和顯示數量作調整)數據結構

返回xml回顯信息curl

直接進網頁能夠看到:ide

 

好,到目前爲止,咱們的本地solr服務器基本搭建ok。

10查詢測試

本地登入http://localhost:8080/solr/#/  管理頁面,以下:

 

 

見咱們本身的示例:

這張圖能夠查不少東西,例如展現的數據結構,數量,很豐富

 

11.多核查詢

以上是配置一個core的方法, 若要配置多個core, 只須要在solr-test/solr下面, 複製一下collection1

# 例如, 爲了使不一樣領域的pat能夠分開查詢, 能夠複製一個core叫navi
# 但必定要記得修改navi下面的core.properties的內容, 裏面的name必須跟core的名字一致, 不然solr會沒法啓動
# cd solr-test/solr
# cp -r collection1 navi
# echo "name=navi" > navi/core.properties
# 添加完core後, 重啓tomcat7
# ./tomcat7/bin/shutdown.sh
# ./tomcat7/bin/startup.sh

 

見效果:

相關文章
相關標籤/搜索