solr实例代码 import org.apache.solr.client.solrj.SolrServer
2009-09-18 23:26
435 查看
Hi all,
I used the sample code given below and tried to run with all the relevant
jars. I receive the exception written below.
package test.general;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.FacetField;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.SolrParams;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Random;
import java.util.List;
/**
* Connect to Solr and issue a query
*/
public class SolrJExample {
public static final String [] CATEGORIES = {"a", "b", "c", "d"};
public static void main(String[] args) throws IOException,
SolrServerException {
SolrServer server = new
CommonsHttpSolrServer("http://localhost:8080/solr/update");
Random rand = new Random();
//Index some documents
Collection<SolrInputDocument> docs = new HashSet<SolrInputDocument>();
for (int i = 0; i < 10; i++) {
SolrInputDocument doc = new SolrInputDocument();
doc.addField("link", "http://non-existent-url.foo/" + i + ".html");
doc.addField("source", "Blog #" + i);
doc.addField("source-link", "http://non-existent-url.foo/index.html");
doc.addField("subject", "Subject: " + i);
doc.addField("title", "Title: " + i);
doc.addField("content", "This is the " + i + "(th|nd|rd) piece of
content.");
doc.addField("category", CATEGORIES[rand.nextInt(CATEGORIES.length)]);
doc.addField("rating", i);
System.out.println("Doc[" + i + "] is " + doc);
docs.add(doc);
}
UpdateResponse response = server.add(docs);
System.out.println("Response: " + response);
//Make the documents available for search
server.commit();
//create the query
SolrQuery query = new SolrQuery("content:piece");
//indicate we want facets
query.setFacet(true);
//indicate what field to facet on
query.addFacetField("category");
//we only want facets that have at least one entry
query.setFacetMinCount(1);
//run the query
QueryResponse results = server.query(query);
System.out.println("Query Results: " + results);
//print out the facets
List<FacetField> facets = results.getFacetFields();
for (FacetField facet : facets) {
System.out.println("Facet:" + facet);
}
}
}
The exception :
Exception in thread "main" java.lang.ClassCastException: java.lang.Long
cannot be cast to org.apache.solr.common.util.NamedList
at
org.apache.solr.common.util.NamedListCodec.unmarshal(NamedListCodec.java:89)
at
org.apache.solr.client.solrj.impl.BinaryResponseParser.processResponse(Binar
yResponseParser.java:39)
at
org.apache.solr.client.solrj.impl.CommonsHttpSolrServer.request(CommonsHttpS
olrServer.java:385)
at
org.apache.solr.client.solrj.impl.CommonsHttpSolrServer.request(CommonsHttpS
olrServer.java:183)
at
org.apache.solr.client.solrj.request.UpdateRequest.process(UpdateRequest.jav
a:217)
at org.apache.solr.client.solrj.SolrServer.add(SolrServer.java:48)
at test.general.SolrJExample.main(SolrJExample.java:48)
Can someone help me out.
Regards,
Sajith Vimukthi Weerakoon
I used the sample code given below and tried to run with all the relevant
jars. I receive the exception written below.
package test.general;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.FacetField;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.SolrParams;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Random;
import java.util.List;
/**
* Connect to Solr and issue a query
*/
public class SolrJExample {
public static final String [] CATEGORIES = {"a", "b", "c", "d"};
public static void main(String[] args) throws IOException,
SolrServerException {
SolrServer server = new
CommonsHttpSolrServer("http://localhost:8080/solr/update");
Random rand = new Random();
//Index some documents
Collection<SolrInputDocument> docs = new HashSet<SolrInputDocument>();
for (int i = 0; i < 10; i++) {
SolrInputDocument doc = new SolrInputDocument();
doc.addField("link", "http://non-existent-url.foo/" + i + ".html");
doc.addField("source", "Blog #" + i);
doc.addField("source-link", "http://non-existent-url.foo/index.html");
doc.addField("subject", "Subject: " + i);
doc.addField("title", "Title: " + i);
doc.addField("content", "This is the " + i + "(th|nd|rd) piece of
content.");
doc.addField("category", CATEGORIES[rand.nextInt(CATEGORIES.length)]);
doc.addField("rating", i);
System.out.println("Doc[" + i + "] is " + doc);
docs.add(doc);
}
UpdateResponse response = server.add(docs);
System.out.println("Response: " + response);
//Make the documents available for search
server.commit();
//create the query
SolrQuery query = new SolrQuery("content:piece");
//indicate we want facets
query.setFacet(true);
//indicate what field to facet on
query.addFacetField("category");
//we only want facets that have at least one entry
query.setFacetMinCount(1);
//run the query
QueryResponse results = server.query(query);
System.out.println("Query Results: " + results);
//print out the facets
List<FacetField> facets = results.getFacetFields();
for (FacetField facet : facets) {
System.out.println("Facet:" + facet);
}
}
}
The exception :
Exception in thread "main" java.lang.ClassCastException: java.lang.Long
cannot be cast to org.apache.solr.common.util.NamedList
at
org.apache.solr.common.util.NamedListCodec.unmarshal(NamedListCodec.java:89)
at
org.apache.solr.client.solrj.impl.BinaryResponseParser.processResponse(Binar
yResponseParser.java:39)
at
org.apache.solr.client.solrj.impl.CommonsHttpSolrServer.request(CommonsHttpS
olrServer.java:385)
at
org.apache.solr.client.solrj.impl.CommonsHttpSolrServer.request(CommonsHttpS
olrServer.java:183)
at
org.apache.solr.client.solrj.request.UpdateRequest.process(UpdateRequest.jav
a:217)
at org.apache.solr.client.solrj.SolrServer.add(SolrServer.java:48)
at test.general.SolrJExample.main(SolrJExample.java:48)
Can someone help me out.
Regards,
Sajith Vimukthi Weerakoon
相关文章推荐
- org.apache.solr.client.solrj.SolrServerException: java.net.SocketTimeoutException: Read timed out
- org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: Document contains multiple val
- Org.apache.solr.client.solrj.impl.CloudSolrServer$RouteException:DEF_CONTENT_CHARST 错误解决
- java.lang.UnsupportedClassVersionError: org/apache/solr/client/solrj/SolrServerException
- No qualifying bean of type [org.apache.solr.client.solrj.SolrServer] found for dependency: expected
- 关于solr异常:org.apache.solr.client.solrj.SolrServerException: IOException occured when talking to server at: http:192.168.0.11/solr/的解决
- org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: Expected mime type application
- Noqualifying bean of type [org.apache.solr.client.solrj.SolrServer] is defined:expected single match
- solr异常:org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: Expected mime type appl
- Apache Solrj EmbeddedSolrServer使用
- python实现的udp协议Server和Client代码实例
- linux c socket client与server实例代码
- Apache Solrj EmbeddedSolrServer使用
- Apache HttpClient用法代码实例
- python实现的udp协议Server和Client代码实例
- solrj的CloudSolrClient源码分析及为什么查询慢
- Apache2.4 虚拟主机(VirtualHost)的配置方法,用来解决client denied by server configuration
- UDP类型的 client/server 实例
- java client访问https server(客户端代码、服务器端配置)
- solr+solrJ facet.pivot 用法实例