在jsp中实现mongodb文本搜索

时间:2016-05-05 07:44:59

标签: mongodb jsp full-text-search

我正在尝试在jsp页面中实现mongodb文本搜索,但是我收到了一个错误。以下是我的代码和错误。如果您发现我的代码有问题,请告诉我。 非常感谢您的帮助。提前谢谢。

代码:

Mongo m=new MongoClient("localhost",27017);
DB db = m.getDB("sagar");
String searchText="samosa";
DBCollection collection=db.getCollection("review");
DBObject searchCmd = new BasicDBObject();
searchCmd.put("text",collection);
searchCmd.put("search",searchText);

DBCursor cursor = collection.find(searchCmd);

while(cursor.hasNext())
{
    System.out.println(cursor.next());
}

错误:

type Exception report

message An exception occurred processing JSP page /review.jsp at line 33

description The server encountered an internal error that prevented it from fulfilling this request.

exception 
org.apache.jasper.JasperException: An exception occurred processing JSP page /review.jsp at line 33

30:     String id= (String) dbo.get("text");
31:     out.println(id+"\n");
32: }*/
33: while(cursor.hasNext())
34: {
35:     System.out.println(cursor.next());
36: }


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)



root cause 
java.lang.IllegalArgumentException: can't serialize class com.mongodb.DBCollectionImpl
    org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:299)
    org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:194)
    org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:136)
    com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:36)
    com.mongodb.OutMessage.putObject(OutMessage.java:289)
    com.mongodb.OutMessage.writeQuery(OutMessage.java:211)
    com.mongodb.OutMessage.query(OutMessage.java:86)
    com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:83)
    com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:68)
    com.mongodb.DBCursor._check(DBCursor.java:498)
    com.mongodb.DBCursor._hasNext(DBCursor.java:621)
    com.mongodb.DBCursor.hasNext(DBCursor.java:657)
    org.apache.jsp.review_jsp._jspService(review_jsp.java:93)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

1 个答案:

答案 0 :(得分:0)

您正在尝试使用关键文本将地图集合对象设为值:

searchCmd.put( “文本”,集合)

您可能只需要搜索searchCmd.put(“search”,searchText);

喜欢,db.reviews.find({“search”:“samosa”})