Solr------solr搜索框架简介

一.简介

二.示意图

三.环境搭建

https://www.jianshu.com/p/573d54d53916

https://blog.csdn.net/weixin_42636552/article/details/86065381

 

四.在eclipse中搭建基础测试代码

1.导入jar包

2.测试代码

package com.kennosaur.solr;

import java.io.IOException;
import java.util.ArrayList;
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.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;

public class Testsolr1 {
	
	public Testsolr1() {
	}

	public static void main(String[] args) throws Exception {
		Testsolr1 testsolr1 = new Testsolr1();
		testsolr1.addDoc();
		
//		List<String> querySolr = testsolr1.querySolr("张");
//		System.out.println(querySolr.toString());
		
//		testsolr1.deleteDocumentById();
	}
	
	// 指定solr服务器的地址
	private final static String SOLR_URL = "http://localhost:8080/solr/";

	// 创建一个客户端
	public HttpSolrClient createSolrServer() {
		HttpSolrClient solr = null;
		solr = new HttpSolrClient.Builder(SOLR_URL).withConnectionTimeout(10000).withSocketTimeout(60000).build();
		return solr;
	}

	// 添加文档
	public void addDoc() throws SolrServerException, IOException {
		// 构造一篇文档
		SolrInputDocument document = new SolrInputDocument();
		// 往doc中添加字段,在客户端这边添加的字段必须在服务端中有过定义
		document.addField("id", "1001");
		document.addField("name", "solr搜索框架");
		// 获得一个solr服务端的请求,去提交 ,选择具体的某一个solr core
		HttpSolrClient solr = new HttpSolrClient.Builder(SOLR_URL + "mycore").withConnectionTimeout(10000)
				.withSocketTimeout(60000).build();		
		solr.add(document);
		solr.commit();
		solr.close();
	}

	// 删除Solr中新建立的索引
	public void deleteDocumentById() throws Exception { // 选择具体的某一个solr core
		HttpSolrClient server = new HttpSolrClient.Builder(SOLR_URL + "mycore").withConnectionTimeout(10000)
				.withSocketTimeout(60000).build();

		server.deleteById("4"); // 删除所有的索引
		// solr.deleteByQuery("*:*");
		// 提交修改
		server.commit();
		server.close();
	}

	// 查询
	public List<String> querySolr(String name) throws Exception {
		HttpSolrClient solrServer = new HttpSolrClient.Builder(SOLR_URL + "mycore/").withConnectionTimeout(10000)
				.withSocketTimeout(60000).build();
		SolrQuery query = new SolrQuery();
		List<String> list = new ArrayList<String>();
		// 设置查询条件
		// query.set("q", "*:*");
		// query.set("fl", "title");
		//query.set("q", "title:" + name + "OR author:" + name);
		query.set("q", "title:" + name );
		// 获取查询结果
		QueryResponse response = solrServer.query(query);
		SolrDocumentList sds = response.getResults();
		System.out.println("===sds.size()====="+sds.size()+"====sds.getNumFound()===="+sds.getNumFound());
		for (SolrDocument per : sds) {
			String artical_id = (String) per.get("id");
			list.add(artical_id);
		}
		return list;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值