(self)
return f'Saved search to ID: {self._resource_id}'
Save the search in the database.

        Returns:
            String with the identification of the saved search.

        Raises:
            ValueError: if there are values missing in order to save the query.
            RuntimeError: if the search could not be saved.
Save the search in the database.
Save the search in the database .
def save(self):
        """Save the search in the database.

        Returns:
            String with the identification of the saved search.

        Raises:
            ValueError: if there are values missing in order to save the query.
            RuntimeError: if the search could not be saved.
        """
        if not self.name:
            raise ValueError(
                'No name for the query saved. Please select a name first.')

        if not (self.query_string or self.query_dsl):
            raise ValueError(
                'Need to have either a query DSL or a query string to be '
                'able to save the search.')

        if not self.description:
            logger.warning(
                'No description selected for search, saving without one')

        if self._resource_id:
            resource_url = (
                f'{self.api.api_root}/sketches/{self._sketch.id}/views/'
                f'{self._resource_id}/')
        else:
            resource_url = (
                f'{self.api.api_root}/sketches/{self._sketch.id}/views/')

        query_filter = self.query_filter
        if self.return_fields:
            sketch_data = self._sketch.data
            sketch_meta = sketch_data.get('meta', {})
            mappings = sketch_meta.get('mappings', [])

            use_mappings = []
            for field in self.return_fields.split(','):
                field = field.strip().lower()
                for map_entry in mappings:
                    if map_entry.get('field', '').lower() == field:
                        use_mappings.append(map_entry)
            query_filter['fields'] = use_mappings

        data = {
            'name': self.name,
            'description': self.description,
            'query': self.query_string,
            'filter': query_filter,
            'dsl': self.query_dsl,
            'labels': json.dumps(self.labels),
        }
        response = self.api.session.post(resource_url, json=data)
        status = error.check_return_status(response, logger)
        if not status:
            error.error_message(
                response, 'Unable to save search', error=RuntimeError)

        response_json = error.get_response_json(response, logger)
        search_dict = response_json.get('objects', [{}])[0]
        self._resource_id = search_dict.get('id', 0)
        return f'Saved search to ID: {self._resource_id}'
def save ( self ) : if not self . name : raise ValueError ( 'No name for the query saved. Please select a name first.' ) if not ( self . query_string or self . query_dsl ) : raise ValueError ( 'Need to have either a query DSL or a query string to be ' 'able to save the search.' ) if not self . description : logger . warning ( 'No description selected for search, saving without one' ) if self . _resource_id : resource_url = ( f'{self.api.api_root}/sketches/{self._sketch.id}/views/' f'{self._resource_id}/' ) else : resource_url = ( f'{self.api.api_root}/sketches/{self._sketch.id}/views/' ) query_filter = self . query_filter if self . return_fields : sketch_data = self . _sketch . data sketch_meta = sketch_data . get ( 'meta' , { } ) mappings = sketch_meta . get ( 'mappings' , [ ] ) use_mappings = [ ] for field in self . return_fields . split ( ',' ) : field = field . strip ( ) . lower ( ) for map_entry in mappings : if map_entry . get ( 'field' , '' ) . lower ( ) == field : use_mappings . append ( map_entry ) query_filter [ 'fields' ] = use_mappings data = { 'name' : self . name , 'description' : self . description , 'query' : self . query_string , 'filter' : query_filter , 'dsl' : self . query_dsl , 'labels' : json . dumps ( self . labels ) , } response = self . api . session . post ( resource_url , json = data ) status = error . check_return_status ( response , logger ) if not status : error . error_message ( response , 'Unable to save search' , error = RuntimeError ) response_json = error . get_response_json ( response , logger ) search_dict = response_json . get ( 'objects' , [ { } ] ) [ 0 ] self . _resource_id = search_dict . get ( 'id' , 0 ) return f'Saved search to ID: {self._resource_id}'