残缺极品 发表于 2017-12-18 19:39:22

Solr简单测试

import org.apache.solr.client.solrj.SolrClient;  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.client.solrj.response.UpdateResponse;
  import org.apache.solr.common.SolrDocument;
  import org.apache.solr.common.SolrDocumentList;
  import org.apache.solr.common.SolrInputDocument;
  import org.junit.Test;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.List;

  public>  //solr 服务器地址
  public static final String solrServerUrl = "http://localhost:8983/solr";
  //solrhome下的core
  public static final String solrCroeHome = "mycore";
  public static String[] docs = {"Solr是一个独立的企业级搜索应用服务器",
  "它对外提供类似于Web-service的API接口",
  "用户可以通过http请求",
  "向搜索引擎服务器提交一定格式的XML文件生成索引",
  "也可以通过Http Get操作提出查找请求",
  "并得到XML格式的返回结果"};
  public static void main(String[] args) {
  System.out.println("solr insert test");
  //      SolrClient client = new HttpSolrClient(solrServerUrl+"/" + solrCroeHome);
  SolrClient client = new HttpSolrClient.Builder(solrServerUrl + "/" + solrCroeHome).build();
  List<SolrInputDocument> solrDocs = new ArrayList<>();
  for (int i = 0; i < docs.length; i++) {
  SolrInputDocument doc = new SolrInputDocument();
  doc.addField(&quot;id&quot;, i);
  doc.addField(&quot;content_text&quot;, docs);
  solrDocs.add(doc);
  }
  try {
  client.add(solrDocs);
  client.commit();
  System.out.println(&quot;Insert Done&quot;);
  } catch (SolrServerException e) {
  e.printStackTrace();
  } catch (IOException e) {
  e.printStackTrace();
  }finally {
  try {
  client.close();
  } catch (IOException e) {
  e.printStackTrace();
  }
  }
  }
  @Test
  public void testSolrQuery() throws IOException, SolrServerException {
  SolrClient client = new HttpSolrClient(solrServerUrl + &quot;/&quot; + solrCroeHome);
  SolrQuery query = new SolrQuery(&quot;content_text:类似&quot;);
  // 设置查询关键字
  /* query.setQuery(&quot;Content:* AND Spare3:1 &quot;); */
  // 指定查询返回字段
  /* query.setParam(&quot;fl&quot;, &quot;Content,IndexTime&quot;); */
  // 设置高亮

  //      query.setHighlight(true).setHighlightSimplePre(&quot;<span>  //                .setHighlightSimplePost(&quot;</span>&quot;);
  //      query.setParam(&quot;hl.fl&quot;, &quot;Content&quot;);//设置高亮字段
  //      query.setParam(&quot;fl&quot;, &quot;ID,Published&quot;);
  //排除条件      - NOT
  //wbQuery.addFilterQuery(&quot;OriginType:wb -Spare3:0&quot;);
  //wbQuery.addFilterQuery(&quot;OriginType:wb NOT Spare3:0&quot;);
  // 时间条件过滤
  /* query.addFilterQuery(&quot;Content:超哥&quot;); */
  /*
  * query.addFilterQuery(
  * &quot;Published:&quot;);
  **/
  // 实现分页的查询
  query.setStart(0);
  query.setRows(10);
  // 设定排序,如果需要对field进行排序就必须在schema.xml中对该field配置stored=&quot;true&quot;属性
  //set会清空原来的sort条件,add不会清空原来的,会在原来的基础上添加 sort=Published asc,Author asc(多条件排序)
  //      query.setSort(IContentCommon.IndexField.Published.getName(),
  //                SolrQuery.ORDER.asc);
  //
  //      query.addSort(IContentCommon.IndexField.Published.getName(),
  //                SolrQuery.ORDER.asc);
  QueryResponse response = client.query(query);
  System.out.println(query);
  SolrDocumentList docs = response.getResults();
  System.out.println(docs.getNumFound());
  for(SolrDocument doc : docs){

  Object>
  System.out.println(&quot;id:&quot; +>  }
  client.close();
  }
  @Test
  public void testSolrDelete() throws IOException, SolrServerException {
  SolrClient client = new HttpSolrClient.Builder(solrServerUrl + &quot;/&quot; + solrCroeHome)
  .build();
  UpdateResponse response = client.deleteById(&quot;0&quot;);
  int status = response.getStatus();
  System.out.println(&quot;status:&quot; + status);
  client.commit();
  client.close();
  }
  }
页: [1]
查看完整版本: Solr简单测试