Exemple #1
0
    def searchDocuments(self, version, query, attribute=None):

        directory = DbDirectory(self.store.txn, self._db,
                                self.store._blocks._db, self._flags)
        searcher = IndexSearcher(directory)
        query = QueryParser.parse(query, "contents", StandardAnalyzer())

        docs = {}
        for i, doc in searcher.search(query):
            ver = long(doc['version'])
            if ver <= version:
                uuid = UUID(doc['owner'])
                dv = docs.get(uuid, None)
                if dv is None or dv[0] < ver:
                    docAttr = doc['attribute']
                    if attribute is None or attribute == docAttr:
                        docs[uuid] = (ver, docAttr)

        searcher.close()

        return docs
Exemple #2
0
  results = []

  for i, doc in hits:
    results.append([doc.get("name"), doc.get("owner").encode('gbk'), doc.get("title").encode('gbk')])
  
  # sort result
  results.sort(lambda x,y: cmp(x[0],y[0]))    
  for name,owner,title in results:
    print name, owner, title 

def test_fixture():
  global BOARDSPATH
  BOARDSPATH = './'

if __name__ == '__main__':
  #test_fixture()

  board = sys.argv[1]
  querystr = sys.argv[2].decode('gbk').strip()
  
  path = BOARDSPATH+board+'/'+RECENT_INDEX
  if not os.path.exists(path) or len(querystr) == 0:
    sys.exit(-1)
  directory = FSDirectory.getDirectory(path, False)
  searcher = IndexSearcher(directory)
  analyzer = StandardAnalyzer()
  run(searcher, analyzer, querystr)
  searcher.close()
    
'path' and 'name' fields for each of the hits it finds in the index.  Note that
search.close() is currently commented out because it causes a stack overflow in
some cases.
"""
def run(searcher, analyzer):

    while True:
        print
        print "Hit enter with no input to quit."
        command = raw_input("Query:")
        if command == '':
            return

        print
        print "Searching for:", command
        query = QueryParser("contents", analyzer).parse(command)
        hits = searcher.search(query)
        print "%s total matching documents" % hits.length()
        
        for i, doc in hits:
            print 'path:', doc.get("path"), 'name:', doc.get("name"), 100*hits.score(i)

if __name__ == '__main__':
    STORE_DIR = "index"
    print 'PyLucene', VERSION, 'Lucene', LUCENE_VERSION
    directory = FSDirectory.getDirectory(STORE_DIR, False)
    searcher = IndexSearcher(directory)
    analyzer = StandardAnalyzer()
    run(searcher, analyzer)
    searcher.close()
class SearchAppService:

    def __init__(self, directory, shaManager):
        self._directory = directory
        self._searcher = IndexSearcher(self._directory)
        self._shaManager = shaManager

    def _prepareSearcher(self):
        # TODO: test index.currentVersion to update searcher,
        # allowing concurrence in search and write
        if self._searcher:
            self._searcher.close()
        self._searcher = IndexSearcher(self._directory)

    def search(self, query):
        print "Searching ", query
        import re
        if  re.match("^[a-f0-9]{40}$", query):
            return self.searchBySHA(query)
        elif query.startswith("http://"):
            return self.searchByURI(query)
        elif '@' in query:
            if not query.startswith('mailto'):
                query = "mailto:" + query
            return self.searchBySHA(sha.new(query).hexdigest())
        else:
            return self.searchByName(query)

    def searchByURI(self, query):
        #print "Searching by URI"
        query = "\"" + query + "\""
        parser = QueryParser("uri", KeywordAnalyzer())
        return self._performSearch(parser, query)

    def searchByName(self, query):
        #print "Preguntando por nombre"
        parser = QueryParser("name", StandardAnalyzer())
        return self._performSearch(parser, query)

    def searchBySHA(self, query):
        print "Preguntando por SHA"
        uris = self._shaManager.searchSha(query)
        if uris == None or len(uris) == 0 :
            return []

        def rebuildFoafs(uri):
            foaf = self.searchByURI(uri)
            if foaf == []:
                return [{'sha':query, 'uri':uri}]
            else:
                return foaf
        return reduce(operator.concat, map(rebuildFoafs, uris))

    def _performSearch(self, queryParser,query):
        q = queryParser.parse(query)

        self._prepareSearcher()
        hits = self._searcher.search(q)
        result = []
        for i in range(0, hits.length()):
            d = hits.doc(i)
            result.append(FoafDocumentFactory.getFOAFFromDocument(d))
        return result

    def close(self):
        self._shaManager.close()