當前位置:首頁 » 凈水方式 » solr過濾

solr過濾

發布時間: 2021-02-13 15:09:59

Ⅰ Solr 查詢速度優化

優化數據訪問
查詢性能低下的最基本的原因是訪問的數據太多,對於低效的查詢,可以從下面兩個步驟來分析:
(1)確認應用程序是否在檢索大量超過需要的行,這通常意味著訪問了太多的行,但有時候也有可能訪問了太多的列。
(2)確認MySQL伺服器層是否在分析大量超過需要的數據行。
一些典型的情況:
(1) 查詢不需要的記錄。這樣的查詢上應該加上LIMIT
(2) 多表關聯時返回了全部列。應該只取需要的列。
(3) 總是取出全部的列:SELECT *
(4) 重復查詢需要的數據。較好的解決方案是使用數據緩存。
確認MySQL只返回了需要的數據之後,接下來應該看看查詢是否掃描了過多的數據,最簡單的衡量查詢開銷的三個指標如下:
(1)響應時間
(2)掃描的行數
(3)返回的行數
響應時間=排隊時間+服務時間
理性情況下的掃描的行數和返回的行數應該是相等的。
訪問類型:MySQL有好幾種方式可以查找並返回一行結果,Explain的type列反應了訪問類型,訪問類型有全表掃描、索引掃描、范圍掃描、唯一索引查詢、常數引用等,這些速度從慢到塊,掃描的行數從大到小。Using Where表示MySQL將通過Where條件來篩選存儲引擎返回的數據。MySQL能夠以三種方式應用Where條件,從好到壞依次是:
(1)、在索引中使用Where來過濾數據,這是在存儲引擎層實現的
(2)、使用了索引覆蓋掃描(Extra列中Using index)來返回記錄,直接從索引中過濾不需要的記錄並返回命中的結果,這是在MySQL伺服器層實現的,但是無需回表查詢記錄。
(3)、從數據表中返回數據,然後過濾不滿足條件的記錄,這是在MySQL伺服器層實現的,MySQL需要先從資料庫中讀取記錄然後過濾。
如果一個查詢需要掃描大量的數據但是只返回少數的行,那麼通常可以嘗試下面的技巧去優化:
(1)、使用索引覆蓋掃描,即把所有需要的列都放到索引中,這樣存儲引擎無需回表獲取對應行就可以返回結果了。
(2)、該表庫表結構,使用單獨的匯總表
(3)、重寫整個復雜的查詢,讓MySQL優化器能夠以最優化的方式執行整個查詢。
重構查詢的方式
確定一個復雜查詢還是多個簡單查詢更加有效
切分查詢:
將一個完整的查詢分散到多次小查詢中(例如通過Limit)
查詢執行的基礎
MySQL執行查詢的過程:
(1)客戶端發送一條查詢給伺服器
(2)伺服器先檢查查詢緩存,如果命中了緩存,則立即返回存儲在緩存中的結果,否則進入下一個階段。
(3)伺服器端進行SQL解析、預處理,再由優化器生成對應的執行計劃
(4)將結果返回給客戶端。

Ⅱ solr查詢某個欄位最大值,如何實現

q:查詢的關鍵復字,此參數制最為重要,例如,q=id:1,默認為q=*:*,類似於sql中的where 1=1。
fq(filter query):過濾查詢,提供一個可選的篩選器查詢。返回在q查詢符合結果中同時符合的fq條件的查詢結果,例如:q=id:1&fq=sort:[1 TO 5]&fq=section:0,找關鍵字id為1 的,並且sort是1到5之間,section=0的。還能寫成fq=+sort[1 TO 5] +section:0]。性能方面的考慮:每個fq下面的記錄都會單獨緩存。可以考慮把經常在一起的fq條件用+方式寫。
sort:排序方式,例如id desc 表示按照 「id」 降序。id desc, price asc先按id降序,再按price升序。sum(x_f, y_f) desc按x_f和y_f欄位的和降序。
start,rows:分頁參數,類似於start,limite的功能。啥都不輸的話默認值分別為start=0,limit=10。

Ⅲ 求助:solr如何通過指定條件修改數據

修改主方法
public int saveContent(String enterpriseId, String enterpriseName, String lableType, String resouce, String pubDate,
String content) {

int state = 0;
LBHttpSolrServer server = SolrUtil.getSolrServer(ap.getEnterprisenewSolrUrl());
SolrQuery query = new SolrQuery();
query.set("q", "enterpriseId:" + enterpriseId);
try {
QueryResponse qr = server.query(query);
List<EnterpriseContentBean> contentList = qr.getBeans(EnterpriseContentBean.class);
// 設置需要保存的文章信息
for (EnterpriseContentBean bean : contentList) {
bean.setEnterpriseId(enterpriseId);
bean.setEnterpriseName(enterpriseName);
List<String> contents = new ArrayList<String>();
contents.add(content);
bean.setContent(contents);
bean.setPubDate(pubDate);
System.out.println("pubDate======>" + pubDate);
List<String> lableTypes = Arrays.asList(lableType.split(","));

bean.setLableType(lableTypes);
bean.setResource(resouce);
bean.setIsVisited_s("1");
}

server.addBeans(contentList);
server.commit();

} catch (SolrServerException e) {
state = 1;
System.out.println("修改solr數據報錯");
e.printStackTrace();
} catch (IOException e) {
state = 1;
System.out.println("修改solr數據報錯");
e.printStackTrace();
}
return state;

}

刪除主方法
public int deletContent(String enterpriseId) {

LBHttpSolrServer server = SolrUtil.getSolrServer(ap.getEnterprisenewSolrUrl());
int state = 0;
try {
server.deleteById(enterpriseId);
server.commit();

} catch (SolrServerException e) {
state = 1;
System.out.println("刪除solr數據報錯");
e.printStackTrace();
} catch (IOException e) {
state = 1;
System.out.println("刪除solr數據報錯");
e.printStackTrace();
}

return state;
}

solr工具類
package com.dinfo.boc.utils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.LBHttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import com.dinfo.boc.enterprise.bean.EnterpriseContentBean;
import com.dinfo.boc.enterprisenew.bean.SolrQueryResult;

/**
* 與Solr伺服器交互的工具類
* @author qiuyj
*
*/
public class SolrUtil {

/**
* 獲取與指定Solr地址的連接
* @param solrUrl
* @return
*/
public static LBHttpSolrServer getSolrServer(String solrUrl){
final int ONE_HUNDRED_MS = 10000000;

if(solrUrl == null || "".equals(solrUrl)){
throw new RuntimeException("Solr url can not be empty!");
}

LBHttpSolrServer solrServer = null;
try {
solrServer = new LBHttpSolrServer(solrUrl);
solrServer.setConnectionTimeout(ONE_HUNDRED_MS);
} catch (MalformedURLException e) {
e.printStackTrace();
} //SolrUtil.getSolrServer(solrUrl);

//solrServer.(100);
//solrServer.setMaxTotalConnections(100);

return solrServer;
}

/**
* 向指定的Solr地址添加一條數據
* @param solrUrl
* @param doc
* @throws Exception
*/
public static void add(String solrUrl, SolrInputDocument doc) throws Exception {
if(doc == null){
throw new RuntimeException("SolrInputDocument object can not be null!");
}

LBHttpSolrServer solr = getSolrServer(solrUrl);
solr.add(doc);
solr.commit();
}

/**
* 向指定的Solr地址用JavaBean添加一條數據
* @param solrUrl
* @param obj
* @throws Exception
*/
public static void add(String solrUrl, Object obj) throws Exception {
if(obj == null){
throw new RuntimeException("Object to be inserted can not be null!");
}

LBHttpSolrServer solr = getSolrServer(solrUrl);
solr.addBean(obj);
solr.commit();
}

/**
* 向指定Solr地址批量添加數據
* @param solrUrl
* @param docs
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static void addAll(String solrUrl, Collection<? extends Object> objs) throws Exception {
if(objs == null){
throw new RuntimeException("Object collection can not be null!");
}
if(objs.size() == 0){
return;
}

LBHttpSolrServer solr = getSolrServer(solrUrl);

if(objs.iterator().next() instanceof SolrInputDocument){
solr.add((Collection<SolrInputDocument>)objs);
} else {
solr.addBeans(objs);
}
solr.commit();
}

/**
* 根據給定的id,從solr中刪除對應信息
* @param solrUrl
* @param ids
*/
public static void deleteByIds(String solrUrl, String ... ids) throws Exception {
if(ids == null || ids.length == 0){
throw new RuntimeException("Ids can not be empty!");
}

LBHttpSolrServer solr = getSolrServer(solrUrl);
solr.deleteById(Arrays.asList(ids));
solr.commit();
}

public static void deleteByIds(String solrUrl, Integer ... ids) throws Exception {
if(ids == null || ids.length == 0){
throw new RuntimeException("Ids can not be empty!");
}

List<String> stringIdList = new ArrayList<>(ids.length);
for(Integer id : ids){
stringIdList.add("" + id);
}

LBHttpSolrServer solr = getSolrServer(solrUrl);
solr.deleteById(stringIdList);
solr.commit();
}

/**
* 刪除指定Solr路徑下符合指定查詢條件的數據
* @param solrUrl
* @param condition
* @throws Exception
*/
public static void deleteByCondition(String solrUrl, String condition) throws Exception {
if(condition == null || "".equals(condition)){
throw new RuntimeException("Condition can not be empty!");
}

LBHttpSolrServer solr = getSolrServer(solrUrl);
solr.deleteByQuery(condition);
solr.commit();
}

/**
* 刪除指定Solr路徑下的所有數據
* @param solrUrl
* @throws Exception
*/
public static void deleteAll(String solrUrl) throws Exception {
deleteByCondition(solrUrl, "*:*");
}

/**
* 根據 指定查詢條件從Solr中查詢數據,並以SolrDocument的List形式返回
* @param solrUrl
* @param query
* @return
* @throws Exception
*/
public static SolrDocumentList queryAndGetSolrDocumentList(String solrUrl, SolrQuery query) throws Exception {
if(query == null){
throw new RuntimeException("SolrQuery object can not be null!");
}

LBHttpSolrServer solr = getSolrServer(solrUrl);
QueryResponse resp = solr.query(query);
return resp.getResults();
}

/**
* 根據 指定查詢條件從Solr中查詢數據,並以QueryResponse形式返回
* @param solrUrl
* @param query
* @return
* @throws Exception
*/
public static QueryResponse queryAndGetSolrQueryResponse(String solrUrl, SolrQuery query) throws Exception {
if(query == null){
throw new RuntimeException("SolrQuery object can not be null!");
}

LBHttpSolrServer solr = getSolrServer(solrUrl);
QueryResponse resp = solr.query(query);
return resp;
}

/**
* 根據 指定查詢條件從Solr中查詢數據,並以Java Bean的List形式返回
* @param solrUrl
* @param query
* @param returnClass 返回的List集合的泛型
* @return
* @throws Exception
*/
public static <T> List<T> queryAndGetBeanList(String solrUrl, SolrQuery query, Class<T> returnClass) throws Exception {
if(query == null){
throw new RuntimeException("SolrQuery object can not be null!");
}
if(returnClass == null){
throw new RuntimeException("Return class can not be null!");
}

LBHttpSolrServer solr = getSolrServer(solrUrl);
QueryResponse resp = solr.query(query);
return resp.getBeans(returnClass);
}

/**
* 根據 指定查詢條件從Solr中查詢數據,並以SolrQueryResult對象的形式返回,其中包含List對象和totalCount
* @param solrUrl
* @param query
* @param returnClass 返回的List集合的泛型
* @return
* @throws Exception
*/
public static <T> SolrQueryResult<T> queryAndGetSolrQueryResult(String solrUrl, SolrQuery query, Class<T> returnClass) throws Exception {
SolrQueryResult<T> result = new SolrQueryResult<T>();

if(query == null){
throw new RuntimeException("SolrQuery object can not be null!");
}
if(returnClass == null){
throw new RuntimeException("Return class can not be null!");
}

LBHttpSolrServer solr = getSolrServer(solrUrl);
solr.setConnectionTimeout(10000);
QueryResponse resp = solr.query(query);
List<T> resultList = resp.getBeans(returnClass);
long totalCount = resp.getResults().getNumFound();

result.setResultList(resultList);
result.setTotalCount(totalCount);

return result;
}

/**
* 根據 指定查詢條件從Solr中查詢數據,並以SolrQueryResult對象的形式返回,其中包含List對象和totalCount
* @param solrUrl
* @param query
* @param returnClass 返回的List集合的泛型
* @return
* @throws Exception
*/
public static <T> SolrQueryResult<T> queryAndGetSolrQueryResult(LBHttpSolrServer solr, SolrQuery query, Class<T> returnClass) throws Exception {
SolrQueryResult<T> result = new SolrQueryResult<T>();

if(query == null){
throw new RuntimeException("SolrQuery object can not be null!");
}
if(returnClass == null){
throw new RuntimeException("Return class can not be null!");
}

QueryResponse resp = solr.query(query);
List<T> resultList = resp.getBeans(returnClass);
long totalCount = resp.getResults().getNumFound();

result.setResultList(resultList);
result.setTotalCount(totalCount);

return result;
}

/**
* 用以過濾一些影響Solr查詢的特殊字元,如左右括弧、星號等
* @param str
* @return
*/
public static String filterSpecialCharacters(String str){
if(str == null){
return str;
}
str = str.replace("(", "\\(");
str = str.replace(")", "\\)");
str = str.replace("*", "\\*");
return str;
}

public static void updateSolrById(LBHttpSolrServer server){
SolrQuery query = new SolrQuery();
String id="";
int state=0;
String name="新疆金風科技股份有限公司";
query.set("q", "enterpriseId:"+id);
try {
QueryResponse qr = server.query(query);
List<EnterpriseContentBean> contentList = qr.getBeans(EnterpriseContentBean.class);
//設置需要保存的文章信息
for(EnterpriseContentBean bean:contentList){
// bean.setEnterpriseId(enterpriseId);
bean.setEnterpriseName(name);
bean.setResource("東方財富網港股頻道");
}

server.addBeans(contentList);
server.commit();

} catch (SolrServerException e) {
state = 1;
e.printStackTrace();
} catch (IOException e) {
state = 1;
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
=new LBHttpSolrServer("http://115.182.226.165:8008/solr/enterprisenew");
enterpriseServer.setConnectionTimeout(10000000);
updateSolrById(enterpriseServer);
System.out.println("over");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

Ⅳ solr中先檢索fieldname,然後再在結果中檢索關鍵字

去查詢filedname? filedname都是定義好的。 沒明白你的問題。
你可以直接寫AND,比如 filename1:亞洲 AND filename2:XX

Ⅳ solr 名稱 關鍵字兩個欄位權重設置

大家都知道,在SQL腳本中設置多欄位做關鍵字相對比較簡單,例:primary key(id1,id2) ,但用腳本建資料庫就比較麻煩了專。
下面我們來介紹屬一下具體的解決方法:
1.把要設置為關鍵字的其中一個欄位設為主鍵。
2.在設為主鍵的欄位上右鍵單擊選擇索引/健,出現索引/健對話框。
3.找到常規----列,單擊右邊的小按鈕,出現索引列對話框。
4.至此,我們就可以選擇幾個欄位作為關鍵字了。
注釋:第一個步驟不能省略。

Ⅵ solr不能過濾負數嗎,為什麼12345都可以,但是fq=id:-1就報錯呢

Esoir不能過濾負數嗎為什麼12345都可以但是fg=ID冒號減一就報錯呢這是演算法算錯了

Ⅶ 如何理解solr的core和collection

/****多核查詢測試*@throwsException*/()throwsException{//查詢a和b下面的數據,HttpSolrClientsc=newHttpSolrClient("a");Stringshards="192.168.1.215:8983/solr/a,192.168.1.214:8983/solr/b";=newModifiableSolrParams();solrParams.set("q","sname:北京奇虎科技有限公司");//solrParams.set("q.op","AND");//設置查詢關系.set("fl","*,score");//設置過濾solrParams.set("shards",shards);//設置shardQueryResponsersp=sc.query(solrParams);system.out.println("命中數量:"+rsp.getResults().getNumFound());for(SolrDocumentsd:rsp.getResults()){System.out.println(sd);}sc.close();}

Ⅷ 如何監控Solr

1、將解壓包中的solr-4.7.1/dist/solr-4.7.1.war復制到tomcat_dir/webapps/目錄,並命名為solr.war。

2、將solr-4.7.1/example/lib/ext/目錄下的jar文件復制到tomcat/lib目錄下,將solr-4.7.1/example/resources/下的log4j.properties文件復制到tomcat_dir/lib目錄下;
切把 solr-4.7.1/example/solr,復制到tomcat_dir/bin下。

3、修改tomcat_dir/conf/server.xml
<Connector port="8080" protocol="HTTP/1.1"

connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8" />

4、創建solr.xml,存放在路徑:tomcat/conf/Catalina/localhost/solr.xml,內容:
<Context path="/solr" docBase="C:\Tomcat 7.0\webapps\solr.war"
debug="0" crossContext="true">
<Environment name="solr/home" type="java.lang.String" value="C:\Tomcat 7.0\bin\solr\" override="true" />
</Context>

PS:上面的docBase和value路徑中不能存在中文字元,否則會出現404錯誤。

5、將C:\Tomcat 7.0\webapps下的solr.war包,啟動項目解壓;然後再添加幾個jar包:
solr-4.7.1\dist\solr-dataimporthandler-4.7.1.jar;
solr-4.7.1\dist\solr-dataimporthandler-extras-4.7.1.jar;
還要載入資料庫驅動包:mysql-connector-java-3.1.13-bin.jar

6、在C:\Tomcat 7.0\bin\solr\collection1\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>

7、將tomcat\bin\solr\collection1\conf下增加data-config.xml文件,內容如下:
<dataConfig>

<dataSource type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://192.168.1.221:3306/tmsdb"
user="root"
password="123456"/>
<document name="content">
<entity name="node" query="select id,author,title,content from solrdb">
<field column="id" name="id" />
<field column="author" name="author" />
<field column="title" name="title" />
<field column="content" name="content" />
</entity>
</document>
</dataConfig>

8、增加中文分詞器,ik-analyzer的配置如下:
①目前的中文分詞主要有兩種
1,基於中科院ICTCLAS的隱式馬爾科夫hhmm演算法的中文分詞器,例如smartcn等。(不支持自定義擴展詞庫)
2,基於正向迭代最細粒度切分演算法(正向最大匹配並且最細分詞)例如IK,庖丁等(支持自定義擴展詞庫)
安裝分詞前,可以去下載IK的分詞包 :
IK-Analyzer-4.7.1-0.0.1-SNAPSHOT.jar

下載完畢後,將此包放進tomcat\solr的\WEB-INF\lib下面:tomcat\webapps\solr\WEB-INF\lib 。
下面需要在solr的schemal.xml進行分詞器注冊:
<!-- 配置IK分詞器 -->

<fieldType name="text_ik" class="solr.TextField" positionIncrementGap="100">
<analyzer type="index">
<!-- 分詞-->
<tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory"/>
<!-- 禁用詞過濾根據情況使用-->
<!-- <filter class="org.wltea.analyzer.lucene.IKStopFilterFactory"/> -->
</analyzer>
<analyzer type="query">
<!-- 分詞-->
<tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory"/>
<!-- 禁用詞過濾根據情況使用-->
<!-- <filter class="org.wltea.analyzer.lucene.IKStopFilterFactory"/>-->
</analyzer>
</fieldType>

最後還得配置一個引用欄位就OK了
<field name="ik" type="text_ik" indexed="true" stored="true" multiValued="true"/>

②它的安裝部署十分簡單,將IKAnalyzer2012.jar部署亍項目的lib目錄中;IKAnalyzer.cfg.xml不stopword.dic文件放置在class根目錄(對於web項目,通常是WEB-I NF/classes目彔,同hibernate、log4j等配置文件相同)下即可 ;然後配置solr4.7中schema.xml配置解析器:
<schema name="example" version="1.1">

……
<fieldType name="text" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
……
</schema>

Ⅸ solrj如何自動定期更新索引,管理頁面如何對用戶屏蔽 http://localhost:8080/solr/admin

你會編程么? 可以採用過濾器 當請求用戶角色為普通用戶時 提示操作或者轉向鏈接

熱點內容
丁度巴拉斯情人電影推薦 發布:2024-08-19 09:13:07 瀏覽:886
類似深水的露點電影 發布:2024-08-19 09:10:12 瀏覽:80
《消失的眼角膜》2電影 發布:2024-08-19 08:34:43 瀏覽:878
私人影院什麼電影好看 發布:2024-08-19 08:33:32 瀏覽:593
干 B 發布:2024-08-19 08:30:21 瀏覽:910
夜晚看片網站 發布:2024-08-19 08:20:59 瀏覽:440
台灣男同電影《越界》 發布:2024-08-19 08:04:35 瀏覽:290
看電影選座位追女孩 發布:2024-08-19 07:54:42 瀏覽:975
日本a級愛情 發布:2024-08-19 07:30:38 瀏覽:832
生活中的瑪麗類似電影 發布:2024-08-19 07:26:46 瀏覽:239