知识点
相关文章
更多最近更新
更多Apache Solr 概念、安装与导入数据库中表格的数据到索引(适合新手)
2019-03-27 01:19|来源: 网路
Solr 概念
在配置方面,solrconfig.xml 文件不仅指定了 Solr 如何处理索引、突出显示、分类、搜索以及其他请求,还指定了用于指定缓存的处理方法的属性,以及用于指定 Lucene 管理索引的方法的属性。配置取决于模式,但模式不取决于配置。
索引和搜索都是通过向 Solr 服务器发送请求来实现的。索引的实现方法很简单,用 POST一个描述所有 Field及其内容的 XML 文档就可以了,如位于 apache-solr-1.3.0/example/exampledocs/ 目录下的 hd.xml 样例文档。
修改apache-solr-3.3.0\example\solr\conf\schema.xml文件
3、编辑apache-solr-3.3.0\example\solr\conf\solrconfig.xml文件,添加内容如下:
4、创建data-config.xml文件,放在apache-solr-3.3.0\example\solr\conf\目录下,文件内容如下:
5、将MySQL的JDBC驱动放在apache-solr-3.3.0\example\lib目录下
6、启动Solr服务,命令行进入apache-solr-3.3.0\example\目录,运行 java -jar start.jar
出现问题:
wiki上的解释:
我的理解:相当于竞价排名,无视搜索的排序,可以额外在顶部配置自定义的结果
7、运行命令:在浏览器中输入:http://localhost:8983/solr/dataimport?command=full-import 来完成全量数据导入,在每次全量数据导入执行的时候,原有索引会被删除,如果不想删除原有索引,可以运行如下命令:http://localhost:8983/solr/dataimport?command=full-import&clean=false ,全量数据导入在浏览器中显示的效果如下:
8、进行搜索,在浏览器中输入http://localhost:8983/solr/admin/
9、以不同的名字索引域
上面data-config.xml配置中mysql中表的名字和schema.xml索引的名字相同,也可以配置使用不同的名字,如下:
12、参考:
Solr Data Import 快速入门:http://blog.chenlb.com/2010/03/solr-data-import-quick-start.html
Index a DB table directly into Solr:http://wiki.apache.org/solr/DIHQuickStart
转自:http://blog.csdn.net/A__java___A/article/details/7353411
从概念上,Solr 可以被分成四大块:
- 模式(schema.xml)
- 配置(solrconfig.xml)
- 索引
- 搜索
在配置方面,solrconfig.xml 文件不仅指定了 Solr 如何处理索引、突出显示、分类、搜索以及其他请求,还指定了用于指定缓存的处理方法的属性,以及用于指定 Lucene 管理索引的方法的属性。配置取决于模式,但模式不取决于配置。
索引和搜索都是通过向 Solr 服务器发送请求来实现的。索引的实现方法很简单,用 POST一个描述所有 Field及其内容的 XML 文档就可以了,如位于 apache-solr-1.3.0/example/exampledocs/ 目录下的 hd.xml 样例文档。
要使用 Solr 和本文中的样例,您必须先安装以下软件:
- Java 1.5 或更高版本。
- Web 浏览器,您将用它查看管理页面。我使用的是 Firefox,但可以使用其他现代浏览器。
- 要运行 DataImportHandler样例,需要一个数据库及其 JDBC 驱动。在本文的样例中,我使用的是 PostgreSQL;MySQL 或其他数据库应该也可以,但可能需要修改我编写的 SQL 以使它适合您的数据库。
- 需要一个 servlet 容器。我在本文中使用的是 Jetty,它与 Solr 打包在一起,所以也就没必要使用其他容器了。但如果您偏爱 Tomcat 或其它容器的话,Solr 也能很好地适应它们。
1、首先在MySQL中创建表,并写入数据
-- ---------------------------- -- Table structure for course -- ---------------------------- CREATE TABLE `course` ( `id` int(11) NOT NULL, `user` varchar(20) NOT NULL, `title` varchar(50) NOT NULL, `content` varchar(200) NOT NULL, `time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ---------------------------- -- Records -- ---------------------------- INSERT INTO `course` VALUES ('1', 'tiehong', '搜索', '这一小节讲搜索引擎,可以通过搜索引擎查找一些数据', '2011-08-18 10:41:35'); INSERT INTO `course` VALUES ('2', 'chenyun', '存储', '这一小节讲分布式存储,可以存储很多数据', '2011-08-18 10:42:19'); INSERT INTO `course` VALUES ('3', 'lengda', '数据导入', '这一小节讲数据导入,可以从mysql数据库中导入数据', '2011-08-18 10:42:59');
修改apache-solr-3.3.0\example\solr\conf\schema.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <schema name="example" version="1.4"> <types> <fieldType name="tint" class="solr.TrieIntField" precisionStep="8" omitNorms="true" positionIncrementGap="0"/> <fieldType name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/> <fieldType name="text" class="solr.TextField" positionIncrementGap="100"> <analyzer type="index"> <tokenizer class="solr.StandardTokenizerFactory"/> <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" /> <filter class="solr.LowerCaseFilterFactory"/> </analyzer> <analyzer type="query"> <tokenizer class="solr.StandardTokenizerFactory"/> <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" /> <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/> <filter class="solr.LowerCaseFilterFactory"/> </analyzer> </fieldType> <fieldType name="date" class="solr.TrieDateField" omitNorms="true" precisionStep="0" positionIncrementGap="0"/> </types> <fields> <field name="id" type="tint" indexed="true" stored="true" required="true" /> <field name="user" type="string" indexed="true" stored="true"/> <field name="title" type="text" indexed="true" stored="true"/> <field name="content" type="text" indexed="true" stored="true" /> <field name="time" type="date" indexed="true" stored="true" default="NOW"/> </fields> <uniqueKey>id</uniqueKey> <defaultSearchField>title</defaultSearchField> <solrQueryParser defaultOperator="AND"/> </schema>
3、编辑apache-solr-3.3.0\example\solr\conf\solrconfig.xml文件,添加内容如下:
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">data-config.xml</str> </lst> </requestHandler>
4、创建data-config.xml文件,放在apache-solr-3.3.0\example\solr\conf\目录下,文件内容如下:
<dataConfig> <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/dbname" user="user-name" password="password"/> <document> <entity name="id" query="select * from course"> </entity> </document> </dataConfig>
5、将MySQL的JDBC驱动放在apache-solr-3.3.0\example\lib目录下
6、启动Solr服务,命令行进入apache-solr-3.3.0\example\目录,运行 java -jar start.jar
出现问题:
严重: org.apache.solr.common.SolrException: QueryElevationComponent requires the schema to have a uniqueKeyField implemented using StrField at org.apache.solr.handler.component.QueryElevationComponent.inform(QueryElevationComponent.java:158) at org.apache.solr.core.SolrResourceLoader.inform(SolrResourceLoader.java:522) at org.apache.solr.core.SolrCore.<init>(SolrCore.java:594) at org.apache.solr.core.CoreContainer.create(CoreContainer.java:463) at org.apache.solr.core.CoreContainer.load(CoreContainer.java:316) at org.apache.solr.core.CoreContainer.load(CoreContainer.java:207) at org.apache.solr.core.CoreContainer$Initializer.initialize(CoreContainer.java:130) at org.apache.solr.servlet.SolrDispatchFilter.init(SolrDispatchFilter.java:94) at org.mortbay.jetty.servlet.FilterHolder.doStart(FilterHolder.java:97) at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50) at org.mortbay.jetty.servlet.ServletHandler.initialize(ServletHandler.java:713) at org.mortbay.jetty.servlet.Context.startContext(Context.java:140) at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1282) at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:518) at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:499) at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50) at org.mortbay.jetty.handler.HandlerCollection.doStart(HandlerCollection.java:152) at org.mortbay.jetty.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:156) at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50) at org.mortbay.jetty.handler.HandlerCollection.doStart(HandlerCollection.java:152) at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50) at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130) at org.mortbay.jetty.Server.doStart(Server.java:224) at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50) at org.mortbay.xml.XmlConfiguration.main(XmlConfiguration.java:985) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.mortbay.start.Main.invokeMain(Main.java:194) at org.mortbay.start.Main.start(Main.java:534) at org.mortbay.start.Main.start(Main.java:441) at org.mortbay.start.Main.main(Main.java:119)
解决:
修改apache-solr-3.3.0\example\solr\conf\solrconfig.xml文件,删除内容如下:
<!-- Query Elevation Component http://wiki.apache.org/solr/QueryElevationComponent a search component that enables you to configure the top results for a given query regardless of the normal lucene scoring. --> <searchComponent name="elevator" class="solr.QueryElevationComponent" > <!-- pick a fieldType to analyze queries --> <str name="queryFieldType">string</str> <str name="config-file">elevate.xml</str> </searchComponent> <!-- A request handler for demonstrating the elevator component --> <requestHandler name="/elevate" class="solr.SearchHandler" startup="lazy"> <lst name="defaults"> <str name="echoParams">explicit</str> </lst> <arr name="last-components"> <str>elevator</str> </arr> </requestHandler>
wiki上的解释:
The QueryElevationComponent enables you to configure the top results for a given query regardless of the normal lucene scoring. This is sometimes called "sponsored search", "editorial boosting" or "best bets". This component matches the user query text to a configured Map of top results. Although this component will work with any QueryParser, it makes the most sense to use with DisMax style queries.
我的理解:相当于竞价排名,无视搜索的排序,可以额外在顶部配置自定义的结果
7、运行命令:在浏览器中输入:http://localhost:8983/solr/dataimport?command=full-import 来完成全量数据导入,在每次全量数据导入执行的时候,原有索引会被删除,如果不想删除原有索引,可以运行如下命令:http://localhost:8983/solr/dataimport?command=full-import&clean=false ,全量数据导入在浏览器中显示的效果如下:
This XML file does not appear to have any style information associated with it. The document tree is shown below. <response> <lst name="responseHeader"> <int name="status">0</int> <int name="QTime">5</int> </lst> <lst name="initArgs"> <lst name="defaults"> <str name="config">data-config.xml</str> </lst> </lst> <str name="command">full-import</str> <str name="status">idle</str> <str name="importResponse"/> <lst name="statusMessages"/> <str name="WARNING"> This response format is experimental. It is likely to change in the future. </str> </response>
8、进行搜索,在浏览器中输入http://localhost:8983/solr/admin/
搜索结果如下:
This XML file does not appear to have any style information associated with it. The document tree is shown below. <response> <lst name="responseHeader"> <int name="status">0</int> <int name="QTime">0</int> <lst name="params"> <str name="indent">on</str> <str name="start">0</str> <str name="q">*:*</str> <str name="version">2.2</str> <str name="rows">10</str> </lst> </lst> <result name="response" numFound="3" start="0"> <doc> <str name="content">这一小节讲搜索引擎,可以通过搜索引擎查找一些数据</str> <int name="id">1</int> <date name="time">2011-08-18T02:41:35Z</date> <str name="title">搜索</str> <str name="user">tiehong</str> </doc> <doc> <str name="content">这一小节讲分布式存储,可以存储很多数据</str> <int name="id">2</int> <date name="time">2011-08-18T02:42:19Z</date> <str name="title">存储</str> <str name="user">chenyun</str> </doc> <doc> <str name="content">这一小节讲数据导入,可以从mysql数据库中导入数据</str> <int name="id">3</int> <date name="time">2011-08-18T02:42:59Z</date> <str name="title">数据导入</str> <str name="user">lengda</str> </doc> </result> </response>
9、以不同的名字索引域
上面data-config.xml配置中mysql中表的名字和schema.xml索引的名字相同,也可以配置使用不同的名字,如下:
<dataConfig> <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/dbname" user="user-name" password="password"/> <document> <entity name="id" query="select id,name,desc from mytable"> <field column="id" name="solr_id"/> <field column="name" name="solr_name"/> <field column="desc" name="solr_desc"/> </entity> </document> </dataConfig>
document:一个文档也就是lucene的document这个没什么解释的;
entity:主要针对的是一个数据库表;
filed:属性column是数据库的字段,name是filed的名字,即schema中的field name
此时,solr_id, solr_name,solr_desc将被写到solr域中,务必注意要在schema.xml文件中配置好这些域
10、从多个表索引数据到Solr
修改data-config.xml文件
<dataConfig> <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/dbname" user="user-name" password="password"/> <document> <entity name="outer" query="select id,name,desc from mytable"> <field column="id" name="solr_id"/> <field column="name" name="solr_name"/> <field column="desc" name="solr_desc"/> <entity name="inner" query="select details from another_table where id ='${outer.id}'"> <field column="details" name="solr_details"/> </entity> </entity> </document> </dataConfig>
注意:schema.xml中还应该有solr_details域
11、数据管理地址: http://localhost:8983/solr/admin/dataimport.jsp?handler=/dataimport
12、参考:
Solr Data Import 快速入门:http://blog.chenlb.com/2010/03/solr-data-import-quick-start.html
Index a DB table directly into Solr:http://wiki.apache.org/solr/DIHQuickStart
参考文章:
http://tbwuming.iteye.com/blog/1152333
http://www.ibm.com/developerworks/cn/java/j-solr-update/
转自:http://blog.csdn.net/A__java___A/article/details/7353411
相关问答
更多-
将Apache Solr与MongoDB一起使用的正确做法是什么?(What are the right practices of using Apache Solr with MongoDB?)[2022-10-15]
如果仅查询Solr是有意义的,那就这样做。 如果查询Solr某些数据是有意义的,那就这样做吧。 这取决于您的使用案例,但如果可以使用Solr中的数据回答任何查询,那么将它用于所有内容就完全没问题了。 这可能会更有效地使用您的缓存。 不,不是我所知道的。 不是真的。 Solr不太适合嵌套的JSON(即使你有父/子文档,它也是你必须在每种情况下手动处理的东西,并且需要特殊的套管)。 在这些情况下,您可以使用Solr进行查询,返回ID,然后从mongo中检索实际文档,其JSON结构完好无损。 在这种情况下,您可以 ... -
术语有点随意使用,因此您可能会发现可以互换使用其中一些术语的文本。 Solr核心 核心是生活在单个服务器上的一组命名文档。 服务器可以有多个核心。 核心可以复制到其他服务器(这是手动完成时的“旧式”复制)。 Solr系列 集合是一组核心,从一个到多个。 这是“这些核心共同构成整个集合”的逻辑描述。 这是SolrCloud引入的,因为这是Solr第一次为您处理群集。 逻辑与物理 集合是一个逻辑索引 - 它可以跨越多个核心。 每个核心都是一个物理索引(它的磁盘上有Lucene的实际索引文件)。 当您与核心进行交 ...
-
Solr服务器有多忙? 如果不是很忙,请检查Drupal和Solr服务器之间是否有防火墙。 如果没有流量通过,某些防火墙会终止连接。 一种测试方法是访问Solr管理界面。 如果可以的话,服务器本身就没问题,只有Drupal的连接死了。 我假设Drupal中的Solr客户端库试图维持持久连接。 如果不是这样,则上述内容不适用。 I ended up reducing the number of documents to be indexed during cron from 200 to 50. That s ...
-
您的DB_ID可能不是一个有用的索引项。 如果您希望能够一般性地搜索数据库中所有表的记录,则可能需要存储每条记录的DB_ID和表名,并索引您希望能够搜索的任何其他字段。 您可以使用相同的字段名称(例如“ content ”)索引所有这些字段。 这将允许通过对content字段的简单搜索来搜索每个表的每个记录。 我猜想将表名称索引以及存储它也是有用的,这样你可以选择只搜索一个表或表记录的子集。 一旦在索引中找到了DB记录,就可以获取表名和DB_ID并从数据库中检索记录。 Your DB_ID is proba ...
-
好吧,如果你想看看Solr如何工作,包括你点击搜索按钮后所做的一切比开始学习的最好方法Lucene.Solr是Lucene的一个包装,增加了一些功能,使其用户界面更容易工作用。 你需要知道各种各样的事情 Solr / Lucene如何索引数据 它如何解析数据。 这有很多数据处理程序。 它如何读取用户输入的查询。 我建议你通过Lucene wiki页面和它的各种文件Apache Lucene 一旦熟悉下载源代码,就可以了解它们是如何做到这一点的。 否则,如果您对特定功能有特定问题,请更好地提出问题 Well ...
-
如果默认情况下正确索引,则pdf内容会在模式中正确声明时被索引到字段名称content中。 所以使用该内容字段搜索一些关键字(或*)。 例如: q=content:keyword (关键字 - >以pdf格式显示) http://localhost:8983/solr/gettingstarted/select?q=content:* 如果contetnt字段未定义。 然后在模式文件中添加字段定义。 例如:字段名称声明
Azure上的Apache Solr(Apache Solr on Azure)[2023-06-05]
他们今天刚刚宣布您现在可以将群集自定义应用于正在运行的群集 。 请参阅Solr群集自定义页面。 仍不建议手动在正在运行的集群上安装内容(而不是使用集群自定义脚本)。 首先,当需要关闭一个节点进行维护时,新节点将在没有安装自定义软件的情况下重新启动。 其次,群集自定义脚本与Azure通信,支持在打开支持案例时对群集所做的更改。 They just announced today that you can now apply cluster customizations to a running cluster ...在任何insert/select/update/delete请求之前总是PING solr服务器 $solr = new Apache_Solr_Service($hostname, $portname,$core); if(!$solr->ping()) { echo "server not responding"; exit; } $solr->search($basicQuery, $start_limit,$end_limit,$params); always PIN ...Apache solr搜索问题(Apache solr search issue)[2023-07-21]
嗯好吃 首先,对于所有这些问题,使用Solr分析工具是你的朋友。 其次,请记住,如果查询和术语是字符相同的100%字符,则Solr仅匹配。 对于以下过滤器你的问题是关于在solr中使用“自然语言处理”。 你没有说明这对你意味着什么。 这可能意味着:检测语言和启发式词干 在这种情况下,请查看文档: https://cwiki.apache.org/confluence/display/solr/Detecting+Languages+During+Indexing https://cwiki.apache.org/confluence/display/solr/Language+Analysis#LanguageAnalysis-Language-Specif ...