我正在使用 solr api 开发自定义请求处理程序。作为参考,我将“MoreLikeThis”作为参考。
我可以使用“getDocList”API 访问文档,然后获取“迭代器”的对象,但它只给了我文档 ID。
如何访问文档的每个字段?
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 am in My Handler");
/**
* Working with core
*/
SolrCore core = req.getCore();
System.out.println("Core is " + "" + core.getDescription());
System.out.println("Data directory is " + "" + core.getDataDir());
System.out.println("Index directory is " + "" + core.getIndexDir());
/**
* Working with 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 over keys
for (String str : map.keySet()){
System.out.println(str);
}
// Iterate over SchemaField
for (SchemaField schemaFieldObj: map.values()){
System.out.println(schemaFieldObj.isRequired());
}
/**
* Find the Unique Key Field
*/
field = schemaObj.getUniqueKeyField();
System.out.println(field.getName());
System.out.println(schemaObj.getQueryParserDefaultOperator());
/**
* Working with 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 with QParser
*/
QParser queryParser = QParser.getParser(q, defType, req);
Query query = queryParser.getQuery();
/**
* Now get searcher
*/
SolrIndexSearcher indexSearcher = req.getSearcher();
DocList matchDocs = indexSearcher.getDocList(query, null, null, 1, 100000,10000);
// Number of documents matched
System.out.println(matchDocs.matches());
// Get Iterator
// get documentreader
Reader reader;
IndexReader readDoc;
Document doc;
DocIterator iterator = matchDocs.iterator();
while (iterator.hasNext()){
Integer id = iterator.nextDoc();
System.out.println("Here I am " + id);
/* HOW TO ACCESS ALL FIELDS OF DOCUMENT */
}
}
@Override
public String getDescription() {
return "My Search Handler";
}
@Override
public String getSource() {
return "$Source$";
}
@Override
public String getVersion() {
return "$Revision$";
}
}