solr5的基本操作

来源:互联网 发布:ubuntu 16.04 wine 编辑:程序博客网 时间:2024/06/15 19:04
摘要
solr5的界面的基本操作 solr5使用solrj进行java操作

Solr5操作界面操作

Document操作索引

 

Query查询索引

 

Java操作

创建maven项目,在maven项目中导入依赖

<dependency>

<groupId>org.apache.solr</groupId>

<artifactId>solr-solrj</artifactId>

<version>4.10.1</version>

</dependency>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.11</version>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-log4j12</artifactId>

<version>1.7.7</version>

</dependency>

<dependency>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId>

<version>1.1.3</version>

</dependency>

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-lang3</artifactId>

<version>3.3.2</version>

</dependency>

 

/**

 * 使用document来插入index

 * @throws  SolrServerException

 * @throws  IOException

 */

@Test

public void addIndexBydocuments() throws SolrServerException, IOException{

HttpSolrServer server = new HttpSolrServer("http://127.0.0.1:8983/solr/core_one");

 

//使用document来操作solr

SolrInputDocument solrInputDocument = new SolrInputDocument();

 

//设置记录的每个必须属性,非必须的属性按需设置

solrInputDocument.addField("id""3");

solrInputDocument.addField("title""华为手机低价处理");

solrInputDocument.addField("price", 1255);

solrInputDocument.addField("name""华为手机");

 

server.add(solrInputDocument); //使用document添加记录

server.commit(); //提交操作  

}

 

使用bean类直接操作index

public class Item {

@Field

private String id;

@Field

private String name;

@Field

private String title;

@Field

private long price;

}

 

注:在shema中存在的field必须加上@Field注解,否则会报错

 

/**

 * 使用bean对象添加index

 * @throws Exception

 */

@Test

public void addIndexByBean() throws Exception{

HttpSolrServer server = new HttpSolrServer("http://127.0.0.1:8983/solr/wxdlCore");

//封装bean对象

Item item = new Item();

item.setId("6");

item.setName("小米手机");

item.setPrice(999l);

item.setTitle("小米手机热销中");

//根据bean添加索引

server.addBean(item);

server.commit();

}

 

 

/**

 * 根据id删除index

 * @throws Exception

 */

@Test

public void delIndexById() throws Exception{

HttpSolrServer server = new HttpSolrServer("http://127.0.0.1:8983/solr/wxdlCore");

 

server.deleteById("change.me");

server.commit();

}

 

/**

 * 根据表达式查询结果删除

 * @throws Exception

 */

@Test

public void delIndexByExpression() throws Exception{

HttpSolrServer server = new HttpSolrServer("http://127.0.0.1:8983/solr/wxdlCore");

server.deleteByQuery("title:小米*");

server.commit();

}

deleteByQuery(query) 此处的query是一个solrQuery,对应query界面中的q :

 

 

/**

 * 对index的修改就是对已存在的index进行添加操作,主键不修改

 * @throws SolrServerException

 * @throws IOException

 */

@Test

public void updateIndex() throws IOException, SolrServerException{

 

Item item = new Item();

 

item.setId("6");

 

item.setTitle("小米2 火热来袭 低价热销");

 

item.setName("小米2");

 

server.addBean(item);

 

server.commit();

}

 

 

/**

 * 使用document获取查询结果

 * @throws Exception

 */

@Test

public void getIndexByDocument() throws Exception{

 

SolrQuery solrQuery = new SolrQuery("title:*");

 

QueryResponse query = server.query(solrQuery);

 

SolrDocumentList results = query.getResults();

 

for (SolrDocument solrDocument : results) {

System.out.println(solrDocument.getFieldValue("name")+"....."+solrDocument.getFieldValue("title"));

}

 

}

 

 

/**

 * 使用bean来直接获取结果

 * @throws Exception

 */

@Test

public void selectIndexs() throws Exception{

 

SolrQuery query = new SolrQuery("title:*");

 

query.setRows(10);

 

query.setFields("name" , "title");

 

QueryResponse query2 = server.query(query);

 

List<Item> beans = query2.getBeans(Item.class);

 

for (Item item : beans) {

System.out.println(item.getName()+"......."+item.getTitle());

}

 

}

怎么样同时查title和content ?

a)采用 <copyField> ,将title和content设置到text的field中

b)根据服务器 solrconfig.xml 配置 df 默认查询字段进行查询

  <requestHandler name="/select" class="solr.SearchHandler">

     <lst name="defaults">

       <str name="echoParams">explicit</str>

       <int name="rows">10</int>

       <str name="df">text</str>

     </lst>

  </requestHandler>

 注:不推荐用name来设置进copyfield中,查询的结果会空

 

/**

 * 高亮显示

 * @throws Exception

 */

@Test

public void  getHightlighter() throws Exception{

SolrQuery query = new SolrQuery("title:手机");

//设置高亮相关配置

query.setHighlight(true); //开启高亮

query.addHighlightField("title"); //设置需要高亮的字段,可设置多个

query.addHighlightField("content"); //设置需要高亮的字段,可设置多个

query.addHighlightField("name"); //设置需要高亮的字段,可设置多个

query.setHighlightFragsize(100); //设置摘要的长度

query.setHighlightSimplePre("<font color='red'>"); //设置高亮前缀

query.setHighlightSimplePost("</font>"); //设置高亮后缀

//------------

QueryResponse response = server.query(query); //查询结果

Map<String, Map<String, List<String>>> highlighting = response.getHighlighting(); //高亮内容集合

System.out.println(highlighting);

Set<String> keySet = highlighting.keySet(); //获取的是主键的set集合

//遍历获取所有的高亮内容

for (String string : keySet) {

Map<String, List<String>> map = highlighting.get(string);

Set<String> fieldKeys = map.keySet();

for (String string2 : fieldKeys) {

List<String> list = map.get(string2);

for (String string3 : list) {

System.out.println(string3);

}

}

}

}

 

结果:

{3={name=[华为<font color='red'>手机</font>], title=[华为<font color='red'>手机</font>低价处理], content=[华为<font color='red'>手机</font>是中国知名品牌的<font color='red'>手机</font>产品]}, 6={name=[小米<font color='red'>手机</font>], title=[小米<font color='red'>手机</font>热销中], content=[小米<font color='red'>手机</font>是<font color='red'>手机</font>界的新起之秀]}}

华为<font color='red'>手机</font>

华为<font color='red'>手机</font>低价处理

华为<font color='red'>手机</font>是中国知名品牌的<font color='red'>手机</font>产品

小米<font color='red'>手机</font>

小米<font color='red'>手机</font>热销中

小米<font color='red'>手机</font>是<font color='red'>手机</font>界的新起之秀

0 0
原创粉丝点击