您的位置:首页 > 运维架构

solr学习第三课----solr索引维护-基于solr搜索引擎

2013-11-19 12:18 441 查看
1. add or update index

第一步:创建文件addOrUpdateIndex.xml,编辑该文件,文件内容为

<add>
        <doc>
                <field name="id">100000000000261</field>
                <field name="wareCode">100261</field>
                <field name="wareName">70元流量包</field>
                <field name="wareStatus">2</field>
                <field name="openDate">2009-12-07T16:00:00Z</field>
                <field name="shopPrice">0</field>
                <field name="marketPrice">1300</field>
                <field name="deliverType">11</field>
                <field name="wareIntegralValue">1200</field>
                <field name="gCurValue">30</field>
                <field name="mCurValue">300</field>
                <field name="eCurValue">624606015</field>
                <field name="isHtmlChannel">1</field>
                <field name="isWapChannel">1</field>
                <field name="isSearch">1</field>
                <field name="sKind">10000000000000</field>
                <field name="sName">流量包1</field>
                <field name="mKind">10000000000001</field>
                <field name="mName">流量包2</field>
        </doc>
        <doc>
                <field name="id">100000000000361</field>
                <field name="wareCode">100361</field>
                <field name="wareName">100元流量包</field>
                <field name="wareStatus">2</field>
                <field name="openDate">2023-12-07T16:00:00Z</field>
                <field name="shopPrice">20</field>
                <field name="marketPrice">2300</field>
                <field name="deliverType">11</field>
                <field name="wareIntegralValue">1200</field>
                <field name="gCurValue">30</field>
                <field name="mCurValue">300</field>
                <field name="eCurValue">62</field>
                <field name="isHtmlChannel">1</field>
                <field name="isWapChannel">1</field>
                <field name="isSearch">1</field>
                <field name="sKind">10000000000000</field>
                <field name="sName">流量包1</field>
                <field name="mKind">10000000000001</field>
                <field name="mName">流量包2</field>
        </doc>
</add>


第二步:进入命令行模式,然后直接一下命令

java -Durl=http://192.168.204.135:9081/solr/update -Ddata=files -jar post.jar addOrUpdateIndex.xml

通过shell脚本添加索引:

[root@demoserver exampledocs]# pwd

/opt/soft/solr-4.4.0/example/exampledocs

[root@demoserver exampledocs]# cat post.sh

#!/bin/sh
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0 #
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FILES=$*
URL=http://192.168.17.128:9080/solr/core0/update
for f in $FILES; do
  echo Posting file $f to $URL
  curl $URL --data-binary @$f -H 'Content-type:application/xml' 
  echo
done

#send the commit command to make sure all the changes are flushed and visible
#curl $URL --data-binary '<commit softCommit=true/>' -H 'Content-type:application/xml'

curl "$URL?softCommit=true"
echo


执行shell脚本过程:

[root@demoserver exampledocs]# ./post.sh addOrUpdateWareInfo.xml

Posting file addOrUpdateWareInfo.xml to http://192.168.17.128:9080/solr/core0/update
<?xml version="1.0" encoding="UTF-8"?>

<response>

<lst name="responseHeader"><int name="status">0</int><int name="QTime">22</int></lst>

</response>

<?xml version="1.0" encoding="UTF-8"?>

<response>

<lst name="responseHeader"><int name="status">0</int><int name="QTime">54</int></lst>

</response>

[root@demoserver exampledocs]#

2. solr下删除索引

Solr 删除全部索引:http://192.168.204.135:9081/solr/update/?stream.body=<delete><query>*:*</query></delete>&stream.contentType=text/xml;charset=utf-8&commit=true

solr 删除指定ID的索引:http://192.168.204.135:9081/solr/update/?stream.body=<delete><id>1403001</id></delete>&stream.contentType=text/xml;charset=utf-8&commit=true

3. 查询数据

单个条件:
http://192.168.204.135:9081/solr/collection1/select?q=username:%E6%B2%88%E7%A6%8F
多个条件:
http://192.168.204.135:9081/solr/collection1/select?q=usermail:shenfl%20AND%20usercode:14030
执行后查看结果:

通过执行http://192.168.17.128:9080/solr/core0/select?q=*%3A*&wt=json&indent=true

{
  "responseHeader":{
    "status":0,
    "QTime":2},
  "response":{"numFound":2,"start":0,"docs":[
      {
        "id":"100000000000261",
        "wareCode":"100261",
        "wareName":"70元流量包",
        "wareStatus":"2",
        "openDate":"2009-12-07T16:00:00Z",
        "shopPrice":"0",
        "marketPrice":"1300",
        "deliverType":"11",
        "wareIntegralValue":1200,
        "gCurValue":30,
        "mCurValue":300,
        "eCurValue":624606015,
        "isHtmlChannel":"1",
        "isWapChannel":"1",
        "isSearch":"1",
        "sKind":10000000000000,
        "sName":"流量包1",
        "mKind":10000000000001,
        "mName":"流量包2",
        "_version_":1460204081034297344},
      {
        "id":"100000000000361",
        "wareCode":"100361",
        "wareName":"100元流量包",
        "wareStatus":"2",
        "openDate":"2023-12-07T16:00:00Z",
        "shopPrice":"20",
        "marketPrice":"2300",
        "deliverType":"11",
        "wareIntegralValue":1200,
        "gCurValue":30,
        "mCurValue":300,
        "eCurValue":62,
        "isHtmlChannel":"1",
        "isWapChannel":"1",
        "isSearch":"1",
        "sKind":10000000000000,
        "sName":"流量包1",
        "mKind":10000000000001,
        "mName":"流量包2",
        "_version_":1460204081041637376}]
  }}


通过执行http://192.168.17.128:9080/solr/core0/select?q=*%3A*&wt=xml&indent=true

<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">0</int>
</lst>
<result name="response" numFound="2" start="0">
<doc>
<str name="id">100000000000261</str>
<str name="wareCode">100261</str>
<str name="wareName">70元流量包</str>
<str name="wareStatus">2</str>
<date name="openDate">2009-12-07T16:00:00Z</date>
<str name="shopPrice">0</str>
<str name="marketPrice">1300</str>
<str name="deliverType">11</str>
<long name="wareIntegralValue">1200</long>
<long name="gCurValue">30</long>
<long name="mCurValue">300</long>
<long name="eCurValue">624606015</long>
<str name="isHtmlChannel">1</str>
<str name="isWapChannel">1</str>
<str name="isSearch">1</str>
<long name="sKind">10000000000000</long>
<str name="sName">流量包1</str>
<long name="mKind">10000000000001</long>
<str name="mName">流量包2</str>
<long name="_version_">1460204081034297344</long>
</doc>
<doc>
<str name="id">100000000000361</str>
<str name="wareCode">100361</str>
<str name="wareName">100元流量包</str>
<str name="wareStatus">2</str>
<date name="openDate">2023-12-07T16:00:00Z</date>
<str name="shopPrice">20</str>
<str name="marketPrice">2300</str>
<str name="deliverType">11</str>
<long name="wareIntegralValue">1200</long>
<long name="gCurValue">30</long>
<long name="mCurValue">300</long>
<long name="eCurValue">62</long>
<str name="isHtmlChannel">1</str>
<str name="isWapChannel">1</str>
<str name="isSearch">1</str>
<long name="sKind">10000000000000</long>
<str name="sName">流量包1</str>
<long name="mKind">10000000000001</long>
<str name="mName">流量包2</str>
<long name="_version_">1460204081041637376</long>
</doc>
</result>
</response>

通过执行http://192.168.17.128:9080/solr/core0/select?q=*%3A*&wt=csv&indent=true

mKind,gCurValue,marketPrice,_version_,mCurValue,sKind,isHtmlChannel,eCurValue,id,wareIntegralValue,isWapChannel,isSearch,wareName,shopPrice,name,deliverType,mName,sName,wareCode,openDate,wareStatus
10000000000001,30,1300,1460204081034297344,300,10000000000000,1,624606015,100000000000261,1200,1,1,70元流量包,0,,11,流量包2,流量包1,100261,2009-12-07T16:00:00Z,2
10000000000001,30,2300,1460204081041637376,300,10000000000000,1,62,100000000000361,1200,1,1,100元流量包,20,,11,流量包2,流量包1,100361,2023-12-07T16:00:00Z,2


4. 批量索引操作

第一种:通过CVS文件的方式提交

修改conf/solrconfig.xml

1)新增csv处理配置项

<!-- CSV update handler, loaded on demand -->

<requestHandler name="/update/csv"

class="solr.CSVRequestHandler"

startup="lazy">

</requestHandler>

2)修改enableRemoteStreaming=true

<requestParsers enableRemoteStreaming="true" multipartUploadLimitInKB="2048" />

3)下面的命令能直接读取输入文件提交到Solr

curl http://192.168.204.135:8983/solr/update/csv?stream.file=books.csv&stream.contentType=text/plain;charset=utf-8
5. 使用solr创建例子

经典例子:http://blog.csdn.net/yjflinchong/article/details/7559508
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: