Start Apache Solr with Embedded.

background

I'm using Apache solr for work, but I was hoping that it could be easily moved at home (local), but I recently learned that it can be launched with Embedded, so I'll make a note of it.

What is Apache solr

A full-text search engine library provided by Apache. It features a rich library even in open source with a management screen, hit highlights, facet search, cache, and duplication.

Things to prepare

-Solr.xml --solr general settings. Since it was supposed to be run locally, I set "localhost" for host.  https://lucene.apache.org/solr/guide/6_6/format-of-solr-xml.html -Solrj --A library for issuing queries to solr on java (see "pom.xml"). -Solr-core --A library for solr core information (see "pom.xml"). -Core folder-A folder that stores the created core data (see "Core folder hierarchy").

pom.xml

	<!-- https://mvnrepository.com/artifact/org.apache.solr/solr-solrj -->
	<dependency>
	    <groupId>org.apache.solr</groupId>
	    <artifactId>solr-solrj</artifactId>
	    <version>7.7.1</version>
	</dependency>

	<dependency>
	    <groupId>org.apache.solr</groupId>
	    <artifactId>solr-core</artifactId>
	    <version>7.7.1</version>
	</dependency>

Core folder hierarchy

This time, I created the following folders under src / main / resources. src/main/resources ┗ solr folder     ┣ solr.xml ┗ test_core folder ┗ core.properties (The contents can be empty)

Source

embeddedSolr.java



	private static CoreContainer container;
	private static SolrClient client;
	
	//Directory for storing setting information
	private final static String solrFolder = "src/main/resources/solr";

	//solr start
	public static void init(String coreName) {

		//Read setting information
		container = new CoreContainer("src/main/resources/solr");
		container.load();

		try {
			//Specify the core and launch
			client = new EmbeddedSolrServer(container, coreName);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//Add data
	public static void add() {

		//List for addition
		List<SolrInputDocument> docList = new ArrayList<>();

		SolrInputDocument doc = new SolrInputDocument();
		doc.setField("id", "1");
		doc.addField("content_txt_ja", "Taro bought vegetables.");
		docList.add(doc);

		doc = new SolrInputDocument();
		doc.setField("id", "2");
		doc.addField("content_txt_ja", "Jiro sold the fruit.");
		docList.add(doc);

		doc = new SolrInputDocument();
		doc.setField("id", "3");
		doc.addField("content_txt_ja", "Saburo plans to buy bread.");
		docList.add(doc);

		doc = new SolrInputDocument();
		doc.setField("id", "4");
		doc.addField("content_txt_ja", "Goro doesn't buy anything.");
		docList.add(doc);

		try {
			//Add and commit data.
			client.add(docList);
			client.commit();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}

	//data search
	public static void select(String keyword) {

		//Query creation
		SolrQuery query = new SolrQuery();
		query.add("q", "content_txt_ja:\""+keyword+"\"");
		query.add("fl", "*,score");

		log.info("[Solr search] query[{}]", query.toString());

		//Search
		QueryResponse response = null;
		try {
			response = client.query(query);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}

		//Get search results
		SolrDocumentList list = response.getResults();
		for(SolrDocument doc : list) {
			String id = doc.getFieldValue("id").toString();
			String text = doc.getFieldValue("content_txt_ja").toString();
			String score = doc.getFieldValue("score").toString();
			log.info("[Acquisition result] id[{}], text[{}] score[{}]", id, text, score);
		}
	}
	
	//solr stop
	public static void stop() {
		container.shutdown();
	}

Execution result

As a result of searching for "buy", the following data was hit.

[Acquisition result] id[1], text[Taro bought vegetables.] score[0.38845786](EmbettedSolrInit.java:111)
[Acquisition result] id[3], text[Saburo plans to buy bread.] score[0.3472057](EmbettedSolrInit.java:111)
[Acquisition result] id[4], text[Goro buys sweets and goes home.] score[0.31387395](EmbettedSolrInit.java:111)

I succeeded in moving it for the time being, so I would like to make a note of Solr's functions while moving it in the future.

Recommended Posts

Start Apache Solr with Embedded.
[Linux] Start Apache container with Docker
Start k3s with docker-compose
Repeated sample with Apache Freemarker
CSV output with Apache Commons CSV
Access Apache Kafka with Micronaut
Get detailed results of morphological analysis with Apache Solr 7.6 + SolrJ
Manipulate Excel with Apache POI
Get detailed results of morphological analysis with Apache Solr 7.6 + SolrJ (Japanese)
Circuit Breaker Pattern with Apache Camel
Try implementing recaptcha with Jetty embedded.
Download large files with Apache JMeter
Easy Pub/Sub messaging with Apache Kafka
[SRE / Docker] Start control with Dockerize
mysql doesn't start up with docker.
Restart apache with docker php-apache image
Java, arrays to start with beginners
FileUpload with Rest on Apache Wicket
Container does not start with docker-compose
How to start Camunda with Docker