本文整理汇总了Python中lucene.IndexSearcher.close方法的典型用法代码示例。如果您正苦于以下问题:Python IndexSearcher.close方法的具体用法?Python IndexSearcher.close怎么用?Python IndexSearcher.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lucene.IndexSearcher
的用法示例。
在下文中一共展示了IndexSearcher.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: displayResults
# 需要导入模块: from lucene import IndexSearcher [as 别名]
# 或者: from lucene.IndexSearcher import close [as 别名]
def displayResults(self, query, sort):
searcher = IndexSearcher(self.directory, True)
fillFields = False
computeMaxScore = False
docsScoredInOrder = False
computeScores = True
collector = TopFieldCollector.create(sort, 20,
fillFields,
computeScores,
computeMaxScore,
docsScoredInOrder)
searcher.search(query, None, collector)
scoreDocs = collector.topDocs().scoreDocs
print "\nResults for:", query, "sorted by", sort
print "Title".rjust(30), "pubmonth".rjust(10), \
"id".center(4), "score".center(15)
for scoreDoc in scoreDocs:
doc = searcher.doc(scoreDoc.doc)
title = doc["title"]
if len(title) > 30:
title = title[:30]
print title.encode('ascii', 'replace').rjust(30), \
doc["pubmonth"].rjust(10), \
str(scoreDoc.doc).center(4), \
("%06f" % (scoreDoc.score)).rjust(12)
print " ", doc["category"]
# print searcher.explain(query, scoreDoc.doc)
searcher.close()
示例2: search
# 需要导入模块: from lucene import IndexSearcher [as 别名]
# 或者: from lucene.IndexSearcher import close [as 别名]
def search(r, keyword=""):
import logging
logger = logging.getLogger("search")
bench = Benchmark(logger)
from lucene import IndexSearcher, StandardAnalyzer, FSDirectory, QueryParser, File, Hit
import lucene, os
os.environ["JAVA_HOME"] = "/usr/local/jdk1.6.0_17"
lucene.initVM(lucene.CLASSPATH)
directory = FSDirectory.open(File(CONFIG.INDEX_PATH))
ROBOT_INDEX = IndexSearcher(directory, True)
ROBOT_ANALYZER = StandardAnalyzer()
keyword = keyword or r.GET["keyword"]
query = QueryParser("context", ROBOT_ANALYZER)
query = query.parse('"%s"' % keyword)
bench.start_mark("search")
hits = ROBOT_INDEX.search(query)
count = len(hits)
result = []
i = 0
for hit in hits:
i += 1
if i > 100:
break
doc = Hit.cast_(hit).getDocument()
result.append(SearchResult(doc, i, keyword))
ROBOT_INDEX.close()
et = bench.stop_mark()
return render_to_response("robot_search_result.html", {"result": result, "count": count, "elaspe": et})
示例3: testTermRangeQuery
# 需要导入模块: from lucene import IndexSearcher [as 别名]
# 或者: from lucene.IndexSearcher import close [as 别名]
def testTermRangeQuery(self):
searcher = IndexSearcher(self.directory, True)
query = TermRangeQuery("title2", "d", "j", True, True)
topDocs = searcher.search(query, 100)
self.assertEqual(3, topDocs.totalHits)
searcher.close()
示例4: main
# 需要导入模块: from lucene import IndexSearcher [as 别名]
# 或者: from lucene.IndexSearcher import close [as 别名]
def main(cls, argv):
if len(argv) != 2:
print "Usage: BerkeleyDbSearcher <index dir>"
return
dbHome = argv[1]
env = DBEnv()
env.set_flags(DB_LOG_INMEMORY, 1);
if os.name == 'nt':
env.set_cachesize(0, 0x4000000, 1)
elif os.name == 'posix':
from commands import getstatusoutput
if getstatusoutput('uname') == (0, 'Linux'):
env.set_cachesize(0, 0x4000000, 1)
env.open(dbHome, (DB_THREAD |
DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN), 0)
index = DB(env)
blocks = DB(env)
txn = None
try:
txn = env.txn_begin(None)
index.open(filename = '__index__', dbtype = DB_BTREE,
flags = DB_THREAD, txn = txn)
blocks.open(filename = '__blocks__', dbtype = DB_BTREE,
flags = DB_THREAD, txn = txn)
except:
if txn is not None:
txn.abort()
txn = None
raise
else:
txn.commit()
txn = None
try:
txn = env.txn_begin(None)
directory = DbDirectory(txn, index, blocks, 0)
searcher = IndexSearcher(directory, True)
topDocs = searcher.search(TermQuery(Term("contents", "fox")), 50)
print topDocs.totalHits, "document(s) found"
searcher.close()
except:
if txn is not None:
txn.abort()
txn = None
raise
else:
txn.abort()
index.close()
blocks.close()
env.close()
示例5: getHitCount
# 需要导入模块: from lucene import IndexSearcher [as 别名]
# 或者: from lucene.IndexSearcher import close [as 别名]
def getHitCount(self, fieldName, searchString):
searcher = IndexSearcher(self.dir, True)
t = Term(fieldName, searchString)
query = TermQuery(t)
hitCount = len(searcher.search(query, 50).scoreDocs)
searcher.close()
return hitCount
示例6: GET
# 需要导入模块: from lucene import IndexSearcher [as 别名]
# 或者: from lucene.IndexSearcher import close [as 别名]
def GET(self):
form1 = login()
user_data = web.input()
vm_env.attachCurrentThread()
STORE_DIR = "F:\\imgindex"
directory = SimpleFSDirectory(File(STORE_DIR))
searcher = IndexSearcher(directory, True)
analyzer = SimpleAnalyzer(Version.LUCENE_CURRENT)
a,b,c,d,e = img_func(user_data.keyword,searcher,analyzer)
searcher.close()
return render.img_result(form1,a,b,c,d,e)
示例7: testExclusive
# 需要导入模块: from lucene import IndexSearcher [as 别名]
# 或者: from lucene.IndexSearcher import close [as 别名]
def testExclusive(self):
searcher = IndexSearcher(self.directory, True)
# pub date of TTC was October 1988
query = NumericRangeQuery.newIntRange("pubmonth",
198805,
198810,
False, False)
topDocs = searcher.search(query, 100)
self.assertEqual(0, topDocs.totalHits)
searcher.close()
示例8: LuceneSearch
# 需要导入模块: from lucene import IndexSearcher [as 别名]
# 或者: from lucene.IndexSearcher import close [as 别名]
class LuceneSearch(object):
def __init__(self):
STORE_DIR = "index"
initVM()
print 'lucene', VERSION
self.directory = SimpleFSDirectory(File(STORE_DIR))
print self.directory
self.searcher = IndexSearcher(self.directory, True)
self.analyzer = StandardAnalyzer(Version.LUCENE_CURRENT)
def close(self):
self.searcher.close()
def raw_search(self, query_string):
query = QueryParser(Version.LUCENE_CURRENT, "contents",
self.analyzer).parse(query_string)
scoreDocs = self.searcher.search(query, 50).scoreDocs
print "%s total matching documents." % len(scoreDocs)
matches = []
for scoreDoc in scoreDocs:
doc = self.searcher.doc(scoreDoc.doc)
#print 'doc matched = ', dir(doc)
contents = LuceneDoc.load(doc.get('name'))
matches.append({'contents' : contents, 'doc' : doc})
return matches
def search(self, query):
matches = self.raw_search(query)
results = ''
if len(matches) > 0:
results += str(len(matches))+" results <br/>"
for match in matches:
results += '<a href='+str(match['contents']['dealUrl'])+'>'+str(match['contents']['merchant'])+'</a><br />'
results += '<p>'+str(match['contents']['shortAnnouncementTitle'])+','+str(match['contents']['redemptionLocation'])+'</p><br/>'
else:
results = "0 results <br/>"
return results
def cli_search(self):
while True:
print
print "Hit enter with no input to quit."
command = raw_input("Query:")
if command == '':
return
matches = self.raw_search(command)
print
print "Searching for:", command
for match in matches:
print match['contents']['dealUrl']
print match['contents']['merchant'], ',', match['contents']['redemptionLocation'], ', ', match['contents']['div']
print match['contents']['shortAnnouncementTitle']
print '-'*80
示例9: begining
# 需要导入模块: from lucene import IndexSearcher [as 别名]
# 或者: from lucene.IndexSearcher import close [as 别名]
def begining(command):
STORE_DIR = "index"
global vm_env
vm_env = initVM()
vm_env.attachCurrentThread()
#print 'lucene', VERSION
directory = SimpleFSDirectory(File(STORE_DIR))
searcher = IndexSearcher(directory, True)
analyzer = lucene.WhitespaceAnalyzer(Version.LUCENE_CURRENT)
a = run(command, searcher, analyzer)
searcher.close()
return a
示例10: testTerm
# 需要导入模块: from lucene import IndexSearcher [as 别名]
# 或者: from lucene.IndexSearcher import close [as 别名]
def testTerm(self):
searcher = IndexSearcher(self.directory, True)
t = Term("subject", "ant")
query = TermQuery(t)
scoreDocs = searcher.search(query, 50).scoreDocs
self.assertEqual(1, len(scoreDocs), "JDwA")
t = Term("subject", "junit")
scoreDocs = searcher.search(TermQuery(t), 50).scoreDocs
self.assertEqual(2, len(scoreDocs))
searcher.close()
示例11: testCollecting
# 需要导入模块: from lucene import IndexSearcher [as 别名]
# 或者: from lucene.IndexSearcher import close [as 别名]
def testCollecting(self):
query = TermQuery(Term("contents", "junit"))
searcher = IndexSearcher(self.directory, True)
collector = BookLinkCollector(searcher)
searcher.search(query, collector)
links = collector.getLinks()
self.assertEqual("java development with ant",
links["http://www.manning.com/antbook"])
scoreDocs = searcher.search(query, 10).scoreDocs
self.dumpHits(searcher, scoreDocs)
searcher.close()
示例12: run
# 需要导入模块: from lucene import IndexSearcher [as 别名]
# 或者: from lucene.IndexSearcher import close [as 别名]
def run(command):
if command == '':
return None
STORE_DIR = "index"
initVM(CLASSPATH)
directory = FSDirectory.getDirectory(STORE_DIR, False)
searcher = IndexSearcher(directory)
analyzer = StandardAnalyzer()
parser = QueryParser("contents", analyzer)
parser.setDefaultOperator(QueryParser.Operator.AND)
parser.setFuzzyMinSim(0.2)
query = parser.parse(command)
hits = map(transform, searcher.search(query))
searcher.close()
return hits
示例13: search_lucene_index
# 需要导入模块: from lucene import IndexSearcher [as 别名]
# 或者: from lucene.IndexSearcher import close [as 别名]
def search_lucene_index(search_params, index_dir, index_metadata, records_per_page):
"""
Uses the query term provided to search the disease ontology lucene index
"""
results = []
index_dir = SimpleFSDirectory(File(index_dir))
analyzer = build_perfield_analyzer(index_metadata)
searcher = IndexSearcher(index_dir)
index_fields = index_metadata.keys()
# Since we are paging results we want to grab what page we are on
page = (int(search_params.get('page', 1))) - 1
# Doing something pretty hacky here since we are trying to move from 0-based to 1
# based indexing to match our pagingation display
offset = int(page) * records_per_page
# If we are executing an advanced search we will be building a BooleanQuery
# in parts as opposed to the one MultiFieldQueryParser when doing a basic
# serach
query = None
if search_params.get('adv_search') == "True":
query = build_advanced_search_query(search_params, search_params.get('operator'), analyzer)
else:
parser = MultiFieldQueryParser(Version.LUCENE_CURRENT, index_fields, analyzer)
query = MultiFieldQueryParser.parse(parser, process_query_param((search_params.get('q'))))
# Parse through our hits
hits = searcher.search(query, 10000)
total_hits = hits.totalHits
count = min(hits.totalHits - offset, records_per_page)
for i in xrange(0, count):
score_doc = hits.scoreDocs[offset+i]
doc = searcher.doc(score_doc.doc)
term_id = doc.get('term id')
name = doc.get('name')
explain = searcher.explain(query, score_doc.doc)
match_fields = get_field_matches( explain.toString(), index_fields )
results.append( (term_id, name, list(match_fields)) )
searcher.close()
return (results, total_hits)
示例14: doSearch
# 需要导入模块: from lucene import IndexSearcher [as 别名]
# 或者: from lucene.IndexSearcher import close [as 别名]
def doSearch(self,searchString,fieldToSearch,luceneDir):
searchResult =[]
store = SimpleFSDirectory(File(luceneDir))
analyzer = StandardAnalyzer(Version.LUCENE_CURRENT)
searcher = IndexSearcher(store)
query = QueryParser(Version.LUCENE_CURRENT,fieldToSearch,analyzer).parse(searchString)
hits = searcher.search(query,self.MAX)
print "Found %d documents that matched the query '%s'" %(hits.totalHits,searchString)
for hit in hits.scoreDocs:
doc = searcher.doc(hit.doc)
#docdict['score'] = hit.score
#docdict['docid'] = hit.doc
#docdict['content'] = doc.get("contents").encode("utf-8")
searchResult.append([doc.get("title").encode("utf-8"),doc.get("contents").encode("utf-8")])
searcher.close()
return searchResult
示例15: delete_old
# 需要导入模块: from lucene import IndexSearcher [as 别名]
# 或者: from lucene.IndexSearcher import close [as 别名]
def delete_old(self, index):
existing_ids = set([book.id for book in Book.objects.all()])
reader = IndexReader.open(index.index, False)
searcher = IndexSearcher(reader)
try:
num = searcher.docFreq(Term('is_book', 'true'))
docs = searcher.search(Search.make_term_query(['true'], 'is_book'), num)
for result in docs.scoreDocs:
stored = searcher.doc(result.doc)
book_id = int(stored.get('book_id'))
if not book_id in existing_ids:
print "book id %d doesn't exist." % book_id
index.remove_book(book_id)
finally:
searcher.close()
reader.close()