Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python搜索包含对象、部分匹配项的对象列表_Python_Search_Full Text Search_Pymssql - Fatal编程技术网

Python搜索包含对象、部分匹配项的对象列表

Python搜索包含对象、部分匹配项的对象列表,python,search,full-text-search,pymssql,Python,Search,Full Text Search,Pymssql,我正在尝试为一个小网站建立一个简单的搜索引擎。我最初的想法是避免使用更大的软件包,如Solr、Haystack等,因为我的搜索需求过于简单 我希望在一些指导下,我能使我的代码更具python风格、效率更高,最重要的是功能更正常 预期功能:基于项目编号、产品名称或类别名称的全部或部分匹配返回产品结果当前未实现类别匹配 一些代码: import pymssql import utils #My utilities class Product(object): def __init_

我正在尝试为一个小网站建立一个简单的搜索引擎。我最初的想法是避免使用更大的软件包,如Solr、Haystack等,因为我的搜索需求过于简单

我希望在一些指导下,我能使我的代码更具python风格、效率更高,最重要的是功能更正常

预期功能:基于项目编号、产品名称或类别名称的全部或部分匹配返回产品结果当前未实现类别匹配

一些代码:


import pymssql
import utils #My utilities  

class Product(object):  
   def __init__(self, item_number, name, description, category, msds):
        self.item_number = str(item_number).strip()
        self.name = name
        self.description = description
        self.category = category
        self.msds = str(msds).strip()

class Category(object):  
    def __init__(self, name, categories):
        self.name = name
        self.categories = categories
        self.slug = utils.slugify(name)
        self.products = []

categories = (
    Category('Food', ('123', '12A')),
    Category('Tables', ('354', '35A', '310', '31G')),
    Category('Chemicals', ('845', '85A', '404', '325'))
)

products = []

conn = pymssql.connect(...)
curr = conn.cursor()

for Category in categories:
    for c in Category.categories:
        curr.execute('SELECT item_number, name, CAST(description as text), category, msds from tblProducts WHERE category=%s', c)
        for row in curr:
            product = Product(row[0], row[1], row[2], row[3], row[4])
            products.append(product)
            Category.products.append(product)

conn.close()

def product_search(*params):
    results = []
    for product in products:
        for param in params:
            name = str(product.name)
            if (name.find(param.capitalize())) != -1:
                results.append(product)
            item_number = str(product.item_number)
            if (item.number.find(param.upper())) != -1:
                results.append(product)
    print results

product_search('something')

MS SQL数据库中的表和字段无法更改。 我最多会推出大约200种产品

有些东西突然向我扑来。嵌套为循环。在产品搜索中有两个不同的if语句,这可能会导致将重复的产品添加到结果中

我的想法是,如果我在内存中有这些产品,那么这些产品将很少改变,我可以缓存它们,减少对数据库的依赖,并可能提供高效的搜索

…现在发布。。。将回来并添加更多想法

编辑: 我之所以有一个Category对象来保存产品列表,是因为我想显示按类别组织的产品的html页面。而且,实际的类别编号将来可能会发生变化,持有一个元组似乎是一个简单的无痛解决方案。而且我对数据库有只读访问权限

单独列出产品清单的原因有点欺骗。我有一个页面,显示所有能够查看MSDS安全表的产品。此外,它还减少了搜索时需要遍历的级别

编辑2:


def product_search(*params):
    results = []
    lowerParams = [ param.lower() for param in params ]

    for product in products:
        item_number = (str(product.item_number)).lower()
        name = (str(product.name)).lower()
        for param in lowerParams:
           if param in item_number or param in name:
               results.append(product)
    print results

准备循环外部的所有变量,并在中使用,而不是。如果不需要子字符串的位置,请查找:

def product_search(*params):
    results = []
    upperParams = [ param.upper() for param in params ]

    for product in products:
        name = str(product.name).upper()
        item_number = str(product.item_number).upper()
        for upperParam in upperParams:
            if upperParam in name or upperParam in item_number:
                results.append(product)
    print results

如果名称和编号都与搜索参数匹配,则产品将在结果列表中出现两次

由于products count是一个较小的数字,我建议构造一个SELECT查询,如:

def search(*args):
    import operator
    cats = reduce(operator.add, [list(c.categories) for c in categories], [])

    query = "SELECT * FROM tblProducts WHERE category IN (" + ','.join('?' * len(cats)) + ") name LIKE '%?%' or CAST(item_number AS TEXT) LIKE '%?%' ..."
    curr.execute(query, cats + list(args)) # Not actual code
    return list(curr)

实际上,tblProducts表包含数千条记录。这就是为什么我试图查询我现有的对象,而不是做一个非常耗时的查询。这样,您的产品列表在内存中也将包含数千条记录。你能做的最好的事情就是测量像这样的查询需要多少时间。为什么它会包含数千条记录?在我的SQL查询中,我只从表中提取与我定义的类别相匹配的产品。我会尝试一下,但我对需要填写的内容有点不知所措。。。而不是实际的代码。这可能需要我一分钟的时间。我无法控制数据库的索引。根据特定类别从数千个表中查找约200个产品的第一次查询相当耗时。我希望避免查询两次。产品名称大写,但商品编号始终为大写,并且可能包含数字。例如,项目编号=147脱咖啡因。所以我想我需要区分名称和项目编号。你不必str 147DECAF-它看起来已经是一个字符串了。如果有人搜索147,那不是一个整数吗?可能有多个包含147的项目编号。147无咖啡因咖啡、147咖啡、147布拉等等。所有用户输入都被视为字符串,不会自动转换。除非必要,否则进行类型检查/转换也很不方便。好的,谢谢。我不知道。输入与项目编号匹配的问题如何?例如:“Decaf”需要与“147DECAF”匹配我可以进行不区分大小写的比较吗?不区分大小写的搜索将解决产品名称和产品项目编号的情况。