當前位置:首頁 » 操作系統 » solr連接資料庫

solr連接資料庫

發布時間: 2022-04-27 13:07:00

Ⅰ 如何使用Solr索引Mysql資料庫

在solr與tomcat整合文章中,我用的索引庫是mycore,現在就以這個為例。
首先要准備jar包:solr-dataimporthandler-4.8.1.jar、solr-dataimporthandler-extras-4.8.1.jar和mysql-connector-java-5.0.7-bin.jar這三個包到solr的tomcat的webapps\solr\WEB-INF\lib下
在這個文件夾的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>
在solrconfig.xml的同目錄下創建data-config.xml。
配置:
復制代碼
<dataConfig>
<dataSource type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/courseman"
user="root"
password="mysql" />
<document>
<entity name="student"
query="SELECT * FROM student">
<field column="id" name="id" />
<field column="name" name="name" />
<field column="gender" name="gender" />
<field column="major" name="major" />
<field column="grade" name="grade" />
</entity>
</document>
</dataConfig>
復制代碼
schemal.xml的配置
復制代碼
<?xml version="1.0" ?>
<!--
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 right 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 of the License at

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.
-->
<schema name="example core one" version="1.1">
<fieldtype name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>
<fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/>
<!-- general -->
<field name="id" type="int" indexed="true" stored="true" />
<field name="gender" type="string" indexed="true" stored="true" />
<field name="name" type="string" indexed="true" stored="true" />
<field name="major" type="string" indexed="true" stored="true" />
<field name="grade" type="string" indexed="true" stored="true" />
<field name="_version_" type="long" indexed="true" stored="true"/>
<!-- field to use to determine and enforce document uniqueness. -->
<uniqueKey>id</uniqueKey>
<!-- field for the QueryParser to use when an explicit fieldname is absent -->
<defaultSearchField>name</defaultSearchField>
<!-- SolrQueryParser configuration: defaultOperator="AND|OR" -->
<solrQueryParser defaultOperator="OR"/>
</schema>
復制代碼
默認的文件不是這樣的,稍微改動了一下。
field 的type類型是根據fieldtype 的name定義的。class是solr自定義的不能更改。
shcema.xml文件的field欄位的屬性介紹:
(1)name:欄位名稱
(2)type:欄位類型(此處type不是java類型,而是下面定義的fieldType)
(3)indexed:是否索引看true--solr會對這個欄位進行索引,只有經過索引的欄位才能被搜索、排序等;false--不索引
(4)stored:是否存儲看true--存儲,當我們需要在頁面顯示此欄位時,應設為true,否則false。
(5)required:是否必須看true--此欄位為必需,如果此欄位的內容為空,會報異常;false--不是必需
(6)multiValued:此欄位是否可以保存多個值看
(7)omitNorms:是否對此欄位進行解析看有時候我們想通過某個欄位的完全匹配來查詢信息,那麼設置 indexed="true"、omitNorms="true"。
(8)default:設置默認值
有這樣一個FieldType描述:
<fieldType name="text_general" positionIncrementGap="100">
<analyzer type="index">
<tokenizer/>
<filter ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
<filter/>
</analyzer>
<analyzer type="query">
<tokenizer/>
<filter ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
<filter synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
<filter/>
</analyzer>
</fieldType>
屬性說明:
(1)name:類型名稱,<field>中的type引用的就是這個name
(2)class:solr自定義的類型
(3)<analyzer type="index">定義建立索引時使用的分詞器及過濾器
(4)<analyzer type="query">定義搜索時所使用的分詞器及過濾器
(5)<tokenizer/>定義分詞器
(6)<filter/>定義過濾器
uniqueKey屬性
<uniqueKey>id</uniqueKey>
類似於數據表數據的id,solr索引庫中最好定義一個用於標示document唯一性的欄位,此欄位主要用於刪除document。
defaultSearchField屬性
就是你在做query搜尋時若不指定特定欄位做檢索時, Solr就會只查這個欄位.
<defaultSearchField>default</defaultSearchField>
Field屬性
是用來復制你一個欄位里的值到另一欄位用. 如你可以將name里的東西到major里, 這樣solr做檢索時也會檢索到name里的東西.
<Field source="name" dest="major"/>
現在可以將資料庫的數據導入solr了。
點擊Execute就可以了。

Ⅱ solr怎麼與資料庫滯

關系型資料庫有四個顯著的特徵,即安全性、完整性、並發性和監測性。資料庫的安全性就是要保證資料庫中數據的安全,防止未授權用戶隨意修改資料庫中的數據,確保數據的安全。在大多數資料庫管理系統中,主要是通過許可來保證資料庫的安全性。

Ⅲ 有資料庫為什麼要solr

嚴格來說,lucene負責數據存儲,而solr只是一個引擎提供搜索和插入而已,跟資料庫的解釋器一樣,有什麼好處呢,比如一個資料庫有一個欄位存了1000個字,你想從這些字裡面搜一個詞的時候,普通的資料庫只會讓你使用like去查詢,他會遍歷每個字去模糊匹配,效率很低,而且有些是無法查詢的,當然除了像一些特殊的資料庫帶有分詞,比如postgresql,那lucene做的事情就是分詞,然後去匹配分詞的詞中是否有你想搜的詞就好了,當然了,為了提高這種檢索效率和內存節省底層做了很復雜的事情,可以這么簡單的認為,全文搜索這件事情上資料庫是無法滿足的

Ⅳ solr+tomcat如何配置

tomcat配置數據源solr使用數據源

1、tomcat中配置數據源(註:需要拷貝jdbc相關jar包到tomcat中。tomcat6.x/lib目錄下,注意tomcat6.0以下的版本在tomcat5.x/common/lib/目錄下)
在server.xml文件,找到「Engine-> Host -> Context」,在其下面配置主要針對某一項目的數據源使用。
在context.xml文件,找到「Context」,在其下面配置可以讓所有項目使用。

在Context下面配置數據源如下:
<Resource driverClassName="oracle.jdbc.driver.OracleDriver" maxActive="1000" maxIdle="30" maxWait="-1" name="jdbc/orcl" password="bb" type="javax.sql.DataSource" url="jdbc:oracle:thin:@127.0.0.1:1521:orcl" username="aa"/>

數據源相關參數說明:
<Resource
name="jdbc/bookstore" //這里的名字隨便起,後面要用到
auth="Container" //auth有兩個值可選「Application」和「Container」
type="javax.sql.DataSource" //指定資源所屬的Java類的完整限定名
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
//這里僅為SqlServer2005的驅動類名
//如果為SqlServer2000:com.microsoft,sqlserver,jdbc,SQLServerDriver
url="jdbc:sqlserver://localhost:1433;DataBaseName=bookstore"
username="sa"
//登錄名
password="000000"
//登錄密碼
maxActive="100"
//指定在連接池中資料庫連接的最大數目,設為0表示無限制
maxIdle="30"
//指定在連接池中保留的空閑的資料庫連接的最大數目,設為0表示無限制
maxWait="1000"/>
//指定等待一個資料庫連接成為可用狀態的最大時間,以毫秒為單位,設為-1表示無限制。
2、solr使用配置好的數據源
db-data-config.xml文件配置如下內容:
<dataSource name="ds-1" jndiName="java:comp/env/jdbc/orcl" type="JdbcDataSource" batch-size="1000" />
注意:「jdbc/orcl」為已配置的數據源名。如果是在tomcat中配置的數據源則jndiName,需要在你配置的數據源名前加「java:comp/env」。在其他容器中配置不用加此內容。
3、solr直接配置資料庫連接
db-data-config.xml文件配置如下內容:
<dataSource name="ds-1" type="JdbcDataSource" driver="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@127.0.0.1:1521:orcl" user="aa" password="bb" />

注意:如果應用伺服器上未安裝oracle客戶端,連接資料庫服務url寫法如下:
jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.100)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=orcl)))
說明「192.168.1.100」資料庫伺服器ip,「orcl」資料庫oid

Ⅳ 如何solr結合mysql全文搜索

solr跟mysql是獨立的,

要通過solr來做全文索引,你就明確mysql那些表的欄位需要做搜索。
然後mysql的增刪改,你都必須在solr伺服器這邊做對應的請求。

最後通過調用solr的查詢介面,solr會返回相應的信息,包括mysql對應數據的id以及匹配的分詞信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

$options = array
(
'hostname' => 'localhost',
'login' => 'username',
'password' => 'password',
'port' => '8983',
);
$client = new SolrClient($options);

//添加
$doc = new SolrInputDocument();
$doc->addField('id', 1);
$doc->addField('content', '如何solr結合mysql全文搜索');
$updateResponse = $client->addDocument($doc);
$client->commit();
updateResponse->getResponse();

//查詢
$query = new SolrQuery();
$query->setQuery('*結合mysql*');
$query->setStart(0);
$query->setRows(50);
$query->addField('id');
$query_response = $client->query($query);
$response = $query_response->getResponse();

Ⅵ solr連接mysql資料庫怎麼用

<bool name="mutable">true</bool>
<str name="managedSchemaResourceName">managed-schema</str>
</schemaFactory> -->

<!-- <processor class="solr.">
<str name="defaultFieldType">strings</str>
<lst name="typeMapping">
<str name="valueClass">java.lang.Boolean</str>
<str name="fieldType">booleans</str>
</lst>
<lst name="typeMapping">
<str name="valueClass">java.util.Date</st

Ⅶ solr5.5.4怎麼將已經建立好的mysql資料庫導入到dataimport

新建一個數據表,這里我選擇的是mysql資料庫,具體如何安裝我這里就不說了,具體代碼如下。這里還需要mysql的驅動包,需要放入到/opt/tomcat6/lib目錄下,或者放到/opt/tomcat6/webapps/solr/WEB-INF/lib目錄下,自行去mysql官網下載jdbc驅動包,在數據導入的時候需要用到!

Ⅷ solr鏈接資料庫時顯示顯示no information avaliable怎麼辦

說明如下:
query是獲取全部數據的SQL(solr從sql中獲取那些數據),多列
deltaImportQuery是獲取增量數據時使用的SQL(資料庫新增數據追加到solr的數據),多列
deltaQuery是獲取pk的SQL(資料庫新增數據是,追加到solr的數據時的條件,根據id ,條件是最後一次獲取的時間,${dataimporter.last_index_time,最後獲取的時間})!

Ⅸ 如何保證solr跟資料庫的數據一致性

關系型資料庫有四個顯著的特徵,即安全性、完整性、並發性和監測性。資料庫的安全性就是要保證資料庫中數據的安全,防止未授權用戶隨意修改資料庫中的數據,確保數據的安全。在大多數資料庫管理系統中,主要是通過許可來保證資料庫的安全性。完整性是資料庫的一個重要特徵,也是保證資料庫中的數據切實有效、防止錯誤、實現商業規則的一種重要機制。在資料庫中,區別所保存的數據是無用的垃圾還是有價值的信息,主要是依據資料庫的完整性是否健全。在SQL Server 7.0中,數據的完整性是通過一系列邏輯來保障的,這些邏輯分為三個方面,即實體完整性、域完整性和參考完整性。對任何系統都可以這樣說,沒有監測,就沒有優化。這句話用在資料庫管理系統方面,也是切合實際的。只有通過對資料庫進行全面的性能監測,也才能發現影響系統性能的因素和瓶頸,才能針對瓶頸因素,採取切合實際策略,解決問題,提高系統的性能。

熱點內容
西門吹雪腳本 發布:2024-10-04 06:54:42 瀏覽:954
android電子相冊 發布:2024-10-04 06:49:41 瀏覽:998
phpapp第三方登錄介面 發布:2024-10-04 06:40:02 瀏覽:748
Lgc的演算法 發布:2024-10-04 06:35:23 瀏覽:857
華為安卓70彩蛋怎麼 發布:2024-10-04 06:17:32 瀏覽:438
谷歌和安卓什麼關系 發布:2024-10-04 06:16:57 瀏覽:380
伺服器形式的電腦 發布:2024-10-04 06:08:35 瀏覽:819
python中的for函數 發布:2024-10-04 05:51:33 瀏覽:308
裝linux系統的電腦 發布:2024-10-04 05:24:36 瀏覽:561
維修案例資料庫 發布:2024-10-04 05:23:02 瀏覽:982