侧边栏壁纸
  • 累计撰写 781 篇文章
  • 累计创建 1 个标签
  • 累计收到 1 条评论
标签搜索

java 里使用

Dettan
2021-04-10 / 0 评论 / 0 点赞 / 110 阅读 / 4,514 字
温馨提示:
本文最后更新于 2022-04-30,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
上面的官方文档里的Class HttpSolrClient.Builder 有例子

HTTPClient 版本要用4.5.6
一定要指定 core
普通查询 高亮查询
package com.fz.util;

import org.apache.solr.client.solrj.SolrQuery;
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 solrTest {
public static void fun() throws Exception {
//创建Solr服务的对象
HttpSolrClient ss = new HttpSolrClient.Builder("http://localhost:8983/solr/new_core").build();

    //创建Solr的文档对象
    SolrInputDocument sid = new SolrInputDocument();
    //向solr文档对象中添加域以及它的值
    sid.setField("id", "2");
    sid.setField("item_title", "三星手机");
    sid.setField("item_sell_point", "三星s8");

    //提交
    ss.add(sid);
    ss.commit();
}

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

// fun();
fun1();
}

public static void fun1() throws Exception {
    //创建Solr服务的对象

    HttpSolrClient ss = new HttpSolrClient.Builder("http://localhost:8983/solr/new_core").build();

    //创建查询对象
			//SolrQuery sq = new SolrQuery("*liu*");
    SolrQuery sq = new SolrQuery();
    sq.setQuery("手机");
    //设置默认搜索域
    sq.add("df", "item_keywords");
			//开启高亮
    sq.add("hl", "on");
    sq.add("hl.fl", "item_title");
    sq.add("hl.simple.pre", "<span>");
    sq.add("hl.simple.post", "</span>");
    QueryResponse response = ss.query(sq);
    SolrDocumentList res = response.getResults();
    for (SolrDocument sd : res) {
        System.out.println("id=" + sd.get("id"));
        System.out.println("标题=" + sd.get("item_title"));
        System.out.println("卖点=" + sd.get("item_sell_point"));
        System.out.println("高亮=" + response.getHighlighting().get(sd.get("id")).get("item_title"));
    }
}

}




<bean id="solrClientBuilder" class="org.apache.solr.client.solrj.impl.HttpSolrClient.Builder">
    <constructor-arg value="http://localhost:8983/solr/new_core"/>
</bean>

<bean id="solrClient" factory-method="build" factory-bean="solrClientBuilder"/>




{
  "responseHeader":{
    "status":0,
    "QTime":36,
    "params":{
      "q":"手机",
      "df":"item_keywords",
      "hl":"on",
      "hl.simple.post":"</span>",
      "hl.fl":"item_title",
      "hl.simple.pre":"<span>",
      "_":"1574129963073"}},
  "response":{"numFound":2,"start":0,"docs":[
      {
        "id":"1",
        "item_title":"三星手机",
        "item_sell_point":"三星note",
        "_version_":1650594667394236416},
      {
        "id":"2",
        "item_title":"三星手机",
        "item_sell_point":"三星s8",
        "_version_":1650594978934554624}]
  },
  "highlighting":{
    "1":{
      "item_title":["三星<span>手机</span>"]},
    "2":{
      "item_title":["三星<span>手机</span>"]}}}


删除document
通过id删除某个document:
public static boolean deleteById(String id){
	  try {
	      solrClient.deleteById(id);
	      solrClient.commit();
	  } catch (Exception e) {
	      e.printStackTrace();
	      return false;
	  }
	  return true;
}
添加单个document
通过SolrInputDocument向solr添加单个document,记得一定要commit:
public boolean newQuestionIndex(int qid, String title, String content){
    SolrInputDocument document = new SolrInputDocument();
    document.setField("id", String.valueOf(qid));
    document.setField(QUESTION_TITLE_FIELD, title);
    document.setField(QUESTION_CONTENT_FIELD, content);
    try {
        UpdateResponse response = solrClient.add(document);
        int status = response.getStatus();
        if(status == 0){
            logger.info("添加问题:“"+title+"”成功 !");
            solrClient.commit();
        }else {
            logger.info("添加问题:“"+title+"”失败 !");
        }
        return response != null && status == 0;
    } catch (Exception e) {
        logger.error("向solr添加document时出错:"+e.getMessage());
        e.printStackTrace();
        return false;
    }
}

0

评论区