Summary of Solr's Study I

Heart Word: Because relational databases have always been in contact with before, all of these nosql s do not fit in very well.It is often used in companies.This is not difficult to learn, but for future review.I decided to write down some of the skills and experiences I learned

Environment Configuration for Solr

This is a lot of online stuff. There's not much rubbish here
https://www.cnblogs.com/lengzhijun/p/4281363.html

Query parameters

  1. Basic Query

  2. Solr's Retrieval Operator

    Of course these conceptual things must be seen solr query parameters

Application of solr in Java

Of course, this is the focus, because in the actual development we only need to use, as for the other can slowly learn

  • solr connection
 //Specify solr server address
    private final static String SOLR_URL = "http://172.16.0.104:13087/solr/fuzzyQySRCdoc_shard12_replica2";

    //Create solrClient object
    public static HttpSolrClient solrClient() {
        HttpSolrClient solrClient = new HttpSolrClient(SOLR_URL);
        return solrClient;
    }
  • By specifying a field query and returning as JsonArray
public JSONArray solrInfo(List<String> readIDs ) throws IOException, SolrServerException { 
        JSONArray listInfo = new JSONArray();
        //Get solr connection
        HttpSolrClient solrClient = EntityListInfo.solrClient();
        SolrQuery solrQuery = new SolrQuery();
        //Traverse ids collection
        for (String id : readIDs) {
            solrQuery.set("q", "entitys_id:" + "\"" + id + "\"");
            QueryResponse response = solrClient.query(solrQuery);
            SolrDocumentList documentList = response.getResults();
            for (SolrDocument document : documentList) {
            //Convert document to json
                Set<Map.Entry<String, Object>> entrySet = document.entrySet();
                JSONObject object = new JSONObject();
                for (Map.Entry<String, Object> objectEntry : entrySet) {
                    if (objectEntry.getKey().equals("_version")) {
                        continue;
                    }
                    object.put(objectEntry.getKey(), objectEntry.getValue());
                }
                listInfo.add(object);
                //System.out.println(document.toString().replace("SolrDocument",""));
                break;
            }
        }
        return listInfo;
    }

Carefully, you can see that the traversal of a document is very similar to that of a map, and less familiar can be Four ways to traverse a map

  • solr paging query and save the resulting information in text
//Find solr's doc_by IDID
    public void solrDocId() throws IOException, SolrServerException {
    //Get all the IDS you need
        List<String> readIDs = readIDs();
        HttpSolrClient solrClient = EntityListInfo.solrClient();
        SolrQuery solrQuery = new SolrQuery();
        for (String id : readIDs) {
            System.out.println(id);
            long start = 0;
            long rows = 500;
            while (true) {
                try {
                    ModifiableSolrParams params = new ModifiableSolrParams();
                    params.add("q", "*:*");
                    params.add("fq", "entity_ids:" + "\"" + id + "\"");
                    params.add("start", String.valueOf(start));
                    params.add("rows", String.valueOf(rows));
                    QueryResponse queryResponse = solrClient.query(params);
                    if (queryResponse == null) {
                        continue;
                    }
                    SolrDocumentList solrDocuments = queryResponse.getResults();
                //Get the total number, which can be viewed in solr admin
                    long docNumbers = solrDocuments.getNumFound();
                    if (solrDocuments.isEmpty()) {
                        break;
                    }
                    // Insert solr data into text
                    for (SolrDocument document : solrDocuments) {
                        Set<Map.Entry<String, Object>> entrySet = document.entrySet();
                        JSONObject object = new JSONObject();
                        for (Map.Entry<String, Object> objectEntry : entrySet) {
                            if (objectEntry.getKey().equals("_version")) {
                                continue;
                            }
                            object.put(objectEntry.getKey(), objectEntry.getValue());
                        }
                        writeInfo("F:\\info\\doc_id.txt", object);
                        System.out.println(document.get("doc_id"));
                        //listInfo.add(object);                       
                    }
                    //Determine conditions for end of paging
                    if (start + rows >= docNumbers) {
                        break;
                    } else {
                        start = start + rows;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return;
    }

Summary: Additions and deletions like solr are not mentioned here and can be referred to SOLR CRUD Operation
Of course, the best thing is to have a general understanding of the rookies

Tags: solr Java JSON less

Posted on Fri, 22 May 2020 12:19:56 -0400 by potatoCode