Solrj 7.0.1 学习总结(一)使用solrj进行文档提交
2017-11-16 12:24
225 查看
在solrj 7.0.1 API中,没有SolrServer,所有的request都由SolrClient发送,用来处理与solr的连接和对话。
request以SolrRequests的形式发送,并以SolrResponses的形式返回。
使用solr中自带的jetty。
向solr中提交数据:
①方式一:
②方式二:
方式一与方式二的唯一区别在于baseUrl中是否明确指定core或者collection。
如果baseUrl中以指定core,但是使用SolrClient的add(String collection, SolrInputDocument doc)方法,则会出现以下错误:
如果baseUrl中未指定core,但是使用add(SolrInputDocument doc)方法,则会出现以下错误:
两种错误都会导致404。
③方式三:
Solrj支持@Field注解,指定bean的一个字段为field,schema.xml中必须有相应的field元素与之对应,否则报错。
request以SolrRequests的形式发送,并以SolrResponses的形式返回。
使用solr中自带的jetty。
向solr中提交数据:
①方式一:
String baseUrl = "http://localhost:8983/solr/core"; HttpSolrClient solrClient = new HttpSolrClient.Builder(baseUrl).withConnectionTimeOut(10000) .withSocketTimeout(60000).build(); SolrInputDocument document = new SolrInputDocument(); document.addField("id",1001); document.addField("name","katherineeeee"); solrClient.add(document); solrClient.commit();
②方式二:
String baseUrl = "http://localhost:8983/solr"; HttpSolrClient solrClient = new HttpSolrClient.Builder(baseUrl).withConnectionTimeOut(10000) .withSocketTimeout(60000).build(); SolrInputDocument document = new SolrInputDocument(); document.addField("id",1001) document.addField("name","katherineeeee"); solrClient.add("core",document); solrClient.commit("core");
方式一与方式二的唯一区别在于baseUrl中是否明确指定core或者collection。
如果baseUrl中以指定core,但是使用SolrClient的add(String collection, SolrInputDocument doc)方法,则会出现以下错误:
org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException: Error from server at http://192.168.37.130:8983/solr/core: Expected mime type application/octet-stream but got text/html. <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <title>Error 404 Not Found</title> </head> <body><h2>HTTP ERROR 404</h2> <p>Problem accessing /solr/core/core/update. Reason: <pre> Not Found</pre></p> </body> </html>
如果baseUrl中未指定core,但是使用add(SolrInputDocument doc)方法,则会出现以下错误:
org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException:** <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <title>Error 404 Not Found</title> </head> <body><h2>HTTP ERROR 404</h2> <p>Problem accessing /solr/update. Reason: <pre> Not Found</pre></p> </body> </html>
两种错误都会导致404。
③方式三:
Solrj支持@Field注解,指定bean的一个字段为field,schema.xml中必须有相应的field元素与之对应,否则报错。
public class Core{ @Field private Integer id; @Field private String name; } 可使用client的addBean()方法和commit()方法。 同样的,需要注意,如果在baseUrl中没有指定core,则在加入bean和commit时需要加入一个字符串参数,指明具体的core,否则找不到。
相关文章推荐
- Android学习之Http使用Post方式进行数据提交(普通数据和Json数据)
- java学习总结(16.05.08)在windows下使用cmd命令行对java文件进行编译和执行
- Android学习之Http使用Post方式进行数据提交
- Apache Ant学习总结(三):使用svnant对SVN进行操作
- Solr文档学习--Solrj的使用
- javaweb学习总结十三(dom4j方式对XML文档进行解析以及Xpath的使用)
- OC学习,self使用总结和注意事项,理解类和对象,使用self进行理解非常好
- [学习总结]8、android 自定义控件 使用declare-styleable进行配置属性(源码角度)
- Spring Boot学习总结(4)——使用Springloaded进行热部署
- CNTK API文档翻译(24)——使用深度迁移学习进行图像识别
- maven2学习总结(4.生命周期和使用Maven进行编译)
- Spring Boot学习总结(4)——使用Springloaded进行热部署
- Solr学习笔记五--solrj的使用(查询文档)
- 公司开发时候,使用svn进行版本控制,在提交代码的时候应该注意的问题,总结!!!!
- Android学习之Http使用Post方式进行数据提交
- xml学习笔记(6)使用Dom4j对xml文档进行dom解析
- Mybatis的学习总结二:使用Mybatis对表进行CRUD操作【参考】
- Solr学习笔记四--solrj的使用(更新文档)
- OpenGL学习笔记(十三):将纹理贴图应用到四边形上,对VAO/VBO/EBO/纹理/着色器的使用方式进行总结
- Android学习之Http使用Post方式进行数据提交