solr环境搭建+连接postgres数据库+查询数据

2018-04-24  本文已影响168人  小YiY

被分配了个用solr连接postgres数据库并查询的任务。上网找了找教程,连接数据库并查询成功。现在把过程记录一下。

首先明确步骤:
1.安装solr
2.启动项目,将solr提供的example执行成功
3.自己照着写一个。
    3.1  配置solr文件
    3.2  建索引
    3.3  根据索引查数据
1.安装solr

1.1 下载solr安装包,网址:http://archive.apache.org/dist/lucene/solr/
我这里直接用solr自带的Jetty服务器,网上有些教程是使用solr+tomcat的方式。
我下载的安装包是solr-6.4.2版本。如下图:右键保存到本地进行下载

下载安装包.png

1.2 下载成功之后,解压

2.运行项目,查看solr的example

命令行,进入解压出来的solr文件夹的bin目录下。

2.1执行命令

solr -e dih  //启动项目,运行example文件夹下的项目

浏览器打开8993端口:http://localhost:8983
效果如下所示

solr-example.png

在Core Selector下拉框可以看到db、mail、rss等,这是slor里自带的example,先看一下他的example。在文件夹solr-6.4.2\example\example-DIH\solr下可以看到


example.png

接下来,在浏览器列表中选择db,再选择Dataimport
2.2生成索引
点击Execute,再点击Refresh Status
可以看到绿色的文字Indexing completed.Added/Updated...............
这个时候索引就已经生成好了。如下图:

生成索引.png

2.3查询数据
点击列表里的Query
点击Execute Query
数据就查询到了。
如下图:

查询数据.png
3.自己动手建一个

3.1执行stop命令

slor stop -all//先停止之前的example项目

3.2执行start命令

slor start 

3.3建项目

solr create -c <projectname>

建的项目可以在solr-6.4.2\server\solr文件夹下找到。
3.4配置文件
3.4.1修改solrconfig.xml
如下:在select的requestHandler上面加上dataimport,配置<str name="config">data-config.xml</str>

  <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
          <lst name="defaults">
             <str name="config">data-config.xml</str>
          </lst>
  </requestHandler>

  <requestHandler name="/select" class="solr.SearchHandler">
    <lst name="defaults">
      <str name="echoParams">explicit</str>
      <int name="rows">10</int>
    </lst>
  </requestHandler>

3.4.2添加data-config.xml文件
在当前文件夹的目录下添加data-config.xml文件,文件内容如下,document标签下是自己要查的表:

<?xml version="1.0" encoding="UTF-8"?>
<dataConfig>
    <dataSource type="JdbcDataSource" driver="org.postgresql.Driver" url="jdbc:postgresql://192.168.3.100:6432/fmbizdb" user="postgres" password="fmbj2014_psql" encoding="UTF-8" />
      <document>
        <entity name="fm_agr_operation_record" pk="id" query="select * from fm_agr_operation_record">
                      <field column="id" name="id"/>
                      <field column="name" name="name"/>
                        <field column="series_id" name="series_id"/>
                        <field column="operation_type" name="operation_type"/>
                        <field column="problem_id" name="problem_id"/>
                        <field column="responsible_man" name="responsible_man"/>
                        <field column="else_responsible" name="else_responsible"/>
                        <field column="description" name="description"/>
                        <field column="from_date" name="from_date"/>
                        <field column="thru_date" name="thru_date"/>
                        <field column="status" name="status"/>
                        <field column="last_updated_stamp" name="last_updated_stamp"/>
                        <field column="last_updated_tx_stamp" name="last_updated_tx_stamp"/>
                        <field column="created_stamp" name="created_stamp"/>
                        <field column="created_tx_stamp" name="created_tx_stamp"/>
                        <field column="organization_id" name="organization_id"/>
                        <field column="plan_id" name="plan_id"/>
                        <field column="end_date" name="end_date"/>
                        <field column="start_date" name="start_date"/>
                        <field column="finish_status" name="finish_status"/>
                        <field column="image_extern" name="image_extern"/>
                        <field column="examine_man" name="examine_man"/>
                        <field column="supply_extern" name="supply_extern"/>
             </entity>
      </document>
</dataConfig>

3.4.3修改目录下的managed-schema文件
添加对应的字段

<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
        <field name="name" type="string" indexed="true" stored="true" />
        <field name="series_id" type="string" indexed="true"/>
        <field name="operation_type" type="string" indexed="true"/>
        <field name="problem_id" type="string" indexed="true"/>
        <field name="responsible_man" type="string" indexed="true"/>
        <field name="else_responsible" type="string" indexed="true"/>
        <field name="description" type="string" indexed="true"/>
        <field name="from_date" type="date" indexed="true"/>
        <field name="thru_date" type="date" indexed="true"/>
        <field name="status" type="string" indexed="true"/>
        <field name="last_updated_stamp" type="date" indexed="true"/>
        <field name="last_updated_tx_stamp" type="date" indexed="true"/>
        <field name="created_stamp" type="date" indexed="true"/>
        <field name="created_tx_stamp" type="date" indexed="true"/>
        <field name="organization_id" type="string" indexed="true"/>
        <field name="plan_id" type="string" indexed="true"/>
        <field name="end_date" type="date" indexed="true"/>
        <field name="start_date" type="date" indexed="true"/>
        <field name="finish_status" type="string" indexed="true"/>
        <field name="image_extern" type="string" indexed="true"/>
        <field name="examine_man" type="string" indexed="true"/>
        <field name="supply_extern" type="string" indexed="true"/>

此时,在回到页面上,访问自己建的项目名,再点击dataimport

上一篇下一篇

猜你喜欢

热点阅读