本文整理汇总了Java中org.apache.lucene.search.Hits类的典型用法代码示例。如果您正苦于以下问题:Java Hits类的具体用法?Java Hits怎么用?Java Hits使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Hits类属于org.apache.lucene.search包,在下文中一共展示了Hits类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: searchIndex
import org.apache.lucene.search.Hits; //导入依赖的package包/类
/**
* 查询索引
*
* @param keywords
* @return
* @throws Exception
*/
public List<Document> searchIndex(Integer typeId, String keywords) throws Exception {
// 1.init searcher
Analyzer analyzer = new PaodingAnalyzer();
IndexReader reader = IndexReader.open(typeId == appConfig.getGameTypeId() ? appConfig.getGameIndexDir()
: appConfig.getSoftIndexDir());
BooleanClause.Occur[] flags = new BooleanClause.Occur[] { BooleanClause.Occur.SHOULD,
BooleanClause.Occur.SHOULD };
Query query = MultiFieldQueryParser.parse(keywords, appConfig.getQueryFields(), flags, analyzer);
query = query.rewrite(reader);
// 2.search
List<Document> docs = new ArrayList<Document>();
Hits hits = (typeId == appConfig.getGameTypeId() ? gameSearcher.search(query, Sort.RELEVANCE) : softSearcher
.search(query, Sort.RELEVANCE));// searcher.search(query,
// Sort.RELEVANCE);
for (int i = 0; i < hits.length(); i++) {
docs.add(hits.doc(i));
}
// 3.return
reader.close();
return docs;
}
示例2: addField
import org.apache.lucene.search.Hits; //导入依赖的package包/类
public boolean addField(String recID, String prefix, String value){
try {
Searcher searcher = new IndexSearcher(indexPath);
Query q=new TermQuery(new Term("ID",recID));
Hits hits=searcher.search(q);
if ((hits==null)||(hits.length()!=1)){
return false;
}
Document doc=hits.doc(0);
IndexWriter iw = getIndexWriter();
Field f=new Field(prefix,value, Field.Store.YES, Field.Index.UN_TOKENIZED, Field.TermVector.NO);
doc.add(f);
iw.updateDocument(new Term("ID", recID), doc);
} catch (IOException ex) {
log.fatal(ex);
return false;
}
return true;
}
示例3: addField
import org.apache.lucene.search.Hits; //导入依赖的package包/类
public boolean addField(String recID, String prefix, String value){
try {
Searcher searcher = new IndexSearcher(indexPath);
Query q=new TermQuery(new Term("ID",recID));
Hits hits=searcher.search(q);
if ((hits==null)||(hits.length()!=1)){
return false;
}
Document doc=hits.doc(0);
IndexWriter iw = getIndexWriter();
Field f=new Field(prefix,value, Field.Store.YES, Field.Index.UN_TOKENIZED, Field.TermVector.NO);
doc.add(f);
iw.updateDocument(new Term("ID", recID), doc);
iw.close();
} catch (IOException ex) {
log.fatal(ex);
return false;
}
return true;
}
示例4: deleteField
import org.apache.lucene.search.Hits; //导入依赖的package包/类
public void deleteField(String recID, String prefix, String value){
try {
Searcher searcher = new IndexSearcher(indexPath);
Query q=new TermQuery(new Term("ID",recID));
Hits hits=searcher.search(q);
if ((hits==null)||(hits.length()!=1)){
log.fatal("greska pri brisanju polja. Zapis: "+recID);
return ;
}
Document doc=hits.doc(0);
Field [] fields=doc.getFields(prefix);
IndexWriter iw = getIndexWriter();
doc.removeFields(prefix);
for(int i=0;i<fields.length;i++){
if(!fields[i].stringValue().equals(value)){
doc.add(fields[i]);
}
}
iw.updateDocument(new Term("ID", recID), doc);
iw.close();
} catch (IOException ex) {
log.fatal(ex);
}
}
示例5: verify
import org.apache.lucene.search.Hits; //导入依赖的package包/类
private void verify(Directory dir, int expectedHits) throws IOException {
IndexSearcher searcher = new IndexSearcher(dir);
Hits hits = searcher.search(new TermQuery(new Term("content", "apache")));
int numHits = hits.length();
assertEquals(expectedHits, numHits);
int[] docs = new int[numHits];
for (int i = 0; i < numHits; i++) {
Document hit = hits.doc(i);
docs[Integer.parseInt(hit.get("id"))]++;
}
for (int i = 0; i < numHits; i++) {
assertEquals(1, docs[i]);
}
searcher.close();
}
示例6: getProperty
import org.apache.lucene.search.Hits; //导入依赖的package包/类
private String getProperty(final String propertyName) throws IOException {
if (_dir == null) {
return null;
}
try {
return doReadOperation(new ReadOperation<String>() {
public String execute(final IndexReader reader, final Searcher searcher, final Analyzer analyzer) throws IOException, ParseException {
Hits hits = searcher.search(new TermQuery(new Term(FIELD_PROPERTY_KEY, propertyName)));
Iterator<?> iterator = hits.iterator();
if (iterator.hasNext()) {
return ((Hit) iterator.next()).get(FIELD_PROPERTY_VALUE);
}
return null;
}
}, false);
}
catch (QuerySyntaxException ex) {
throw new NoQueryPerformedException(ex);
}
}
示例7: docExists
import org.apache.lucene.search.Hits; //导入依赖的package包/类
/** Checks if a given document exists in the index.<br><br>
*
* @param key The key associated with the document in the index.
*
* @return true if a document was found, false if not.
*
*/
public boolean docExists(String key)
throws ParserConfigurationException, SAXException, IOException
{
// We need to find the docInfo chunk that contains the specified
// file. So construct a boolean query looking for a chunk with
// a "docInfo" field AND a "key" field containing the specified
// source file key.
//
BooleanQuery query = new BooleanQuery();
Term docInfo = new Term("docInfo", "1");
Term srcPath = new Term("key", key);
query.add(new TermQuery(docInfo), BooleanClause.Occur.MUST);
query.add(new TermQuery(srcPath), BooleanClause.Occur.MUST);
// Use the query to see if the document is in the index..
Hits match = indexSearcher.search(query);
// Let the caller know if we found a match.
return match.length() > 0;
}
示例8: performSearch
import org.apache.lucene.search.Hits; //导入依赖的package包/类
public Hits performSearch(Directory dir, String query, boolean useMust)
throws Exception {
NGramQueryParser parser = new NGramQueryParser("name",
new NGramAnalyzer(min_ngram, max_ngram), useMust);
IndexSearcher searcher = new IndexSearcher(dir);
Query q = parser.parse(query);
Hits hits = searcher.search(q);
log.info("Original Query = " + query);
log.info("Parsed Query = " + q.toString());
log.info("Hits.length() = " + hits.length());
for (int i=0; i < hits.length(); i++) {
log.debug("Document<"+hits.id(i)+"> = " + hits.doc(i));
//Explanation explain = searcher.explain(q, hits.id(i));
//log.debug("explain = " + explain.toString());
}
return hits;
}
示例9: thresholdHits
import org.apache.lucene.search.Hits; //导入依赖的package包/类
protected int thresholdHits(Hits hits) throws IOException {
/** We could consider doing thresholding as a relative thing...
* instead of checking against an absolute value, we grab top score
* then filter based on difference from that...
*/
int counter = 0;
for (int i=0; i < hits.length(); i++) {
if (hits.score(i) >= score_threshold) {
counter++;
}
else {
break;
}
}
return counter;
}
示例10: debugExplainResults
import org.apache.lucene.search.Hits; //导入依赖的package包/类
private void debugExplainResults(String indexName, Hits hits, IndexSearcher searcher,
Query q, Set<Term> queryTerms)
throws IOException {
log.debug("Parsed Query is " + q.toString());
log.debug("Looking at index: " + indexName);
for (int i = 0; i < hits.length(); i++) {
if ((i < 10)) {
Document doc = hits.doc(i);
Float score = hits.score(i);
Explanation ex = searcher.explain(q, hits.id(i));
log.debug("Looking at hit<" + i + ", " + hits.id(i) + ", " + score +
">: " + doc);
log.debug("Explanation: " + ex);
MatchingField match = new MatchingField(q.toString(), doc, queryTerms);
String fieldName = match.getFieldName();
String fieldValue = match.getFieldValue();
log.debug("Guessing that matched fieldName is " + fieldName + " = " +
fieldValue);
}
}
}
示例11: doPost
import org.apache.lucene.search.Hits; //导入依赖的package包/类
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String query = request.getParameter("query");
if (query != null && query.length() > 0) {
HttpSession session = request.getSession();
SearchService searchService = (SearchService) snipsnap.api.container.Components.getComponent(SearchService.class);
Hits hits = searchService.search(query);
session.setAttribute("query", query);
session.setAttribute("hits", hits);
session.setAttribute("startIndex", new Integer(0));
RequestDispatcher dispatcher = request.getRequestDispatcher("/exec/search.jsp");
dispatcher.forward(request, response);
return;
}
Configuration config = Application.get().getConfiguration();
response.sendRedirect(config.getUrl());
}
示例12: skynetsearch
import org.apache.lucene.search.Hits; //导入依赖的package包/类
public Hits skynetsearch(String query, String Field, String indexPath) {
String indexfield = Field + ":";
String querytext = indexfield + query.trim();
Hits result = null;
try {
String[] search_fields = {Field};
//String indexPath = StorageHandler.GetDocIndexPath();
IndexSearcher searcher = new IndexSearcher(indexPath);
KeywordAnalyzer analyzer = new KeywordAnalyzer();
Query lucenequery = MultiFieldQueryParser.parse(query,
search_fields, analyzer);
// QueryParser queryparse = new QueryParser(query,analyzer);
// Query lucenequery = queryparse.parse(querytext);
result = searcher.search(lucenequery);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception ex) {
System.out.println(ex + "");
}
return result;
}
示例13: skynetsearchMulti
import org.apache.lucene.search.Hits; //导入依赖的package包/类
public Hits skynetsearchMulti(String query, String[] Field, String indexPath) {
Hits result = null;
try {
IndexSearcher searcher = new IndexSearcher(indexPath);
KeywordAnalyzer analyzer = new KeywordAnalyzer();
MultiFieldQueryParser multiparser = new MultiFieldQueryParser(Field, analyzer);
multiparser.setDefaultOperator(QueryParser.Operator.OR);
Query lucenequery = multiparser.parse(query);
result = searcher.search(lucenequery);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception ex) {
System.out.println(ex + "");
}
return result;
}
示例14: updateDocument
import org.apache.lucene.search.Hits; //导入依赖的package包/类
public void updateDocument(String documentId, String fieldName,
String fieldValue) {
try {
SearchBean sbean = new SearchBean();
Hits hresult = sbean.skynetsearch(documentId, "DocumentId");
Document doc = hresult.doc(0);
ArrayList<DocumentFields> docfields = new ArrayList<DocumentFields>();
for (int i = 0; i < fields.length; i++) {
DocumentFields docFields = new DocumentFields();
docFields.SetFieldName(fields[i]);
if (fields[i].equalsIgnoreCase(fieldName)) {
docFields.SetFieldValue(fieldValue);
} else {
docFields.SetFieldValue(doc.get(fields[i]));
}
docfields.add(docFields);
}
DeleteIndex(documentId);
CreateIndex(docfields);
} catch (Exception ex) {
System.out.print(ex.toString());
}
}
示例15: search
import org.apache.lucene.search.Hits; //导入依赖的package包/类
public Hits search(String type, String qstr)
{
String queryStr = "(" + qstr + ") OR ("+TStransformer.STtransform(qstr)+")";
Hits hits = null;
try{
FSDirectory fsd = FSDirectory.getDirectory(QuickSearchConfig.getIndexDir() + "/" + type);
//RAMDirectory ramd = new RAMDirectory(QuickSearchConfig.getIndexDir() + "/" + type);
IndexSearcher indexSearcher = new IndexSearcher(fsd);
Query query = MultiFieldQueryParser.parse(queryStr, fields, flags, analyzer);
hits = indexSearcher.search(query);
}catch(Exception ex){
ex.printStackTrace();
}
return hits;
}