Changeset 8205 for mauRepo


Ignore:
Timestamp:
01/04/12 18:07:50 (8 years ago)
Author:
mnagni
Message:

Complete - # 22416: CEDA MOLES3 - search function - by title keyword
 http://team.ceda.ac.uk/trac/ceda/ticket/22416

Location:
mauRepo/MolesManager/trunk/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • mauRepo/MolesManager/trunk/src/MolesManager/moles3epb.py

    r8203 r8205  
    1313from ea_model.iso_19115_2006_metadata_corrigendum.citation_and_responsible_party_information.ci_citation import CI_Citation 
    1414from ea_model.moles3_4.observationcollection.mo_observationcollection import MO_ObservationCollection 
     15from ea_model.moles3_4.observation.mo_observation import MO_Observation 
    1516 
    1617#molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT) 
     
    6364        intSession = Moles3EPB._getSession(session) 
    6465        return intSession.query(CEDA_ObservationCollection, CEDA_Observation).filter(CEDA_ObservationCollection.id==obs_coll_id).filter(CEDA_Observation.id==obs_id).count() > 0 
     66 
     67    @classmethod 
     68    def extractObservationByTitleKeywords(self, keywords, session = None): 
     69        """ 
     70            Loooks for CEDA_Observation containing a specific title (observation.identifier.code) 
     71            @param i_title: a sspace separated terms string 
     72            @return: a tuple containing a CEDA_Observation satisfying the queryllection.idenfitier element having the title   
     73        """                 
     74        intSession = Moles3EPB._getSession(session) 
     75        # search_vector is a ts_vector column. To search for terms, you use the  
     76        # @@ operator. plainto_tsquery turns a string into a query that can be  
     77        # used with @@. So this adds a where clause like "WHERE search_vector  
     78        # @@ plaint_tsquery(<search string>)" 
     79        q = intSession.query(CEDA_Observation). \ 
     80            join(MO_Observation).join(MO_ObservationCollection.identifier). \ 
     81            filter('md_identifier.code_search_vector @@ to_tsquery(:terms)') 
     82        # This binds the :terms placeholder to the searchterms string. User input  
     83        # should always be put into queries this way to prevent SQL injection. 
     84        q = q.params(terms=keywords) 
     85        return q 
     86 
    6587 
    6688    @classmethod 
  • mauRepo/MolesManager/trunk/src/MolesManager/molesSessionMiddleware.py

    r8155 r8205  
    1111from libs.commons_db import DbManager 
    1212from libs.migration.MigrationEPB import MigrationEPB 
     13from ea_model.iso_19115_2006_metadata_corrigendum.reference_system_information.md_identifier import MD_Identifier 
    1314 
    1415 
     
    2122     
    2223 
     24    def _initSearchIndexes(self, db_manager): 
     25        #To Be Done - CHECK IF THE COLUMN ALREADY EXISTS! 
     26        # We don't want sqlalchemy to know about this column so we add it externally. 
     27        try: 
     28            db_manager._engine.execute("alter table md_identifier add column code_search_vector tsvector")                  
     29 
     30            # This indexes the tsvector column 
     31            db_manager._engine.execute("create index md_identifier_code_search_index on md_identifier using gin(code_search_vector)")             
     32        except Exception as e: 
     33            pass 
     34 
     35        # This sets up the trigger that keeps the tsvector column up to date. 
     36        db_manager._engine.execute("create trigger md_identifier_code_search_update before update or insert on md_identifier \ 
     37            for each row execute procedure tsvector_update_trigger('code_search_vector', 'pg_catalog.english', 'code')") 
     38 
    2339     
    2440    def _getMolesSession(self): 
     
    3046            Moles3EPB.overrrideDBManager(molesDB) 
    3147            MolesSessionMiddleware._epbInitialized = True 
     48             
     49            self._initSearchIndexes(molesDB) 
     50             
    3251            return Moles3EPB.getNewMolesSession() 
    3352        else: 
  • mauRepo/MolesManager/trunk/src/libs/migration/tests/moles3epbmothods.py

    r8198 r8205  
    1111import logging 
    1212from logging import StreamHandler 
    13 from ea_model.iso_19115_2006_metadata_corrigendum.reference_system_information.md_identifier import MD_Identifier 
    14 from test_utils import createObservationCollection 
    15 from ea_model.ceda_metadatamodel.ceda_observationcollection.ceda_observationcollection import CEDA_ObservationCollection 
    16 from ea_model.moles3_4.observationcollection.mo_observationcollection import MO_ObservationCollection 
    17 from ea_model.iso_19115_2006_metadata_corrigendum.citation_and_responsible_party_information.ci_citation import CI_Citation 
     13from test_utils import createObservationCollection, createObservation 
     14from sqlalchemy.dialects import postgres 
    1815 
    1916 
     
    2320        Moles3EPBTest.molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT) 
    2421        Moles3EPB.overrrideDBManager(Moles3EPBTest.molesDB) 
     22        self._initSearchIndexes(Moles3EPBTest.molesDB)         
    2523        self.logging = logging.getLogger('Moles3EPBTest') 
    2624        self.logging.addHandler(StreamHandler()) 
     
    3230 
    3331    def runTest(self): 
    34         self.checkExtractCollectionIdentifierByTitle()         
     32        self.checkExtractCollectionIdentifierByTitle()    
     33        self.checkExtractObservationByTitleKeywords()  
    3534 
    3635    def checkExtractCollectionIdentifierByTitle(self): 
     
    5150        session.commit() 
    5251 
     52    def checkExtractObservationByTitleKeywords(self): 
     53        session = Moles3EPB.getNewMolesSession(); 
     54        observation = createObservation() 
     55         
     56        self.logging.info('Stores an new CEDA_Observation') 
     57        Moles3EPB.addCedaObject(observation, session) 
     58        session.commit() 
     59         
     60        obs = Moles3EPB.extractObservationByTitleKeywords('test_code', session) 
     61        self.assertTrue(obs.count() > 1, "Cannot find a CEDA_Observation") 
     62        obs = Moles3EPB.extractObservationByTitleKeywords('dummy_code', session) 
     63        self.assertTrue(obs.count() == 0, "Should NOT find a CEDA_Observation")         
     64 
     65    def _initSearchIndexes(self, db_manager): 
     66        #To Be Done - CHECK IF THE COLUMN ALREADY EXISTS! 
     67        # We don't want sqlalchemy to know about this column so we add it externally. 
     68        try: 
     69            db_manager._engine.execute("alter table md_identifier add column code_search_vector tsvector")                  
     70 
     71            # This indexes the tsvector column 
     72            db_manager._engine.execute("create index md_identifier_code_search_index on md_identifier using gin(code_search_vector)") 
     73             
     74            # This sets up the trigger that keeps the tsvector column up to date. 
     75            db_manager._engine.execute("create trigger md_identifier_code_search_update before update or insert on md_identifier \ 
     76                for each row execute procedure tsvector_update_trigger('code_search_vector', 'pg_catalog.english', 'code')")             
     77        except Exception as e: 
     78            pass 
     79 
     80 
     81 
    5382if __name__ == "__main__": 
    5483    #import sys;sys.argv = ['', 'Test.testName'] 
  • mauRepo/MolesManager/trunk/src/libs/migration/tests/test_utils.py

    r8198 r8205  
    77from libs.migration.processor.commons import createMD_Identifier,\ 
    88    createCI_Citation 
     9from ea_model.ceda_metadatamodel.ceda_observation.ceda_observation import CEDA_Observation 
    910 
    1011def createObservationCollection(): 
     
    1920    observationCollection.identifier.append(i_identifier)       
    2021    return observationCollection 
     22 
     23def createObservation(): 
     24    ''' 
     25        Creates a CEDA_Observation with the following data: 
     26        oc.identifier[0].code = 'test_code'         
     27        oc.identifier[0].authority.title = 'test_title' 
     28        oc.identifier[1].code = 'mau_code'         
     29        oc.identifier[1].authority.title = 'mau_title'         
     30    ''' 
     31    observation = CEDA_Observation() 
     32    i_identifier = createMD_Identifier(code = 'test_code', authority=createCI_Citation('test_title')) 
     33    observation.identifier.append(i_identifier)       
     34    i_identifier = createMD_Identifier(code = 'mau_code', authority=createCI_Citation('mau_title')) 
     35    observation.identifier.append(i_identifier)   
     36    observation.dataLineage = "test_dataLineage"         
     37    return observation 
Note: See TracChangeset for help on using the changeset viewer.