6.solr學習速成之multicore查詢


查詢關聯多個core

再新建一個core

向每個core添加索引,修改

final static String SOLR_URL = "http://localhost:8080/solr/test";

即可

 

多個core的關聯查詢

package com.liucheng.solr;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
public class solrServer {
    private solrServer(){};
    final static String SOLR_URL = "http://localhost:8080/solr/core1";
    private static HttpSolrClient server = null;
    public static HttpSolrClient getServer(){
        if(server == null){
            server = new HttpSolrClient(SOLR_URL);
            server.setDefaultMaxConnectionsPerHost(1000);
            server.setMaxTotalConnections(10000);
            server.setConnectionTimeout(60000);
            server.setSoTimeout(60000);
            server.setFollowRedirects(false);
            server.setAllowCompression(true);
        }
        return server;
    }
}
package com.liucheng.solr;
import java.io.IOException;
import java.util.List;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
public class solrTest {
    public static void addIndex(){
        HttpSolrClient server = solrServer.getServer();
        student stu = new student();
        stu.setId("1006");
        stu.setName_s("wanglc6");
        stu.setScore_i(885);
        try {
            server.addBean(stu);
            server.commit();
        } catch (SolrServerException | IOException e) {
            e.printStackTrace();
        }
    }
    public static void delete(){
        HttpSolrClient server = solrServer.getServer();
        try {
            server.deleteById("1001");
            server.commit();
        } catch (SolrServerException | IOException e) {
            e.printStackTrace();
        }
    }
    public static void search(){
        HttpSolrClient server = solrServer.getServer();
        SolrQuery query = new SolrQuery();
        //query.setQuery("*:*");
        query.set("q", "*:*");
        query.setStart(0);
        query.setRows(5);
        query.setSort("score_i",ORDER.desc);
        query.set("shards", "http://localhost:8080/solr/core1,http://localhost:8080/solr/test");
        QueryResponse queryResponse;
        try {
            queryResponse = server.query(query);
            List<student> list = queryResponse.getBeans(student.class);
            System.out.println("num = "+list.size());
            for(int i=0;i<list.size();i++){
                System.out.println(list.get(i).getId() + "___" +list.get(i).getName_s() + "___" +list.get(i).getName_s() + "___" + list.get(i).getScore_i());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        //addIndex();
        //delete();
        search();
    }
}
package com.liucheng.solr;
import java.io.Serializable;
import org.apache.solr.client.solrj.beans.Field;
public class student implements Serializable{
    private static final long serialVersionUID = 1L;
    @Field
    private String id;
    @Field
    private String name_s;
    @Field
    private int score_i;
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName_s() {
        return name_s;
    }
    public void setName_s(String name_s) {
        this.name_s = name_s;
    }
    public int getScore_i() {
        return score_i;
    }
    public void setScore_i(int score_i) {
        this.score_i = score_i;
    }
}
測試的結果:
會綜合兩個core中的數據,如果有重復的id,只會取其中一個
但是會有一個問題,當兩個core中主鍵相同,但是內容不同的時候,他會怎么取值,測試結果有點隨機, 兩個值不時的更換,實際業務中不會出現這種不同值的情況吧!

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM