A simple query builder for Amazon Cloudsearch Structured query parser.
Features
Provide a simple query builder for Amazon Cloudsearch Structured query parser.
Please refer to the following link regarding Structured Search Syntax .
Caution
At the moment, this library is compatible only to Structured Search Syntax .
It does not have any plans corresponding to the other query parser(lucene, dismax, simple).
If you want the lucene query builder, it’s a good idea is to use the following library.
This library does not handle to generate queries that are not related to Structured Search Syntax. Ex) size, facets …
Set up
Make environment with pip:
$ pip install csquery
Usage
and
Syntax: (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
Syntax: (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
Syntax: (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
Syntax: (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')
phrase
Syntax: (phrase field=FIELD boost=N ‘STRING’)
from csquery.structured import phrase
q = phrase('star', boost=2, field='title')
q() #=> (phrase field=title boost=2 'star')
prefix
Syntax: (prefix field=FIELD boost=N ‘STRING’)
from csquery.structured import prefix
q = prefix('star', boost=2, field='title')
q() #=> (prefix field=title boost=2 'star')
range
Syntax: (range field=FIELD boost=N RANGE)
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,})
term
Syntax: (term field=FIELD boost=N ‘STRING’|VALUE)
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')
Complex query sample
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')))
Using with 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 Support
Python 2.7, 3,3, 3.4 or later.
License
Source code of this library Licensed under the MIT License.
See the LICENSE.rst file for specific terms.
Contributors
Thanks.
@podhmo
@furi
History
0.1.4(Nov 25, 2015)
Fixed unquoted field value. #4.
0.1.3(Nov 20, 2015)
Fixed over escaped expression. #3.
0.1.2(Nov 18, 2015)
Fixed escape bug. #2.
0.1.1(Nov 6, 2015)
Fixed bug. #1.
0.1.0(Jun 8, 2015)
First release
Metadata
Release files for csquery 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| csquery-0.1.4.tar.gz | 8.8 kB | Without provenance | Details for csquery-0.1.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| csquery-0.1.4-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for csquery-0.1.4-py2.py3-none-any.whl |
Total release size: 18.3 kB
Release files / csquery-0.1.4.tar.gz
| Download URL | csquery-0.1.4.tar.gz |
|---|---|
| Size | 8.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
467420c8d96a7486b34aaa56f30f327c6de09b759b873fc385e4057b5c2d2218
|
|
BLAKE2b-256 checksum How to use checksums |
29bef881e24c434fab3fdcd9e5725ca3792d9380fc8c3f9741feb3725c1027d0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / csquery-0.1.4-py2.py3-none-any.whl
| Download URL | csquery-0.1.4-py2.py3-none-any.whl |
|---|---|
| Size | 9.4 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
7f1fc3fb8dc3985fe878182dd0d8c1e95e3ef6971519ec9b026264bf82ce245d
|
|
BLAKE2b-256 checksum How to use checksums |
7a30f2f1369e822ba6bfab28f807e00ca51f9513bfede095aec50ecc2f090479
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for csquery 0.1.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.