跳转到主要内容

Amazon Cloudsearch结构化查询解析器的简单查询构建器。

项目描述

A simple query builder for Amazon Cloudsearch Structured query parser.

travis coveralls.io downloads latest version license requirements status

功能

注意

  • 目前,此库仅兼容于结构化搜索语法

  • 它没有针对其他查询解析器(lucene,dismax,simple)的计划。

  • 如果您想使用lucene查询构建器,使用以下库是个好主意。

  • 此库不处理生成与结构化搜索语法无关的查询。例如:size,facets……

设置

使用pip创建环境

$ pip install csquery

用法

语法:(and boost=N EXPRESSION EXPRESSION …… EXPRESSIONn)

from csquery.structured import and_, field

q = and_(title='star', actors='Harrison Ford', year=('', 2000))
q() #=> (and title:'star' actors:'Harrison Ford' year:{,2000])

# with option
q = and_({'title': 'star'}, {'title': 'star2'}, boost=2)
q() #=> (and boost=2 title:'star' title:'star2')

# another writing
and_({'title': 'star'}, {'actors': 'Harrison Ford'}, {'year': ('', 2000)})
and_(field('star', 'title'), field('Harrison Ford', 'actors'), field(('', 2000), 'year'))

语法:(or boost=N EXPRESSION1 EXPRESSION2 …… EXPRESSIONn)

from csquery.structured import or_, field

q = or_(title='star', actors='Harrison Ford', year=('', 2000))
q() #=> (or title:'star' actors:'Harrison Ford' year:{,2000])

# with option
q = or_({'title': 'star'}, {'title': 'star2'}, boost=2)
q() #=> (or boost=2 title:'star' title:'star2')

语法:(not boost=N EXPRESSION)

from csquery.structured import not_, and_

q = not_(and_(actors='Harrison Ford', year=('', 2010)))
q() #=> (not (and actors:'Harrison Ford' year:{,2010]))

# with option
q = not_(and_(actors='Harrison Ford', year=('', 2010)), boost=2)
q() #=> (not boost=2 (and actors:'Harrison Ford' year:{,2010]))

语法:(near field=FIELD distance=N boost=N ‘STRING’)

from csquery.structured import near

q = near('teenage vampire', boost=2, field='plot', distance=2)
q() #=> (near field=plot distance=2 boost=2 'teenage vampire')

短语

语法: (短语字段=字段名 增量=N '字符串')

from csquery.structured import phrase

q = phrase('star', boost=2, field='title')
q() #=> (phrase field=title boost=2 'star')

前缀

语法: (前缀字段=字段名 增量=N '字符串')

from csquery.structured import prefix

q = prefix('star', boost=2, field='title')
q() #=> (prefix field=title boost=2 'star')

范围

语法: (范围字段=字段名 增量=N 范围)

from csquery.structured import range_

q = range_((1990, 2000))
q() #=> (range [1990,2000])
q = range_((None, 2000))
q() #=> (range {,2000])
q = range_((1990,))
q() #=> (range [1990,})

# with opition
q = range_((1990, 2000), field='date', boost=2)
q() #=> (range field=date boost=2 [1990,2000])

# another writing
q = range_('[1990,2000]')
q() #=> (range [1990,2000])

q = range_(('', 2000))
q() #=> (range {,2000])
q = range_('{,2000]')
q() #=> (range {,2000])

q = range_((1990, None))
q() #=> (range [1990,})
q = range_((1990, ''))
q() #=> (range [1990,})
q = range_('[1990,}')
q() #=> (range [1990,})

术语

语法: (术语字段=字段名 增量=N '字符串'|值)

from csquery.structured import term

q = term(2000, field='year', boost=2)
q() #=> (term field=year boost=2 2000)

q = term('star', field='title', boost=2)
q() #=> (term field=title boost=2 'star')

复杂查询示例

from csquery.structured import and_, or_, not_, term

q = and_(
    not_('test', field='genres'),
    or_(
        term('star', field='title', boost=2),
        term('star', field='plot')
    )
)
q() #=> (and (not field=genres 'test') (or (term field=title boost=2 'star') (term field=plot 'star')))

与 boto 一起使用

http://boto.readthedocs.org/en/latest/ref/cloudsearch2.html

from csquery.structured import and_
from boto.cloudsearch2.layer2 import Layer2

conn = Layer2(
    region='ap-northeast-1',
    aws_access_key_id=[AWS ACCESSS KEY ID],
    aws_secret_access_key=[AWS SECRET KEY],
)
domain = conn.lookup('search_domain_name')
search_service = domain.get_search_service()

q = and_(title='star', actors='Harrison Ford', year=('', 2000))
result = search_service.search(q=q(), parser='structured')

Python支持

  • Python 2.7, 3.3, 3.4 或更高版本。

许可证

  • 此库的源代码根据 MIT 许可证授权。

有关具体条款,请参阅 LICENSE.rst 文件。

作者

  • tell-k <ffk2005 at gmail.com>

贡献者

谢谢。

  • @podhmo

  • @furi

历史

0.1.4(2015年11月25日)

  • 修复了未引用的字段值问题。 #4

0.1.3(2015年11月20日)

  • 修复了过度转义的表达式问题。 #3

0.1.2(2015年11月18日)

  • 修复了转义错误。 #2

0.1.1(2015年11月6日)

  • 修复了错误。 #1

0.1.0(2015年6月8日)

  • 首次发布

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定要选择哪个,请了解更多关于 安装包 的信息。

源代码发行版

csquery-0.1.4.tar.gz (8.8 kB 查看哈希值

上传时间 源代码

构建发行版

csquery-0.1.4-py2.py3-none-any.whl (9.4 kB 查看哈希值

上传时间 Python 2 Python 3

由以下组织支持

AWSAWS 云计算和安全赞助商 DatadogDatadog 监控 FastlyFastly CDN GoogleGoogle 下载分析 MicrosoftMicrosoft PSF赞助商 PingdomPingdom 监控 SentrySentry 错误日志 StatusPageStatusPage 状态页面