Skip to content

Commit

Permalink
Index body and target
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandermendes committed May 27, 2018
1 parent 4371e9b commit 6213060
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
14 changes: 12 additions & 2 deletions docs/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ The `fts` query accepts the following parameters for each field:
| operator | Join tokens with `and`, `or` or `not` (default `and`) |
| prefix | Treat each token as a prefix (default `True`) |

!!! info "Full-text search language"

The dictionary used for full-text searches is defined for each Annotation
by the first `language` code found in the Annotation's `body`. If no
such language code is found then the server default is used. See the
[Configuration](/setup/configuration.md) section for more details.


## fts_phrase

Return Annotations where the specified keys contain a `query` phrase. The
Expand All @@ -133,5 +141,7 @@ The `fts_phrase` query accepts the following parameters for each field:
| prefix | Treat the query as a prefix (default `True`) |
| distance | The distance between tokens (default `1`) |

Note that all phrase queries will be treated as prefixes; to search for
exact phrases use `contains` instead.
!!! note "Exact phrase searches"

Note that all phrase queries will be treated as prefixes; to search for
exact phrases use [`contains`](/search.md#contains) instead.
6 changes: 6 additions & 0 deletions explicates/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,20 @@ def setup_search(app):

def setup_db(app):
"""Setup database."""
from explicates.model.indexes import indexes
def create_slave_session(db, bind):
slave = app.config.get('SQLALCHEMY_BINDS')['slave']
if slave == app.config.get('SQLALCHEMY_DATABASE_URI'):
return db.session
engine = db.get_engine(db.app, bind=bind)
options = dict(bind=engine, scopefunc=_app_ctx_stack.__ident_func__)
slave_session = db.create_scoped_session(options=options)

for idx in indexes:
idx.create(bind=engine)

return slave_session

db.app = app
db.init_app(app)
db.slave_session = create_slave_session(db, bind='slave')
Expand Down
4 changes: 2 additions & 2 deletions explicates/model/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from flask import url_for, current_app
from sqlalchemy.schema import Column, ForeignKey
from sqlalchemy import Integer, Text
from sqlalchemy import Integer, String
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.ext.declarative import declarative_base

Expand Down Expand Up @@ -53,7 +53,7 @@ class Annotation(db.Model, Base):
nullable=False)

#: The language used for full-text searches.
language = Column(Text, nullable=False, default=get_language)
language = Column(String, nullable=False, default=get_language)

@hybrid_property
def iri(self):
Expand Down
15 changes: 15 additions & 0 deletions explicates/model/indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# -*- coding: utf8 -*-
"""Indexes."""

from sqlalchemy.schema import Index
from sqlalchemy.sql import text


indexes = [
Index('idx_annotation_body',
text("to_tsvector(language::regconfig, _data -> 'body')"),
postgresql_using='gin'),
Index('idx_annotation_target',
text("to_tsvector(language::regconfig, _data -> 'target')"),
postgresql_using='gin')
]
12 changes: 12 additions & 0 deletions test/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ def test_search_by_fts_default(self):
results = self.search.search(fts=fts_query)
assert_equal(results, [anno1])

@with_context
def test_search_by_fts_does_not_include_keys(self):
"""Test search by fts does not include keys."""
anno1 = AnnotationFactory(data={'body': {'source': 'foo'}})
fts_query = {
'body': {
'query': 'source'
}
}
results = self.search.search(fts=fts_query)
assert_equal(results, [])

@with_context
def test_search_by_fts_without_prefix(self):
"""Test search by fts without prefix."""
Expand Down

0 comments on commit 6213060

Please sign in to comment.