Solr索引数据
一般来说,索引是系统地排列文档或(其他实体)。索引使用户能够在文档中快速地查找信息。
- 索引集合,解析和存储文档。
- 索引是为了在查找所需文档时提高搜索查询的速度和性能。
在Apache Solr中的索引
在Apache Solr中,我们可以索引(添加,删除,修改)各种文档格式,如xml,csv,pdf等。可以通过几种方式向Solr索引添加数据。
在本章中,将讨论创建索引的几个方法 -
- 使用Solr Web界面。
- 使用任何客户端API(如Java,Python等)。
- 使用提交工具。
在本章中,将讨论如何使用各种接口(命令行,Web界面和Java客户端API)向Apache Solr的索引添加数据,
使用Post命令添加文档
Solr在其bin/目录中有一个post命令。使用这个命令,可以在Apache Solr中索引各种格式的文件,例如JSON,XML,CSV。
进入到Apache Solr的bin目录并执行post命令的-h选项,如以下代码块所示。
yiibai@ubuntu:/usr/local/solr-6.4.0/bin$ cd $SOLR_HOME
yiibai@ubuntu:/usr/local/solr-6.4.0/bin$ ./post -h
在执行上述命令时,将得到post命令的选项列表,如下所示。
Usage: post -c <collection> [OPTIONS] <files|directories|urls|-d [".."]>
or post –help
collection name defaults to DEFAULT_SOLR_COLLECTION if not specified
OPTIONS
=======
Solr options:
-url <base Solr update URL> (overrides collection, host, and port)
-host <host> (default: localhost)
-p or -port <port> (default: 8983)
-commit yes|no (default: yes)
Web crawl options:
-recursive <depth> (default: 1)
-delay <seconds> (default: 10)
Directory crawl options:
-delay <seconds> (default: 0)
stdin/args options:
-type <content/type> (default: application/xml)
Other options:
-filetypes <type>[,<type>,...] (default:
xml,json,jsonl,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots,
rtf,htm,html,txt,log)
-params "<key> = <value>[&<key> = <value>...]" (values must be
URL-encoded; these pass through to Solr update request)
-out yes|no (default: no; yes outputs Solr response to console)
-format Solr (sends application/json content as Solr commands
to /update instead of /update/json/docs)
Examples:
* JSON file:./post -c wizbang events.json
* XML files: ./post -c records article*.xml
* CSV file: ./post -c signals LATEST-signals.csv
* Directory of files: ./post -c myfiles ~/Documents
* Web crawl: ./post -c gettingstarted http://lucene.apache.org/Solr -recursive 1 -delay 1
* Standard input (stdin): echo '{commit: {}}' | ./post -c my_collection -
type application/json -out yes –d
* Data as string: ./post -c signals -type text/csv -out yes -d $'id,value\n1,0.47'
示例`
假设有一个名称为sample.csv的文件,其内容如下(这个文件也在`bin目录中)。
上述数据集包含个人详细信息,如学生ID,名字,姓氏,电话和城市。数据集的CSV文件如下所示。 在这里必须注意:数据记录的第一行。
id, first_name, last_name, phone_no, location
001, Pruthvi, Reddy, 9848022337, Hyderabad
002, kasyap, Sastry, 9848022338, Vishakapatnam
003, Rajesh, Khanna, 9848022339, Delhi
004, Preethi, Agarwal, 9848022330, Pune
005, Trupthi, Mohanty, 9848022336, Bhubaneshwar
006, Archana, Mishra, 9848022335, Chennai
可以使用post命令在名称为Solr_sample的核心下,对此数据编制索引,如下所示:
yiibai@ubuntu:/usr/local/solr-6.4.0/bin$ ./post -c solr_sample sample.csv
在执行上述命令时,给定文档在指定的核心下会生成索引,生成以下输出。
yiibai@ubuntu:/usr/local/solr-6.4.0/bin$ ./post -c solr_sample sample.csv
/usr/local/jdk1.8.0_65/bin/java -classpath /usr/local/solr-6.4.0/dist/solr-core-6.4.0.jar -Dauto=yes -Dc=solr_sample -Ddata=files org.apache.solr.util.SimplePostTool sample.csv
SimplePostTool version 5.0.0
Posting files to [base] url http://localhost:8983/solr/solr_sample/update...
Entering auto mode. File endings considered are xml,json,jsonl,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots,rtf,htm,html,txt,log
POSTing file sample.csv (text/csv) to [base]
1 files indexed.
COMMITting Solr index changes to http://localhost:8983/solr/solr_sample/update...
Time spent: 0:00:00.663
访问Solr Web UI的主页使用以下URL -
选择核心Solr_sample。 默认情况下,请求处理程序是/select,查询为“:”。 不做任何修改,单击页面底部的ExecuteQuery按钮。

在执行查询时,可以以JSON格式(默认)观察索引的CSV文档的内容,如下面的屏幕截图所示。

注意 - 以相同的方式,可以索引其他文件格式,如JSON,XML,CSV等。
使用Solr Web界面添加文档
还可以使用Solr提供的Web界面对文档编制索引。看看下面如何索引JSON格式的文档。
[
{
"id" : "001",
"name" : "Ram",
"age" : 53,
"Designation" : "Manager",
"Location" : "Hyderabad",
},
{
"id" : "002",
"name" : "Robert",
"age" : 43,
"Designation" : "SR.Programmer",
"Location" : "Chennai",
},
{
"id" : "003",
"name" : "Rahim",
"age" : 25,
"Designation" : "JR.Programmer",
"Location" : "Delhi",
}
]
第1步
使用以下URL打开Solr Web界面 -
第2步
选择核心Solr_sample。 默认情况下,Request Handler,Common Within,Overwrite和Boost字段的值分别为/update,1000,true和1.0,如下面的屏幕截图所示。
现在,从JSON,CSV,XML等中选择所需的文档格式。在文本区域中键入要索引的文档,然后单击提交文档按钮,如下面的屏幕截图所示。
使用Java Client API添加文档
以下是Java程序向Apache Solr索引添加文档代码。将代码保存在AddingDocument.java文件中。
import java.io.IOException;
import org.apache.Solr.client.Solrj.SolrClient;
import org.apache.Solr.client.Solrj.SolrServerException;
import org.apache.Solr.client.Solrj.impl.HttpSolrClient;
import org.apache.Solr.common.SolrInputDocument;
public class AddingDocument {
public static void main(String args[]) throws Exception {
//Preparing the Solr client
String urlString = "http://localhost:8983/Solr/my_core";
SolrClient Solr = new HttpSolrClient.Builder(urlString).build();
//Preparing the Solr document
SolrInputDocument doc = new SolrInputDocument();
//Adding fields to the document
doc.addField("id", "003");
doc.addField("name", "Rajaman");
doc.addField("age","34");
doc.addField("addr","vishakapatnam");
//Adding the document to Solr
Solr.add(doc);
//Saving the changes
Solr.commit();
System.out.println("Documents added");
}
}
通过在终端中执行以下命令编译上述代码 -
yiibai@ubuntu:/usr/local/solr-6.4.0/bin$ javac AddingDocument.java
yiibai@ubuntu:/usr/local/solr-6.4.0/bin$ java AddingDocument
执行上述命令后,将得到以下输出。
Documents added
Solr索引数据的更多相关文章
- [solr] - 索引数据删除
删除solr索引数据,使用XML有两种写法: 1) <delete><id>1</id></delete> <commit/> 2) < ...
- Solr(六)Solr索引数据存放到HDFS下
Solr索引数据存放到HDFS下 一 新建solr core hdfs 方法:http://www.cnblogs.com/Matchman/p/7287385.html 二 修改solrconfig ...
- [转][solr] - 索引数据删除
删除solr索引数据,使用XML有两种写法: 1) <delete><id>1</id></delete> <commit/> 2) < ...
- (二) solr 索引数据导入:xml格式
xml 是最常用的数据索引格式,不仅可以索引数据,还可以对文档与字段进行增强,从而改变它们的重要程度. 下面就是具体的实现方式: schema.xml的字段配置部分如下: <field name ...
- Java solr 索引数据增删改查
具体代码如下: import java.io.IOException; import java.util.*; import org.apache.solr.client.solrj.SolrClie ...
- python 操作solr索引数据
测试代码1: def test(self): data = {", "*字段名*": u"我是一个大好人"}}} params = {"bo ...
- 企业级搜索引擎Solr 第三章 索引数据(Indexing Data)[1]
转载:http://quweiprotoss.wap.blog.163.com/ Push data to Solr or have Solr pull it 尽管一个应用通过HTTP方式与Solr通 ...
- 企业级搜索引擎Solr 第三章 索引数据(Indexing Data)[1] (转)
Index Data Author: David Smiley Eric Pugh 译者:Koala++ / 屈伟 在这一章中我们将了解如何将数据传入Solr.这个传入的过程称之为索引,尽管中间还包含 ...
- [solr 管理界面] - 索引数据删除
删除solr索引数据,使用XML有两种写法: 1) <delete><id>1</id></delete> <commit/> 2) < ...
随机推荐
- Week 7 - 714. Best Time to Buy and Sell Stock with Transaction Fee & 718. Maximum Length of Repeated Subarray
714. Best Time to Buy and Sell Stock with Transaction Fee - Medium Your are given an array of intege ...
- django amdin后台改成中文
- 个人对BFC的见解
BFC:块级格式化上下文,它是指一个独立的块级渲染区域,只有Block-level BOX参与,该区域拥有一套渲染规则来约束块级盒子的布局,且与区域外部无关. BFC的生成 既然上文提到BFC是一块渲 ...
- Python基础-7.1字符串的格式化
字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-310 ...
- (136)leetcode刷题Python笔记——只出现一次的数字
题目如下: 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? 示例 1: ...
- 多线程12-ManualResetEventSlim
)); )); )); t1.Start(); t2.Start(); ...
- 在无界面centos7上部署MYSQL5.7数据库
1. 利用xshell连接好服务后,输入 wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm 下载软件安装 ...
- mysql 小数位
1 select convert(t/100,decimal(15,2)) as a from user (1) convert() 一.在mysql操作中我们经常需要对数据进行类型转换.此时我 ...
- Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式)
Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式) 题外话,这是我第40篇随笔,纪念一下.<( ̄︶ ̄)↗[GO!] 题意 是说有棵树,每个节点上 ...
- 在学习linux磁盘管理期间学习的逻辑卷管理笔记
LVM(逻辑分区)的创建顺序:物理分区-物理卷-卷组-逻辑卷-挂载. 物理卷(Physical Volume,PV):就是指硬盘分区,也可以是整个硬盘或已创建的软RAID,是LVM的基本存储设备. 卷 ...