-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24d1b7e
commit 9f4772f
Showing
9 changed files
with
423 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from faker import Faker as OriginalFaker | ||
from factory.base import Factory | ||
from factory import Faker, LazyAttribute | ||
|
||
from search_index.documents import Animal | ||
|
||
FAKER = OriginalFaker() | ||
|
||
__all__ = ( | ||
'AnimalFactory', | ||
) | ||
|
||
|
||
class AnimalFactory(Factory): | ||
"""Animal factory.""" | ||
|
||
class Meta(object): | ||
model = Animal | ||
|
||
scope = LazyAttribute(lambda x: { | ||
'farm_id': FAKER.uuid4(), | ||
'holding_id': FAKER.uuid4(), | ||
}) | ||
action = Faker('word') | ||
entity = 'animal' | ||
id = Faker('uuid4') | ||
app = "s4farm-api" | ||
message_id = Faker('uuid4') | ||
publish_date = Faker('date') | ||
data = LazyAttribute(lambda x: { | ||
'id': FAKER.pyint(), | ||
'genetic': { | ||
'id': FAKER.pyint(), | ||
'name': FAKER.sentence(nb_words=3), | ||
}, | ||
'animal_type': { | ||
'id': FAKER.pyint(), | ||
'name': FAKER.word(), | ||
'gender': { | ||
'id': FAKER.pyint(), | ||
'name': FAKER.random_sample(['Male', 'Female'])[0] | ||
} | ||
}, | ||
'uuid': FAKER.uuid4(), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import graphene | ||
from graphene import Node | ||
from graphene_elastic import ( | ||
ElasticsearchObjectType, | ||
ElasticsearchConnectionField, | ||
) | ||
from graphene_elastic.filter_backends import ( | ||
FilteringFilterBackend, | ||
SearchFilterBackend, | ||
OrderingFilterBackend, | ||
DefaultOrderingFilterBackend, | ||
) | ||
from graphene_elastic.constants import ( | ||
LOOKUP_FILTER_PREFIX, | ||
LOOKUP_FILTER_TERM, | ||
LOOKUP_FILTER_TERMS, | ||
LOOKUP_FILTER_WILDCARD, | ||
LOOKUP_QUERY_EXCLUDE, | ||
LOOKUP_QUERY_IN, | ||
) | ||
|
||
from search_index.documents import Animal as AnimalDocument | ||
|
||
|
||
__all__ = ( | ||
'Animal', | ||
'Query', | ||
'schema', | ||
) | ||
|
||
|
||
class Animal(ElasticsearchObjectType): | ||
"""Animal.""" | ||
|
||
class Meta: | ||
|
||
document = AnimalDocument | ||
interfaces = (Node,) | ||
filter_backends = [ | ||
FilteringFilterBackend, | ||
SearchFilterBackend, | ||
OrderingFilterBackend, | ||
DefaultOrderingFilterBackend, | ||
] | ||
filter_fields = { | ||
'id': { | ||
'field': 'id.raw', | ||
'default_lookup': LOOKUP_FILTER_TERM, | ||
}, | ||
'action': { | ||
'field': 'action.raw', | ||
'default_lookup': LOOKUP_FILTER_TERM, | ||
}, | ||
'entity': { | ||
'field': 'entity.raw', | ||
'default_lookup': LOOKUP_FILTER_TERM, | ||
}, | ||
} | ||
search_fields = { | ||
'action': None, | ||
'entity': None, | ||
} | ||
ordering_fields = { | ||
'id': 'id.raw', | ||
'publish_date': 'publish_date', | ||
'action': 'action.raw', | ||
'entity': 'entity.raw', | ||
} | ||
|
||
ordering_defaults = ( | ||
'id.raw', | ||
'publish_date' | ||
) | ||
|
||
|
||
class Query(graphene.ObjectType): | ||
"""Animal query.""" | ||
|
||
animals = ElasticsearchConnectionField(Animal) | ||
|
||
|
||
schema = graphene.Schema( | ||
query=Query | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import graphene | ||
from graphene import Node | ||
from graphene_elastic import ( | ||
ElasticsearchObjectType, | ||
ElasticsearchConnectionField, | ||
) | ||
from graphene_elastic.filter_backends import ( | ||
FilteringFilterBackend, | ||
SearchFilterBackend, | ||
OrderingFilterBackend, | ||
DefaultOrderingFilterBackend, | ||
) | ||
from graphene_elastic.constants import ( | ||
LOOKUP_FILTER_PREFIX, | ||
LOOKUP_FILTER_TERM, | ||
LOOKUP_FILTER_TERMS, | ||
LOOKUP_FILTER_WILDCARD, | ||
LOOKUP_QUERY_EXCLUDE, | ||
LOOKUP_QUERY_IN, | ||
) | ||
|
||
from search_index.documents import ReadOnlyAnimal as ReadOnlyAnimalDocument | ||
|
||
|
||
__all__ = ( | ||
'ReadOnlyAnimal', | ||
'Query', | ||
'schema', | ||
) | ||
|
||
|
||
class ReadOnlyAnimal(ElasticsearchObjectType): | ||
"""Read-only animal.""" | ||
|
||
class Meta: | ||
|
||
document = ReadOnlyAnimalDocument | ||
interfaces = (Node,) | ||
filter_backends = [ | ||
FilteringFilterBackend, | ||
SearchFilterBackend, | ||
OrderingFilterBackend, | ||
DefaultOrderingFilterBackend, | ||
] | ||
filter_fields = { | ||
'id': { | ||
'field': 'id.raw', | ||
'default_lookup': LOOKUP_FILTER_TERM, | ||
}, | ||
'action': { | ||
'field': 'action.raw', | ||
'default_lookup': LOOKUP_FILTER_TERM, | ||
}, | ||
'entity': { | ||
'field': 'entity.raw', | ||
'default_lookup': LOOKUP_FILTER_TERM, | ||
}, | ||
} | ||
search_fields = { | ||
'action': None, | ||
'entity': None, | ||
} | ||
ordering_fields = { | ||
'id': 'id.raw', | ||
'publish_date': 'publish_date', | ||
'action': 'action.raw', | ||
'entity': 'entity.raw', | ||
} | ||
|
||
ordering_defaults = ( | ||
'id.raw', | ||
'publish_date' | ||
) | ||
|
||
|
||
class Query(graphene.ObjectType): | ||
"""Animal query.""" | ||
|
||
read_only_animals = ElasticsearchConnectionField(ReadOnlyAnimal) | ||
|
||
|
||
schema = graphene.Schema( | ||
query=Query | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .animal import * | ||
from .post import * | ||
from .user import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import datetime | ||
from elasticsearch_dsl import connections | ||
from elasticsearch_dsl import ( | ||
analyzer, | ||
Integer, | ||
Boolean, | ||
Completion, | ||
Date, | ||
Document, | ||
InnerDoc, | ||
Keyword, | ||
Nested, | ||
Object, | ||
Text, | ||
) | ||
from .read_only import ReadOnlyDocument | ||
from .settings import FARM_ANIMAL_DOCUMENT_NAME | ||
|
||
try: | ||
from elasticsearch import logger | ||
except ImportError: | ||
import logging | ||
logger = logging.getLogger(__name__) | ||
|
||
__all__ = ( | ||
'Animal', | ||
'ReadOnlyAnimal', | ||
) | ||
|
||
connections.create_connection(hosts=['localhost'], timeout=20) | ||
|
||
|
||
html_strip = analyzer('html_strip', | ||
tokenizer="standard", | ||
filter=["lowercase", "stop", "snowball"], | ||
char_filter=["html_strip"] | ||
) | ||
|
||
|
||
class Animal(Document): | ||
|
||
scope = Object( | ||
properties={ | ||
'farm_id': Text(), | ||
'holding_id': Text(), | ||
} | ||
) | ||
action = Text(analyzer=html_strip, fields={'raw': Keyword()}) | ||
entity = Text(analyzer=html_strip, fields={'raw': Keyword()}) | ||
# This is not internal ID of the Elasticsearch | ||
id = Text(analyzer=html_strip, fields={'raw': Keyword()}) | ||
app = Text() | ||
message_id = Text() | ||
publish_date = Date() | ||
data = Object( | ||
properties={ | ||
'id': Integer(), | ||
'genetic': Object( | ||
properties={ | ||
'id': Integer(), | ||
'name': Text(), | ||
} | ||
), | ||
'animal_type': Object( | ||
properties={ | ||
'id': Integer(), | ||
'name': Text(), | ||
'gender': Object( | ||
properties={ | ||
'id': Integer(), | ||
'name': Text(), | ||
} | ||
), | ||
} | ||
), | ||
} | ||
) | ||
uuid = Text() | ||
|
||
class Index: | ||
name = FARM_ANIMAL_DOCUMENT_NAME | ||
settings = { | ||
'number_of_shards': 1, | ||
'number_of_replicas': 1, | ||
'blocks': {'read_only_allow_delete': None}, | ||
} | ||
|
||
|
||
class ReadOnlyAnimal(ReadOnlyDocument): | ||
|
||
scope = Object( | ||
properties={ | ||
'farm_id': Text(), | ||
'holding_id': Text(), | ||
} | ||
) | ||
action = Text(analyzer=html_strip, fields={'raw': Keyword()}) | ||
entity = Text(analyzer=html_strip, fields={'raw': Keyword()}) | ||
# This is not internal ID of the Elasticsearch | ||
id = Text(analyzer=html_strip, fields={'raw': Keyword()}) | ||
app = Text() | ||
message_id = Text() | ||
publish_date = Date() | ||
data = Object( | ||
properties={ | ||
'id': Integer(), | ||
'genetic': Object( | ||
properties={ | ||
'id': Integer(), | ||
'name': Text(), | ||
} | ||
), | ||
'animal_type': Object( | ||
properties={ | ||
'id': Integer(), | ||
'name': Text(), | ||
'gender': Object( | ||
properties={ | ||
'id': Integer(), | ||
'name': Text(), | ||
} | ||
), | ||
} | ||
), | ||
} | ||
) | ||
uuid = Text() | ||
|
||
class Index: | ||
name = FARM_ANIMAL_DOCUMENT_NAME | ||
settings = { | ||
'number_of_shards': 1, | ||
'number_of_replicas': 1, | ||
'blocks': {'read_only_allow_delete': None}, | ||
} | ||
|
||
|
||
try: | ||
# Create the mappings in Elasticsearch | ||
Animal.init() | ||
except Exception as err: | ||
logger.error(err) |
Oops, something went wrong.