예제 #1
0
class SearchIndex(object):
    """docstring for SearchIndex"""
    def __init__(self):
        self.schema = schema
        self.collect = MongoDB()

    def create_index(self):
        if not os.path.exists("index"):
            os.mkdir("index")
            index = create_in("index", schema)
            writer = index.writer()
            for post in self.collect.find():
                writer.update_document(title=post['title'],
                                        content=post['content'],
                                        nid=unicode(post["_id"]),
                                        url=post['url'])
            writer.commit()

    def get_index_info(self, query):
        index = open_dir("index")
        with index.searcher() as searcher:
            parser = QueryParser("content", index.schema)
            my_query = parser.parse(query)
            result = []
            resp = searcher.search(my_query, limit=None)
            if resp:
                for one in resp:
                    post = self.collect.find_one(ObjectId(one["nid"]))
                    result.append({"title":one['title'], "url":post['url'], "content":one.highlights("content")})
                    # return one['title']
                    # print one["title"]
                    # print post['url']
                    # print one.highlights("content")
                    # print "###############"
            return result, len(result)
예제 #2
0
# req - create collection with validation
m0.create_collection(collection_name='collection000')
status(m0.get_current_status(), 'creating collection')
# 2
m = MongoDB(database_name='try002', collection_name='collection002')
status(m.get_current_status(), 'creating collection - part 2')
# req - insert a value
m.insert(get_dummy_data())
status(m.insert(get_dummy_data()), msg=' Insert one')
# req - insert many
status(m.insert_many([get_dummy_data() for _ in range(10)]), msg=' Insert Many ')

# req - find/query
status(m.find_one({'author': 'sR4aSZ5q'}), msg=' find one')
status(m.find(), msg=' find many')
# req - count
status(m.find(count=True), msg='count')
print('Count again - ', m.count())
# req - drop collection by name
status(m.drop_collection(), msg='drop collection')
# m.find()      # Raise Exception Error

# req - update

# req - insert/bulk write

# req - create index

# req - create indexs
예제 #3
0
 def save_to_mongo(self,dic):
         mon = MongoDB()
         if not mon.find({'内容':self.weibo_content}):
             mon.insert(dic)
예제 #4
0
 def save_to_mongo(self, dic):
     mon = MongoDB()
     if not mon.find({'内容': self.weibo_content}):
         mon.insert(dic)