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

solr索引資料庫

發布時間: 2023-06-05 13:12:07

1. solr給資料庫做索引有什麼好處

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

2. solr的索引數據可以存放到資料庫嗎

在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就可以了。

3. 怎麼用java代碼操作solr對資料庫進行索引

SolrQuery solrQuery = new SolrQuery(); Map map = new HashMap(); map.put(FacetParams.FACET_DATE, "manufacturedate_dt"); map.put(FacetParams.FACET_DATE_START,"2004-01-01T00:00:00Z"); map.put(FacetParams.FACET_DATE_END,"2010-01-01...

熱點內容
scratch少兒編程課程 發布:2025-04-16 17:11:44 瀏覽:627
榮耀x10從哪裡設置密碼 發布:2025-04-16 17:11:43 瀏覽:356
java從入門到精通視頻 發布:2025-04-16 17:11:43 瀏覽:73
php微信介面教程 發布:2025-04-16 17:07:30 瀏覽:297
android實現陰影 發布:2025-04-16 16:50:08 瀏覽:787
粉筆直播課緩存 發布:2025-04-16 16:31:21 瀏覽:337
機頂盒都有什麼配置 發布:2025-04-16 16:24:37 瀏覽:202
編寫手游反編譯都需要學習什麼 發布:2025-04-16 16:19:36 瀏覽:800
proteus編譯文件位置 發布:2025-04-16 16:18:44 瀏覽:356
土壓縮的本質 發布:2025-04-16 16:13:21 瀏覽:582