Solr API , how to access document? -
i developing custom request handler using solr api. reference taking "morelikethis" reference.
i able access document using "getdoclist" api , taking object of "iterator" gives me document id .
how can access each field of document ??
package org.apache.solr.handler.ext; import org.apache.solr.request.*; import org.apache.solr.response.*; import org.apache.solr.handler.*; import org.apache.solr.core.*; import org.apache.solr.schema.*; import org.apache.lucene.analysis.*; import org.apache.solr.common.params.*; import org.apache.solr.search.*; import org.apache.lucene.search.*; import org.apache.solr.search.solrindexsearcher.*; import org.apache.lucene.document.document; import org.apache.lucene.index.indexreader; import java.util.*; import java.io.*; public class myrequesthandler extends requesthandlerbase{ @override public void handlerequestbody(solrqueryrequest req, solrqueryresponse rsp) throws exception { system.out.println("i in handler"); /** * working core */ solrcore core = req.getcore(); system.out.println("core " + "" + core.getdescription()); system.out.println("data directory " + "" + core.getdatadir()); system.out.println("index directory " + "" + core.getindexdir()); /** * working schema */ indexschema schema = core.getschema(); schemafield field = schema.getfield("country"); system.out.println(field.isrequired()); /** * create map */ indexschema schemaobj = req.getschema(); map<string , schemafield>map = req.getschema().getfields(); // iterate on keys (string str : map.keyset()){ system.out.println(str); } // iterate on schemafield (schemafield schemafieldobj: map.values()){ system.out.println(schemafieldobj.isrequired()); } /** * find unique key field */ field = schemaobj.getuniquekeyfield(); system.out.println(field.getname()); system.out.println(schemaobj.getqueryparserdefaultoperator()); /** * working request parameters */ solrparams reqparams = req.getparams(); string q = reqparams.get(commonparams.q); system.out.println(reqparams.get(commonparams.q)); string deftype = reqparams.get(queryparsing.deftype , qparserplugin.default_qtype); system.out.println(deftype); /** * working qparser */ qparser queryparser = qparser.getparser(q, deftype, req); query query = queryparser.getquery(); /** * searcher */ solrindexsearcher indexsearcher = req.getsearcher(); doclist matchdocs = indexsearcher.getdoclist(query, null, null, 1, 100000,10000); // number of documents matched system.out.println(matchdocs.matches()); // iterator // documentreader reader reader; indexreader readdoc; document doc; dociterator iterator = matchdocs.iterator(); while (iterator.hasnext()){ integer id = iterator.nextdoc(); system.out.println("here " + id); /* how access fields of document */ } } @override public string getdescription() { return "my search handler"; } @override public string getsource() { return "$source$"; } @override public string getversion() { return "$revision$"; } }
well got answer .. here code me each field..
package org.apache.solr.handler.ext; public class myrequesthandler extends requesthandlerbase{ @override public void handlerequestbody(solrqueryrequest req, solrqueryresponse rsp) throws exception { system.out.println("i in handler"); /** * working core */ solrcore core = req.getcore(); system.out.println("core " + "" + core.getdescription()); system.out.println("data directory " + "" + core.getdatadir()); system.out.println("index directory " + "" + core.getindexdir()); /** * working schema */ indexschema schema = core.getschema(); schemafield field = schema.getfield("country"); system.out.println(field.isrequired()); /** * create map */ indexschema schemaobj = req.getschema(); map<string , schemafield>map = req.getschema().getfields(); // iterate on keys (string str : map.keyset()){ system.out.println(str); } // iterate on schemafield (schemafield schemafieldobj: map.values()){ system.out.println(schemafieldobj.isrequired()); } /** * find unique key field */ field = schemaobj.getuniquekeyfield(); system.out.println(field.getname()); system.out.println(schemaobj.getqueryparserdefaultoperator()); /** * working request parameters */ solrparams reqparams = req.getparams(); string q = reqparams.get(commonparams.q); system.out.println(reqparams.get(commonparams.q)); string deftype = reqparams.get(queryparsing.deftype , qparserplugin.default_qtype); system.out.println(deftype); /** * working qparser */ qparser queryparser = qparser.getparser(q, deftype, req); query query = queryparser.getquery(); /** * searcher */ solrindexsearcher indexsearcher = req.getsearcher(); indexreader reader = indexsearcher.getindexreader(); doclist matchdocs = indexsearcher.getdoclist(query, null, null, 1, 100000,10000); // number of documents matched system.out.println(matchdocs.matches()); // iterator // documentreader document doc; string[] docfields; dociterator iterator = matchdocs.iterator(); while (iterator.hasnext()){ integer id = iterator.nextdoc(); system.out.println("here " + id); doc = reader.document(id); docfields = doc.getvalues("state"); for(int i=0;i<docfields.length;i++){ system.out.println(docfields[i]); } } } @override public string getdescription() { return "my search handler"; } @override public string getsource() { return "$source$"; } @override public string getversion() { return "$revision$"; } }
well in short, access document need "reader" object of "indexreader" class 'reader' call method 'getvalues(' field name')' . return array of strings.
Comments
Post a Comment