写点什么

全文检索工具 solr:第一章:理论知识

  • 2021 年 11 月 11 日
  • 本文字数:5913 字

    阅读完需:约 19 分钟

</dependency>


<dependency>


<groupId>junit</groupId>


<artifactId>junit</artifactId>


<version>4.9</version>


</dependency>


</dependencies>


(2)在 src/main/resources 下创建 ?applicationContext-solr.xml


<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"


xmlns:context="http://www.springframework.org/schema/context"


xmlns:solr="http://www.springframework.org/schema/data/solr"


xsi:schemaLocation="http://www.springframework.org/schema/data/solr


http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd


http://www.springframework.org/schema/beans


http://www.springframework.org/schema/beans/spring-beans.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context.xsd">


<solr:solr-server id="solrServer" url="http://127.0.0.1:8080/solr" />


<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">


<constructor-arg ref="solrServer" />


</bean>


</beans>

@Field 注解

属性使用 @Field(“域名”)注解标识 。 ??如果属性与配置文件定义的域名称不一致,需要在注解中指定域名称。


public class SkuInfo implements Serializable{


String id;


String spuId;


@Field("sku_price")


BigDecimal price;


@Field("sku_name")


String skuName;


BigDecimal weight;


@Field("sku_desc")


String skuDesc;


String catalog3Id;


@Field("sku_default_img")


String skuDefaultImg;


.......


}

增加(修改)

对于修改:如果 id 存在就认为是修改,若不存在,就认为是新增


创建测试类 TestTemplate.java


@RunWith(SpringJUnit4ClassRunner.class)


@ContextConfiguration(locations="classpath:applicationContext-solr.xml")


public class TestTemplate {


@Autowired


private SolrTemplate solrTemplate;


@Test


public void testAdd(){


SkuInfo skuInfo = new SkuInfo();


skuInfo.setId("1");


skuInfo.setSkuName("华为");


skuInfo.setPrice(new BigDecimal(3000.01));


skuInfo.setCatalog3Id("2");


skuInfo.setSkuDefaultImg("www.baidu,com");


skuInfo.setSkuDesc("非常好");


skuInfo.setSpuId("3");


solrTemplate.saveBean(skuInfo);


solrTemplate.commit();


}


}

按主键查询

<table border="1" cellspacing="0" style="width:426.1pt;"><tbody><tr><td style="vertical-align:top;width:426.1pt;"><p><span style="color:#1e1c11;"><span style="color:#646464;">@</span><span style="color:#646464;">Test</span></span></p><p><span style="color:#1e1c11;"><strong><span style="color:#7f0055;"><strong>public</strong></span></strong> <strong><span style="color:#7f0055;"><strong>void</strong></span></strong><span style="color:#000000;"> testFindOne(){</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;">   SkuInfo sku =  template.getById("1",SkuInfo.class);</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;">        System.out.printf(sku.getSkuName());</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;">}</span></span></p></td></tr></tbody></table>

按主键删除

<table border="1" cellspacing="0" style="width:426.1pt;"><tbody><tr><td style="vertical-align:top;width:426.1pt;"><p><span style="color:#1e1c11;"><span style="color:#646464;">@Test</span></span></p><p><span style="color:#1e1c11;"><strong><span style="color:#7f0055;"><strong>public</strong></span></strong> <strong><span style="color:#7f0055;"><strong>void</strong></span></strong><span style="color:#000000;"> testDelete(){</span></span></p><p><span style="color:#1e1c11;"><span style="color:#0000c0;">solrTemplate</span><span style="color:#000000;">.deleteById(</span><span style="color:#2a00ff;">"1"</span><span style="color:#000000;">);</span></span></p><p><span style="color:#1e1c11;"><span style="color:#0000c0;">solrTemplate</span><span style="color:#000000;">.commit();</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;">}</span></span></p></td></tr></tbody></table>

分页查询

首先循环插入 100 条测试数据


<table border="1" cellspacing="0" style="width:426.1pt;"><tbody><tr><td style="vertical-align:top;width:426.1pt;"><p><span style="color:#1e1c11;"><span style="color:#646464;">@Test</span></span></p><p><span style="color:#1e1c11;"><strong><span style="color:#7f0055;"><strong>public</strong></span></strong> <strong><span style="color:#7f0055;"><strong>void</strong></span></strong><span style="color:#000000;"> testAddList(){</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;">List<TbItem> </span><span style="color:#6a3e3e;">list</span><span style="color:#000000;">=</span><strong><u><span style="color:#7f0055;"><strong><u>new</u></strong></span></u></strong><u><span style="color:#000000;"><u> ArrayList()</u></span></u><span style="color:#000000;">;</span></span></p><p> </p><p><span style="color:#1e1c11;"><strong><span style="color:#7f0055;"><strong>for</strong></span></strong><span style="color:#000000;">(</span><strong><span style="color:#7f0055;"><strong>int</strong></span></strong> <span style="color:#6a3e3e;">i</span><span style="color:#000000;">=0;</span><span style="color:#6a3e3e;">i</span><span style="color:#000000;"><100;</span><span style="color:#6a3e3e;">i</span><span style="color:#000000;">++){</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;">TbItem </span><span style="color:#6a3e3e;">item</span><span style="color:#000000;">=</span><strong><span style="color:#7f0055;"><strong>new</strong></span></strong><span style="color:#000000;"> TbItem();</span></span></p><p><span style="color:#1e1c11;"><span style="color:#6a3e3e;">item</span><span style="color:#000000;">.setId(</span><span style="color:#6a3e3e;">i</span><span style="color:#000000;">+1L);</span></span></p><p><span style="color:#1e1c11;"><span style="color:#6a3e3e;">item</span><span style="color:#000000;">.setBrand(</span><span style="color:#2a00ff;">"华为"</span><span style="color:#000000;">);</span></span></p><p><span style="color:#1e1c11;"><span style="color:#6a3e3e;">item</span><span style="color:#000000;">.setCategory(</span><span style="color:#2a00ff;">"手机"</span><span style="color:#000000;">);</span></span></p><p><span style="color:#1e1c11;"><span style="color:#6a3e3e;">item</span><span style="color:#000000;">.setGoodsId(1L);</span></span></p><p><span style="color:#1e1c11;"><span style="color:#6a3e3e;">item</span><span style="color:#000000;">.setSeller(</span><span style="color:#2a00ff;">"华为 2 号专卖店"</span><span style="color:#000000;">);</span></span></p><p><span style="color:#1e1c11;"><span style="color:#6a3e3e;">item</span><span style="color:#000000;">.setTitle(</span><span style="color:#2a00ff;">"华为 Mate"</span><span style="color:#000000;">+</span><span style="color:#6a3e3e;">i</span><span style="color:#000000;">);</span></span></p><p><span style="color:#1e1c11;"><span style="color:#6a3e3e;">item</span><span style="color:#000000;">.setPrice(</span><strong><span style="color:#7f0055;"><strong>new</strong></span></strong><span style="color:#000000;"> BigDecimal(2000+</span><span style="color:#6a3e3e;">i</span><span style="color:#000000;">));</span></span></p><p><span style="color:#1e1c11;"><span style="color:#6a3e3e;">list</span><span style="color:#000000;">.add(</span><span style="color:#6a3e3e;">item</span><span style="color:#000000;">);</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;">}</span></span></p><p> </p><p><span style="color:#1e1c11;"><span style="color:#0000c0;">solrTemplate</span><span style="color:#000000;">.saveBeans(</span><span style="color:#6a3e3e;">list</span><span style="color:#000000;">);</span></span></p><p><span style="color:#1e1c11;"><span style="color:#0000c0;">solrTemplate</span><span style="color:#000000;">.commit();</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;">}</span></span></p></td></tr></tbody


【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


</table>


编写分页查询测试代码:


<table border="1" cellspacing="0" style="width:426.1pt;"><tbody><tr><td style="vertical-align:top;width:426.1pt;"><p> </p><p><span style="color:#1e1c11;"><span style="color:#646464;">@Test</span></span></p><p><span style="color:#1e1c11;"><strong><span style="color:#7f0055;"><strong>public</strong></span></strong> <strong><span style="color:#7f0055;"><strong>void</strong></span></strong> <span style="color:#000000;">testPageQuery</span><span style="color:#000000;">(){</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;">Query </span><span style="color:#6a3e3e;">query</span><span style="color:#000000;">=</span><strong><span style="color:#7f0055;"><strong>new</strong></span></strong><span style="color:#000000;"> SimpleQuery(</span><span style="color:#2a00ff;">":"</span><span style="color:#000000;">);</span></span></p><p><span style="color:#1e1c11;"><span style="color:#6a3e3e;">query</span><span style="color:#000000;">.setOffset(20);</span><span style="color:#3f7f5f;">//开始索引(默认 0)</span></span></p><p><span style="color:#1e1c11;"><span style="color:#6a3e3e;">query</span><span style="color:#000000;">.setRows(20);</span><span style="color:#3f7f5f;">//每页记录数(默认 10)</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;">ScoredPage<TbItem> </span><span style="color:#6a3e3e;">page</span><span style="color:#000000;"> = </span><span style="color:#0000c0;">solrTemplate</span><span style="color:#000000;">.queryForPage(</span><span style="color:#6a3e3e;">query</span><span style="color:#000000;">, TbItem.</span><strong><span style="color:#7f0055;"><strong>class</strong></span></strong><span style="color:#000000;">);</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;">System.</span><strong><em><span style="color:#0000c0;"><strong><em>out</em></strong></span></em></strong><span style="color:#000000;">.println(</span><span style="color:#2a00ff;">"总记录数:"</span><span style="color:#000000;">+</span><span style="color:#6a3e3e;">page</span><span style="color:#000000;">.getTotalElements());</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;">List<TbItem> </span><span style="color:#6a3e3e;">list</span><span style="color:#000000;"> = </span><span style="color:#6a3e3e;">page</span><span style="color:#000000;">.getContent();</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;">showList(</span><span style="color:#6a3e3e;">list</span><span style="color:#000000;">);</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;">}</span></span></p><p><span style="color:#1e1c11;"><span style="color:#3f7f5f;">//显示记录数据</span></span></p><p><span style="color:#1e1c11;"><strong><span style="color:#7f0055;"><strong>private</strong></span></strong> <strong><span style="color:#7f0055;"><strong>void</strong></span></strong><span style="color:#000000;"> showList(List<TbItem> </span><span style="color:#6a3e3e;">list</span><span style="color:#000000;">){</span></span></p><p><span style="color:#1e1c11;"><strong><span style="color:#7f0055;"><strong>for</strong></span></strong><span style="color:#000000;">(TbItem </span><span style="color:#6a3e3e;">item</span><span style="color:#000000;">:</span><span style="color:#6a3e3e;">list</span><span style="color:#000000;">){</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;">System.</span><strong><em><span style="color:#0000c0;"><strong><em>out</em></strong></span></em></strong><span style="color:#000000;">.println(</span><span style="color:#6a3e3e;">item</span><span style="color:#000000;">.getTitle() +</span><span style="color:#6a3e3e;">item</span><span style="color:#000000;">.getPrice());</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;">}</span></span></p><p><span style="color:#1e1c11;"><span style="color:#000000;">}</span></span></p></td></tr></tbody></table>

条件查询

评论

发布
暂无评论
全文检索工具solr:第一章:理论知识