Exemplo n.º 1
0
def index(client=None):
    if not client:
        client = typesense.Client({
            'nodes': [{
                'host': 'localhost',
                'port': '8108',
                'protocol': 'http',
            }],
            'api_key':
            '55CL97li9EyOuG54WobxcAjTwHl3rYRRUQcmelXXrr87HmKn',
            'connection_timeout_seconds':
            2
        })
    for response in fetch_dyanmodb():
        if not response:
            raise ValueError("Response cannot be empty")
        else:
            for i in response.get('Items', []):
                i['matchid'] = int(i['id'])
                i.pop('id')
                i['season'] = int(i['season'])
                i['dl'] = int(i['dl'])
                i['winRun'] = int(i['winRun'])
                i['winWicket'] = int(i['winWicket'])
                client.collections['IPL-Data'].documents.create(i)
Exemplo n.º 2
0
def search(query):

    client = typesense.Client({
        'nodes': [{
          'host': config.TYPESENSE_HOST,
          'port': config.TYPESENSE_PORT,
          'protocol': 'http',
        }],
        'api_key': config.TYPESENSE_API_KEY,
        'connection_timeout_seconds': 2
    })

    search_parameters = {
        'q': prepare_string(query),
        'query_by': "combined",
        'prefix': 'no',
        'num_typos': 5
    }

    hits = client.collections['mbid_mapping_latest'].documents.search(search_parameters)

    output = []
    for hit in hits['hits']:
        output.append({'artist_credit_name': hit['document']['artist_credit_name'],
                       'artist_mbids': hit['document']['artist_mbids'],
                       'release_name': hit['document']['release_name'],
                       'release_mbid': hit['document']['release_mbid'],
                       'recording_name': hit['document']['recording_name'],
                       'recording_mbid': hit['document']['recording_mbid']})

    return output
Exemplo n.º 3
0
def connect():
    """
    Open a connection to the typesense server.
    """

    api_key = os.getenv('TYPESENSE_API_KEY')
    if not api_key:
        try:
            with open('/etc/typesense/typesense-server.ini') as f:
                lines = f.readlines()
                api_key = [
                    line[10:] for line in lines
                    if line.startswith('api-key = ')
                ][0].rstrip()
        except FileNotFoundError:
            pass
    if not api_key:
        return None

    return typesense.Client({
        'nodes': [{
            'host': 'search.imagej.net',
            'port': '8108',
            'protocol': 'https'
        }],
        'api_key':
        api_key,
        'connection_timeout_seconds':
        2,
    })
def initial_typesense():
    client = typesense.Client({
        'api_key':
        TYPESENSE_KEY,
        'nodes': [{
            'host': TYPESENSE_HOST,
            'port': '',
            'protocol': 'https'
        }],
        'connection_timeout_seconds':
        2
    })

    try:
        client.collections['another'].delete()
    except Exception as e:
        pass

    initial_schema = {
        'name':
        'another',
        'fields': [{
            "name": "productID",
            "type": "int32"
        }, {
            "name": "category",
            "type": "string",
            "facet": True
        }, {
            "name": "price",
            "type": "float",
            "facet": True
        }, {
            "name": "img",
            "type": "string"
        }, {
            "name": "product",
            "type": "string",
            "facet": True
        }, {
            "name": "url",
            "type": "string",
            "facet": True
        }, {
            "name": "test",
            "type": "int32"
        }, {
            "name": "offers",
            "type": "string"
        }],
        'default_sorting_field':
        'productID'
    }

    client.collections.create(initial_schema)
Exemplo n.º 5
0
 def __init__(self):
     super().__init__()
     self.client = typesense.Client({
         'master_node': {
             'host': '159.65.76.64',
             'port': '8108',
             'protocol': 'http',
             'api_key': api_key
         },
         'timeout_seconds': 2
     })
Exemplo n.º 6
0
 def connect(self):
     self.client = typesense.Client({
         "api_key":
         self.api_key,
         "nodes": [{
             "host": self.host,
             "port": self.port,
             "protocol": self.protocol
         }],
         "connection_timeout_seconds":
         self.timeout,
     })
def lambda_handler(event, context):
    client = typesense.Client({
        'nodes': [{
            'host': 'i3jcr4k0wfbz6qnup-1.a1.typesense.net',
            'port': '443',
            'protocol': 'https',
        }],
        'api_key':
        'AaBOdPwIj7doBybWN5rfiXd12baeudWD',
        'connection_timeout_seconds':
        2
    })
    processed = 0
    atrributes = {
        'id': 'N',
        'season': 'S',
        'city': 'S',
        'date': 'S',
        'team1': 'S',
        'team2': 'S',
        'toss': 'S',
        'decision': 'S',
        'result': 'S',
        'dl': 'N',
        'winner': 'S',
        'winRun': 'N',
        'winWicket': 'N',
        'mom': 'S',
        'venue': 'S',
        'umpire1': 'S',
        'umpire2': 'S',
    }
    for record in event['Records']:
        ddb_record = record['dynamodb']
        if record['eventName'] == 'REMOVE':
            res = client.collections['IPL-Data'].documents[str(
                ddb_record['OldImage']['id']['N'])].delete()
        else:
            upload = ddb_record['NewImage']
            record1 = {}
            for key in atrributes.keys():
                record1[key] = upload['NewImage'][key][atrributes[key]]
            record1['id'] = str(record1['id'])
            record1['season'] = int(record1['season'])
            record1['dl'] = int(record1['dl'])
            record1['winRun'] = int(record1['winRun'])
            record1['winWicket'] = int(record1['winWicket'])
            res = client.collections['IPL-Data'].upsert(record1)
            print(res)
        processed = processed + 1

    print('Successfully processed {} records'.format(processed))
    return processed
def build_index():

    client = typesense.Client({
        'nodes': [{
            'host': config.TYPESENSE_HOST,
            'port': config.TYPESENSE_PORT,
            'protocol': 'http',
        }],
        'api_key':
        config.TYPESENSE_API_KEY,
        'connection_timeout_seconds':
        1000000
    })

    collection_name = COLLECTION_NAME_PREFIX + datetime.datetime.now(
    ).strftime('%Y%m%d_%H%M%S')
    try:
        log("typesense index: build index '%s'" % collection_name)
        build(client, collection_name)
    except typesense.exceptions.TypesenseClientError as err:
        log("typesense index: Cannot build index: ", str(err))
        return -1

    try:
        latest = COLLECTION_NAME_PREFIX + "latest"
        log("typesense index: alias index '%s' to %s" %
            (collection_name, latest))
        aliased_collection = {"collection_name": collection_name}
        client.aliases.upsert(latest, aliased_collection)
    except typesense.exceptions.TypesenseClientError as err:
        log("typesense index: Cannot build index: ", str(err))
        return -2

    try:
        for collection in client.collections.retrieve():
            if collection["name"] == collection_name:
                continue

            if collection["name"].startswith(COLLECTION_NAME_PREFIX):
                log("typesense index: delete collection '%s'" %
                    collection["name"])
                client.collections[collection["name"]].delete()
            else:
                log("typesense index: ignore collection '%s'" %
                    collection["name"])

    except typesense.exceptions.ObjectNotFound:
        log("typesense index: Failed to delete collection '%s'.", str(err))

    return 0
Exemplo n.º 9
0
    def __init__(self):
        self.debug = False

        self.client = typesense.Client({
            'nodes': [{
                'host': config.TYPESENSE_HOST,
                'port': config.TYPESENSE_PORT,
                'protocol': 'http',
            }],
            'api_key':
            config.TYPESENSE_API_KEY,
            'connection_timeout_seconds':
            2
        })
Exemplo n.º 10
0
    def __init__(self, timeout=DEFAULT_TIMEOUT, remove_stop_words=False):
        self.debug = False

        self.client = typesense.Client({
            'nodes': [{
                'host': config.TYPESENSE_HOST,
                'port': config.TYPESENSE_PORT,
                'protocol': 'http',
            }],
            'api_key':
            config.TYPESENSE_API_KEY,
            'connection_timeout_seconds':
            timeout
        })
        self.remove_stop_words = remove_stop_words
Exemplo n.º 11
0
def typesense_client():
    """Return a typesense Client object for accessing document collections.
    """
    client = typesense.Client({
        'nodes': [{
            'host': settings.TYPESENSE_HOST,
            'port': settings.TYPESENSE_PORT,
            'protocol': settings.TYPESENSE_PROTOCOL,
        }],
        'api_key':
        settings.TYPESENSE_API_KEY,
        'connection_timeout_seconds':
        settings.TYPESENSE_CONN_TIMEOUT,
    })
    return client
def main():
    client = typesense.Client({
        'nodes': [{
            'host': 'i3jcr4k0wfbz6qnup-1.a1.typesense.net',
            'port': '443',
            'protocol': 'https',
        }],
        'api_key': 'AaBOdPwIj7doBybWN5rfiXd12baeudWD',
        'connection_timeout_seconds': 2
    })
    search_parameters = {
        'q'         : '50000',
        'query_by'  : 'id',
    }
    response = client.collections['IPL-Data'].documents.search(search_parameters)
    print(json.dumps(response, indent=4))
def main():
    client = typesense.Client({
        'nodes': [{
            'host': 'localhost',
            'port': '8108',
            'protocol': 'http',
        }],
        'api_key':
        '55CL97li9EyOuG54WobxcAjTwHl3rYRRUQcmelXXrr87HmKn',
        'connection_timeout_seconds':
        2
    })
    search_parameters = {
        'q': 'Royal Challengers Bangalore',
        'query_by': 'team1',
    }
    response = client.collections['IPL-Data'].documents.search(
        search_parameters)
    print(response)
Exemplo n.º 14
0
def connect():
    """
    Open a connection to the typesense server.
    """
    with open('/etc/typesense/typesense-server.ini') as f:
        lines = f.readlines()

    api_key = [line[10:] for line in lines
               if line.startswith('api-key = ')][0].rstrip()

    return typesense.Client({
        'nodes': [{
            'host': 'search.imagej.net',
            'port': '8108',
            'protocol': 'https'
        }],
        'api_key':
        api_key,
        'connection_timeout_seconds':
        2,
    })
Exemplo n.º 15
0
def search_q(request, qry, pg):
    """ Search For books"""
    query = qry
    client = typesense.Client({
        'master_node': {
            'host': 'localhost',
            'port': '8108',
            'protocol': 'http',
            'api_key': 'pNCB36PlZveIL9s8XmvVZM1KFM6BFmwG8F6BaOKQCNIrkHGY'
        },
        'timeout_seconds': 2
    })

    search_parameters = {
        'q': query,
        'query_by': 'title,author,publication,isbn',
        'sort_by': 'book_id:desc',
        'page': pg,
    }
    result = client.collections['temp5'].documents.search(search_parameters)
    return JsonResponse(result)
    def run(self):
        thread_db = None
        thread_cursor = None

        try:
            thread_db = mysql.connector.connect(host=DB_HOST,
                                                user=DB_USER,
                                                password=DB_PASS,
                                                database=DATABASE)

            thread_cursor = thread_db.cursor(prepared=True)

            thread_client = typesense.Client({
                'api_key':
                TYPESENSE_KEY,
                'nodes': [{
                    'host': TYPESENSE_HOST,
                    'port': '',
                    'protocol': 'https'
                }],
                'connection_timeout_seconds':
                2
            })

            for index in range(self._range[0], self._range[1]):
                print(random.randint(0, 9))
                offers = sub_query(records[index][0], thread_cursor)
                communicate_with_search(records[index], offers, thread_client)

        except mysql.connector.Error as e:
            print("Failed to query table in MySQL: {}".format(e))

        finally:
            if thread_db and thread_db.is_connected():
                thread_cursor.close()
                thread_db.close()
Exemplo n.º 17
0
import typesense

client = typesense.Client({
    'nodes': [{
        'host': 'localhost',  # For Typesense Cloud use xxx.a1.typesense.net
        'port': '8108',  # For Typesense Cloud use 443
        'protocol': 'http'  # For Typesense Cloud use https
    }],
    'api_key':
    "NfIrs3e3cS8H6hctV0eGsAmFaCQc181QtlAwdMZHIiiMNt6I",
    'connection_timeout_seconds':
    2
})

website_schema = {
    'name':
    'spudooli-website',
    'fields': [{
        'name': 'id',
        'type': 'int32'
    }, {
        'name': 'headline',
        'type': 'string'
    }, {
        'name': 'body',
        'type': 'string'
    }, {
        'name': 'datetime',
        'type': 'int64'
    }, {
        'name': 'url',
Exemplo n.º 18
0
import typesense

from settings import TYPESENSE

client = typesense.Client(TYPESENSE)
Exemplo n.º 19
0
import typesense

client = typesense.Client({
    'api_key':
    'abcd',
    'nodes': [{
        'host': 'localhost',
        'port': '8108',
        'protocol': 'http'
    }],
    'connection_timeout_seconds':
    2
})

client.collections['key_collection'].delete()

# Create a collection with admin key
create_response = client.collections.create({
    "name":
    "key_collection",
    "fields": [
        {
            "name": "title",
            "type": "string"
        },
        {
            "name": "ratings_count",
            "type": "int32"
        },
    ],
    "default_sorting_field":
Exemplo n.º 20
0
import typesense
import globals

tsClient = typesense.Client({
    'nodes': [{
        'host': globals.TYPESENSE_HOST,
        'port': globals.TYPESENSE_PORT,
        'protocol': 'http'
    }],
    'api_key':
    globals.TYPESENSE_API_KEY,
    'connection_timeout_seconds':
    2
})
Exemplo n.º 21
0
def build_index():

    client = typesense.Client({
        'nodes': [{
            'host': 'typesense',
            'port': '8108',
            'protocol': 'http',
        }],
        'api_key':
        config.TYPESENSE_API_KEY,
        'connection_timeout_seconds':
        1000000
    })

    schema = {
        'name':
        COLLECTION_NAME,
        'fields': [
            {
                'name': 'combined',
                'type': 'string'
            },
            {
                'name': 'score',
                'type': 'int32'
            },
        ],
        'default_sorting_field':
        'score'
    }

    try:
        client.collections[COLLECTION_NAME].delete()
    except typesense.exceptions.ObjectNotFound:
        pass

    client.collections.create(schema)

    with psycopg2.connect(config.DB_CONNECT_MB) as conn:
        with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as curs:

            curs.execute("SELECT max(score) FROM mapping.mbid_mapping")
            max_score = curs.fetchone()[0]

            query = ("""SELECT recording_name AS recording_name,
                               r.gid AS recording_mbid,
                               release_name AS release_name,
                               rl.gid AS release_mbid,
                               artist_credit_name AS artist_credit_name,
                               artist_credit_id,
                               score
                          FROM mapping.mbid_mapping
                          JOIN recording r
                            ON r.id = recording_id
                          JOIN release rl
                            ON rl.id = release_id""")

            if config.USE_MINIMAL_DATASET:
                query += " WHERE artist_credit_id = 1160983"

            curs.execute(query)
            documents = []
            for i, row in enumerate(curs):
                document = dict(row)
                document['score'] = max_score - document['score']
                document['combined'] = prepare_string(
                    document['recording_name'] + " " +
                    document['artist_credit_name'])
                documents.append(document)

                if len(documents) == BATCH_SIZE:
                    client.collections[COLLECTION_NAME].documents.import_(
                        documents)
                    documents = []

                if i and i % 100000 == 0:
                    print(i)

            if documents:
                client.collections[COLLECTION_NAME].documents.import_(
                    documents)
Exemplo n.º 22
0
        socket.gethostbyname(name)
        return name
    except Exception:
        return 'localhost'


rd = redis.Redis(host=lookup('redis'), port=6379, db=1)  # for general tasks
rn = redis.Redis(host=lookup('redis'), port=6379, db=2)  # for nlp related tasks
rc = redis.Redis(host=lookup('redis'), port=6379, db=3)  # for cache only
ri = redis.Redis(host=lookup('redis'), port=6379, db=4)  # for index only

wd = WeedFS(lookup("master"), 9333)  # weed-fs master address and port

ts = typesense.Client({
  'nodes': [{
    'host': lookup('typesense'),
    'port': '8108',
    'protocol': 'http',
  }],

  'api_key': 'MUzQD3ncGDBihx6YGTBeBJ4Q',
  'connection_timeout_seconds': 2
})


sb = redis.Redis(host=lookup('simbase'), port=7654)  # for recommand engine
if not sb.execute_command('blist'):
    sb.execute_command('bmk', 'b768', *['b%03d' % i for i in range(768)])
    sb.execute_command('vmk', 'b768', 'page')
    sb.execute_command('rmk', 'page', 'page', 'cosinesq')
def main():
    client = typesense.Client({
        'nodes': [{
            'host': 'i3jcr4k0wfbz6qnup-1.a1.typesense.net',
            'port': '443',
            'protocol': 'https',
        }],
        'api_key': 'AaBOdPwIj7doBybWN5rfiXd12baeudWD',
        'connection_timeout_seconds': 2
    })

    schema = {
        'name': 'IPL-Data',
        'fields': [
            {
                'name'  :  'id',
                'type'  :  'string'
            },
            {
                'name'  :  'season',
                'type'  :  'int32',
                'facet' :  True
            },
            {
                'name'  :  'city',
                'type'  :  'string',
                'facet' :  True
            },
            {
                'name'  :  'date',
                'type'  :  'string'
            },
            {
                'name'  :  'team1',
                'type'  :  'string',
                'facet' :  True
            },
            {
                'name'  :  'team2',
                'type'  :  'string',
                'facet' :  True
            },
            {
                'name'  :  'toss',
                'type'  :  'string'
            },
            {
                'name'  :  'decision',
                'type'  :  'string'
            },
            {
                'name'  :  'result',
                'type'  :  'string'
            },
            {
                'name'  :  'dl',
                'type'  :  'int32'
            },
            {
                'name'  :  'winner',
                'type'  :  'string',
                'facet' :  True
            },
            {
                'name'  :  'winRun',
                'type'  :  'int32'
            },
            {
                'name'  :  'winWicket',
                'type'  :  'int32'
            },
            {
                'name'  :  'mom',
                'type'  :  'string'
            },
            {
                'name'  :  'venue',
                'type'  :  'string',
            },
            {
                'name'  :  'umpire1',
                'type'  :  'string'
            },
            {
                'name'  :  'umpire2',
                'type'  :  'string'
            },
        ],
        'default_sorting_field': 'season'
    }
    client.collections.create(schema)   
    index(client)
Exemplo n.º 24
0
def main():
    client = typesense.Client({
        'nodes': [{
            'host': 'localhost',
            'port': '8108',
            'protocol': 'http',
        }],
        'api_key':
        '55CL97li9EyOuG54WobxcAjTwHl3rYRRUQcmelXXrr87HmKn',
        'connection_timeout_seconds':
        2
    })

    schema = {
        'name':
        'IPL-Data',
        'fields': [
            {
                'name': 'matchid',
                'type': 'int32'
            },
            {
                'name': 'season',
                'type': 'int32',
                'facet': True
            },
            {
                'name': 'city',
                'type': 'string',
                'facet': True
            },
            {
                'name': 'date',
                'type': 'string'
            },
            {
                'name': 'team1',
                'type': 'string',
                'facet': True
            },
            {
                'name': 'team2',
                'type': 'string',
                'facet': True
            },
            {
                'name': 'toss',
                'type': 'string'
            },
            {
                'name': 'decision',
                'type': 'string'
            },
            {
                'name': 'result',
                'type': 'string'
            },
            {
                'name': 'dl',
                'type': 'int32'
            },
            {
                'name': 'winner',
                'type': 'string',
                'facet': True
            },
            {
                'name': 'winRun',
                'type': 'int32'
            },
            {
                'name': 'winWicket',
                'type': 'int32'
            },
            {
                'name': 'mom',
                'type': 'string'
            },
            {
                'name': 'venue',
                'type': 'string',
            },
            {
                'name': 'umpire1',
                'type': 'string'
            },
            {
                'name': 'umpire2',
                'type': 'string'
            },
        ],
        'default_sorting_field':
        'season'
    }
    client.collections.create(schema)
    index(client)
Exemplo n.º 25
0
import config
import typesense

client = typesense.Client({
    'nodes': [{
        'host': 'localhost',  # For Typesense Cloud use xxx.a1.typesense.net
        'port': '8108',  # For Typesense Cloud use 443
        'protocol': 'http'  # For Typesense Cloud use https
    }],
    'api_key':
    config.api_key,
    'connection_timeout_seconds':
    2
})

print(client.collections['spudooli-website'].documents.search({
    'q':
    'auckland',
    'query_by':
    'headline, body',
    'include_fields':
    'headline, body, url',
}))

# drop_response = client.collections['news'].delete()
# print(drop_response)
Exemplo n.º 26
0
import os
import typesense as ts

client = ts.Client({
    "api_key":
    os.getenv("TYPESENSE_API_KEY") or input("TypeSense Admin API Key: "),
    "nodes": [{
        "host": "api.reguleque.cl",
        "port": "443",
        "protocol": "https"
    }],
})

print(
    "=>  Generated Search-only API Key:",
    client.keys.create({
        "description": "Search-only reguleque key.",
        "actions": ["documents:search"],
        "collections": ["revenue_entry"],
    })["value"],
)
Exemplo n.º 27
0
import json
import os
import typesense

# This only needs to be run once to setup the collection in Typesense (or when the schema is updated).
# IMPORTANT: It will *drop* previously setup collections, *including* their records.

client = typesense.Client({
    'master_node': {
        'host': 'search.datenanfragen.de',
        'port': '443',
        'protocol': 'https',
        'api_key': os.environ['TYPESENSE_API_KEY']
    },
    'timeout_seconds': 2
})


def get_fields_for_props(props):
    fields = []
    for prop, details in props:
        prop_type = ''
        if details["type"] == 'string': prop_type = 'string'
        elif details['type'] == 'array':
            if details['items'] and details['items']['type'] != 'string':
                continue
            prop_type = 'string[]'
        else:
            prop_type = 'string'

        fields.append({'name': prop, 'type': prop_type})
Exemplo n.º 28
0
import typesense

client = typesense.Client({
    'api_key':
    'abcd',
    'nodes': [{
        'host': 'localhost',
        'port': '8108',
        'protocol': 'http'
    }],
    'connection_timeout_seconds':
    2
})

# Create a collection

create_response = client.collections.create({
    "name":
    "books_january",
    "fields": [{
        "name": "title",
        "type": "string"
    }, {
        "name": "authors",
        "type": "string[]",
        "facet": True
    }, {
        "name": "publication_year",
        "type": "int32",
        "facet": True
    }, {
Exemplo n.º 29
0
books_table = '/home/aayush_linux/Downloads/college-project/cproject/common/search/books.json'  ## path to books_data.json

import typesense
import json

client = typesense.Client({
    'master_node': {
        'host': 'localhost',
        'port': '8108',
        'protocol': 'http',
        'api_key': 'pNCB36PlZveIL9s8XmvVZM1KFM6BFmwG8F6BaOKQCNIrkHGY'
    },
    'timeout_seconds': 2
})

books_schema = {
    'name':
    'temp5',
    'fields': [
        {
            'name': 'book_id',
            'type': 'int32'
        },
        {
            'name': 'title',
            'type': 'string'
        },
        {
            'name': 'author',
            'type': 'string'
        },
Exemplo n.º 30
0
if not len(sys.argv) == 2:
    print("Please provide the SKR name as the only argument.")
    sys.exit(0)

index_name = sys.argv[1]
if index_name.endswith('.xml'):
    print("Please only provide the SKR name (without '.xml').")
    sys.exit(0)

client = typesense.Client({
    'api_key':
    os.environ['TYPESENSE_KEY'],
    'nodes': [{
        'host': os.environ['TYPESENSE_HOST'],
        'port': os.environ['TYPESENSE_PORT'],
        'protocol': os.environ['TYPESENSE_PROTOCOL']
    }],
    'connection_timeout_seconds':
    10
})

# Using collection aliases would be a lot cleaner here of course but this should be fast enough for me not to care.
try:
    client.collections[index_name].delete()
except:
    pass
schema = {
    'name':
    index_name,
    'default_sorting_field':