Lucene is an open source full-text search engine provided by the Apache Software Foundation.Solr is an easy-to-use full-text search server that is packaged on top of lucene and provides related api.

This article uses solr3.4 as an example of use, which has been debugged. Later versions may require code changes.

1. Start solr. After unpacking the solr archive, there is a war package under solr_home\dist. One way is to deploy the war package into a web container. I'm here to save trouble, just use the built-in jetty. The startup method is to run Java –jar start.jar in the example directory. The background will show that the port 8983 is occupied by default. At this point, there should be a response to visiting http://localhost:8983/solr.

2. Enter data into solr. One way is to use the api (see code below), and another way is to use the command line to index some disk files. Such as executing in the exampledoc directory: java–jar post.jar *.xml.

The following code is to display data entry, query and other functions. The project needs to introduce the following jar packages: apache-solr-solrj-3.4.0.jar, commons-httpclient-3.1.jar, commons-codec-1.4.jar, slf4j-api-1.6.1.jar, commons-logging- 1.1.3.jar

import java.util.*;

import org.apache.solr.client.solrj.*;

import org.apache.solr.client.solrj.impl.*;

import org.apache.solr.client.solrj.response.*;

import org.apache.solr.common.*;

public class SolrTest {

    public static void main(String[] args)throws Exception{

        String url = "http://localhost:8983/solr";

SolrServer server = new CommonsHttpSolrServer(url);

        server.deleteByQuery( "*:*" );

        SolrInputDocument doc1 = new SolrInputDocument();

        doc1.addField( "id", "book1" );

doc1.addField( "name", "The Legend of Heaven and Dragon Slayer");

        doc1.addField( "price", 29 );

        SolrInputDocument doc2 = new SolrInputDocument();

        doc2.addField( "id", "book2" );

doc2.addField( "name", "The Condor Heroes" );

        doc2.addField( "price", 20 );

        SolrInputDocument doc3 = new SolrInputDocument();

        doc3.addField( "id", "book3");

doc3.addField( "name", "Dragon Babu");

        doc3.addField( "price", 33.8 );

        SolrInputDocument doc4 = new SolrInputDocument();

        doc4.addField( "id", "book4" );

doc4.addField( "name", "The Legend of the Condor Heroes");

        doc4.addField( "price", 25.5 );

        List<SolrInputDocument> docs = new  ArrayList<SolrInputDocument>();







        SolrQuery query = new SolrQuery();

        query.setQuery( "name:" );



        query.addSortField( "price", SolrQuery.ORDER.desc );



        query.setHighlightSimplePre("<font color=\"red\">");





        query.set("hl.fragsize", 5);

        QueryResponse rsp = server.query( query );


        SolrDocumentList docsList = rsp.getResults();

//Get the highlighted result

        Map<String,Map<String,List<String>>> hl = rsp.getHighlighting();

        for(Iterator<SolrDocument> doc =docsList.iterator();doc.hasNext();){

SolrDocument d =;






Related articles

Use CobaltStrike

CobaltStrike is a penetration testing tool, known by the industry people as CS gods. cobaltStrike is divided into client and server, the server is one, the client can have more than one, can be distributed by the team to collaborate group operations. Coba

Use force index to force the use of indexes

1. When testing a query according to the time range, although the index is added, it is found that the index cannot be used. You can use this to force the use of the index The test process is to create the following table and create a joint index create

Install Cobbler

Cobbler is a Linux system installation and configuration software surroundings [root@56-201 ~]# hostnamectl Static hostname: 56-201 Icon name: computer-vm Chassis: vm

CPU Load and CPU Utilization

Insufficient system resources such as storage, memory, and CPU (central processing unit) can significantly impact application performance. Therefore, it is critical to monitor these components.