solr(3)solrj - how to add documents to solr index

本文介绍了如何使用SolrJ向Solr索引添加单个文档、多个文档及使用Java Bean作为参数添加文档的方法,并提供了详细的示例代码。
solr(3)solrj - how to add documents to solr index

1. Use Solrj to add documen to index
All the configuration of fields in under the example\solr\conf directory schema.xml file. For example:
<field name="id" type="string" indexed="true" stored="true" required="true" />
<field name="sku" type="text_en_splitting_tight" indexed="true" stored="true" omitNorms="true"/>
<field name="name" type="text_general" indexed="true" stored="true"/>
<field name="alphaNameSort" type="alphaOnlySort" indexed="true" stored="false"/>
<field name="manu" type="text_general" indexed="true" stored="true" omitNorms="true"/>
<field name="cat" type="string" indexed="true" stored="true" multiValued="true"/>

@Test
public void addDoc() {
//创建doc文档
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", 1);
doc.addField("name", "Solr Input Document");
doc.addField("manu", "this is SolrInputDocument content");

try {
//添加一个doc文档
UpdateResponse response = embeddedSolrServer.add(doc);
System.out.println(embeddedSolrServer.commit());//commit后才保存到索引库
System.out.println(response);
System.out.println("querying time:" + response.getQTime());
System.out.println("Elapsed Time:" + response.getElapsedTime());
System.out.println("status:" + response.getStatus());
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
query("name:solr");
}


private void query(String query) {
SolrParams params = new SolrQuery(query);

try {
QueryResponse response = embeddedSolrServer.query(params);

SolrDocumentList list = response.getResults();
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
} catch (SolrServerException e) {
e.printStackTrace();
}
}

2. Use solrj to add multiple documents
@Test
public void addDocs() {
Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();

SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", 2);
doc.addField("name", "Solr Input Documents 1");
doc.addField("manu", "this is SolrInputDocuments 1 content");

docs.add(doc);

doc = new SolrInputDocument();
doc.addField("id", 3);
doc.addField("name", "Solr Input Documents 2");
doc.addField("manu", "this is SolrInputDocuments 3 content");

docs.add(doc);

try {
//add documents
UpdateResponse response = embeddedSolrServer.add(docs);
//commit后才保存到索引库
System.out.println(embeddedSolrServer.commit());
System.out.println(response);
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
query("solr");
}

There is a parameter collection in API.

3. Use JavaEntity Bean as Parameter to add document
package com.sillycat.easyhunter.model;

import org.apache.solr.client.solrj.beans.Field;

public class Product {

@Field
private String id;

@Field
private String name;

@Field
private String manu;

@Field
private String[] cat;

@Field
private String[] features;

@Field
private float price;

@Field
private int popularity;

@Field
private boolean inStock;
...snip...
getter and setter
...snip...

@Test
public void addBean() {
//Product需要添加相关的Annotation注解,便于告诉solr哪些属性参与到index中
Product p4 = new Product();
p4.setId("4");
p4.setName("add bean index");
p4.setManu("index bean manu");
p4.setCat(new String[] { "category1", "catagory2" });

try {
//添加Index Bean到索引库
UpdateResponse response = this.embeddedSolrServer.addBean(p4);
System.out.println(this.embeddedSolrServer.commit());//commit后才保存到索引库
System.out.println(response);
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
query("name:add bean index");
}

4. Add bean collection to our solr server
@Test
public void addBeans() {
Product p6 = new Product();
p6.setId("6");
p6.setName("add beans index 6");
p6.setManu("index beans manu 6");
p6.setCat(new String[] { "a", "b" });

List<Product> products = new ArrayList<Product>();
products.add(p6);

Product p5 = new Product();
p5.setId("5");
p5.setName("add beans index 5");
p5.setManu("index beans manu 5");
p5.setCat(new String[] { "aaa", "bbbb" });
products.add(p5);
try {
//添加索引库
UpdateResponse response = this.embeddedSolrServer.addBeans(products);
System.out.println(this.embeddedSolrServer.commit());//commit后才保存到索引库
System.out.println(response);
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
query("name:add beans index");
}

references:
http://www.blogjava.net/hoojo/archive/2011/10/21/361747.html
http://wiki.apache.org/solr/Solrj
源码地址: https://pan.quark.cn/s/d1f41682e390 miyoubiAuto 米游社每日米游币自动化Python脚本(务必使用Python3) 8更新:更换cookie的获取地址 注意:禁止在B站、贴吧、或各大论坛大肆传播! 作者已退游,项目不维护了。 如果有能力的可以pr修复。 小引一波 推荐关注几个非常可爱有趣的女孩! 欢迎B站搜索: @嘉然今天吃什么 @向晚大魔王 @乃琳Queen @贝拉kira 第三方库 食用方法 下载源码 在Global.py中设置米游社Cookie 运行myb.py 本地第一次运行时会自动生产一个文件储存cookie,请勿删除 当前仅支持单个账号! 获取Cookie方法 浏览器无痕模式打开 http://user.mihoyo.com/ ,登录账号 按,打开,找到并点击 按刷新页面,按下图复制 Cookie: How to get mys cookie 当触发时,可尝试按关闭,然后再次刷新页面,最后复制 Cookie。 也可以使用另一种方法: 复制代码 浏览器无痕模式打开 http://user.mihoyo.com/ ,登录账号 按,打开,找到并点击 控制台粘贴代码并运行,获得类似的输出信息 部分即为所需复制的 Cookie,点击确定复制 部署方法--腾讯云函数版(推荐! ) 下载项目源码和压缩包 进入项目文件夹打开命令行执行以下命令 xxxxxxx为通过上面方式或取得米游社cookie 一定要用双引号包裹!! 例如: png 复制返回内容(包括括号) 例如: QQ截图20210505031552.png 登录腾讯云函数官网 选择函数服务-新建-自定义创建 函数名称随意-地区随意-运行环境Python3....
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值