class FileDocument(Document): autocomplete = TextField(attr="name_autocomplete", analyzer=autocomplete_analyzer) coordinates = GeoPointField(attr="coordinates") person_ids = IntegerField(attr="person_ids") description = TextField(attr="description", analyzer=text_analyzer) # Elasticsearch wants `index_options: "offsets"` for the highlighter for large texts parsed_text = TextField(attr="parsed_text", analyzer=text_analyzer, index_options="offsets") def get_queryset(self): return (File.objects.prefetch_related("locations").prefetch_related( "mentioned_persons").order_by("id")) class Index: name = settings.ELASTICSEARCH_PREFIX + "-file" class Django: model = File queryset_pagination = settings.ELASTICSEARCH_QUERYSET_PAGINATION fields = [ "id", "name", "filename", "page_count", "created", "modified", "sort_date", ]
class BlogDocument(Document): content = TextField(analyzer='ik_max_word') suggestion = Completion(analyzer='ik_max_word') title = TextField(analyzer='ik_max_word') describe = TextField(analyzer='ik_max_word') class Index: # Name of the Elasticsearch index # 索引名称 name = 'blog_content' # 创建索引 # See Elasticsearch Indices API reference for available settings settings = {'number_of_shards': 2, 'number_of_replicas': 0} class Django: # 默认表 model = BlogModel # 默认字段 fields = [ 'username', "cover", "create_time", 'read_count', 'favorite', ] # 自定义字段 def prepare_words(self, instance): return " ".join(instance.fields)
class MeetingDocument(DocType): location = GeoPointField() sort_date = DateField() agenda_items = NestedField( attr="agendaitem_set", properties={ "key": TextField(), "name": TextField(analyzer=text_analyzer), "position": IntegerField(), "public": BooleanField(), }, ) @staticmethod def prepare_location(instance: Meeting) -> Optional[Dict[str, Any]]: if instance.location: return instance.location.coordinates() def get_queryset(self): return (Meeting.objects.prefetch_related( "agendaitem_set").prefetch_related("location").order_by("id")) class Meta: model = Meeting queryset_pagination = 500 fields = [ "id", "name", "short_name", "start", "end", "created", "modified" ]
class FileDocument(DocType): autocomplete = TextField(attr="name_autocomplete", analyzer=autocomplete_analyzer) coordinates = GeoPointField(attr="coordinates") person_ids = IntegerField(attr="person_ids") description = TextField(attr="description", analyzer=text_analyzer) # Elasticsearch wants `index_options: "offsets"` for the highlighter for large texts parsed_text = TextField( attr="parsed_text", analyzer=text_analyzer, index_options="offsets" ) def get_queryset(self): return ( File.objects.prefetch_related("locations") .prefetch_related("mentioned_persons") .order_by("id") ) class Meta: model = File queryset_pagination = 500 fields = [ "id", "name", "filename", "page_count", "created", "modified", "sort_date", ]
class GenericMembershipDocument: autocomplete = TextField(attr="name", analyzer=autocomplete_analyzer) sort_date = DateField() body = ObjectField(properties={"id": IntegerField(), "name": TextField()}) class Meta: fields = ["id", "name", "short_name"]
class GenericMembershipDocument: autocomplete = TextField(attr="name", analyzer=autocomplete_analyzer) sort_date = DateField() body = ObjectField(properties={"id": IntegerField(), "name": TextField()}) class Django: fields = ["id", "name", "short_name"] queryset_pagination = settings.ELASTICSEARCH_QUERYSET_PAGINATION
class OrganizationDocument(DocType, GenericMembershipDocument): autocomplete = TextField(attr="name", analyzer=autocomplete_analyzer) sort_date = DateField() body = ObjectField(properties={"id": IntegerField(), "name": TextField()}) def get_queryset(self): return Organization.objects.prefetch_related("body").order_by("id") class Meta(GenericMembershipDocument.Meta): model = Organization queryset_pagination = 500 fields = [ "id", "name", "short_name", "start", "end", "created", "modified" ]
class PaperDocument(DocType): autocomplete = TextField(attr="get_autocomplete", analyzer=autocomplete_analyzer) main_file = IntegerField(attr="main_file_id") person_ids = IntegerField(attr="person_ids") organization_ids = IntegerField(attr="organization_ids") def get_queryset(self): return ( Paper.objects.prefetch_related("persons") .prefetch_related("organizations") .order_by("id") ) class Meta: model = Paper queryset_pagination = 500 fields = [ "id", "short_name", "legal_date", "created", "name", "reference_number", "modified", "sort_date", ]
class PaperDocument(Document): autocomplete = TextField(attr="get_autocomplete", analyzer=autocomplete_analyzer) main_file = IntegerField(attr="main_file_id") person_ids = IntegerField(attr="person_ids") organization_ids = IntegerField(attr="organization_ids") def get_queryset(self): return (Paper.objects.prefetch_related("persons").prefetch_related( "organizations").order_by("id")) class Index: name = settings.ELASTICSEARCH_PREFIX + "-paper" class Django: model = Paper queryset_pagination = settings.ELASTICSEARCH_QUERYSET_PAGINATION fields = [ "id", "short_name", "legal_date", "created", "name", "reference_number", "modified", "sort_date", "display_date", ]
class PersonDocument(DocType): autocomplete = TextField(attr="name_autocomplete", analyzer=autocomplete_analyzer) sort_date = DateField() organization_ids = IntegerField(attr="organization_ids") def get_queryset(self): sort_date_queryset = Membership.objects.filter(start__isnull=False).order_by( "-start" ) return ( Person.objects.order_by("id") .prefetch_related("membership_set") .prefetch_related( Prefetch( "membership_set", queryset=sort_date_queryset, to_attr="sort_date_prefetch", ) ) ) class Meta: model = Person queryset_pagination = 500 fields = ["id", "name", "given_name", "family_name"]
class PersonDocument(Document): autocomplete = TextField(attr="name_autocomplete", analyzer=autocomplete_analyzer) sort_date = DateField() organization_ids = IntegerField(attr="organization_ids") def get_queryset(self): sort_date_queryset = Membership.objects.filter( start__isnull=False).order_by("-start") return (Person.objects.order_by("id").prefetch_related( "membership_set").prefetch_related( Prefetch( "membership_set", queryset=sort_date_queryset, to_attr="sort_date_prefetch", ))) class Index: name = settings.ELASTICSEARCH_PREFIX + "-person" class Django: model = Person queryset_pagination = settings.ELASTICSEARCH_QUERYSET_PAGINATION fields = ["id", "name", "given_name", "family_name"]
class OrganizationDocument(Document, GenericMembershipDocument): autocomplete = TextField(attr="name", analyzer=autocomplete_analyzer) sort_date = DateField() body = ObjectField(properties={"id": IntegerField(), "name": TextField()}) def get_queryset(self): return Organization.objects.prefetch_related("body").order_by("id") class Index: name = settings.ELASTICSEARCH_PREFIX + "-organization" class Django(GenericMembershipDocument.Django): model = Organization queryset_pagination = settings.ELASTICSEARCH_QUERYSET_PAGINATION fields = [ "id", "name", "short_name", "start", "end", "created", "modified" ]
def __init__(self, **kwargs): settings = { 'analyzer': analyzers.url_main_analyzer(), 'fields': { 'ngram': TextField(analyzer=analyzers.url_ngram_analyzer()), }, } settings.update(kwargs) super(URLField, self).__init__(**settings)
def __init__(self, **kwargs): settings = { 'analyzer': analyzers.standard_ascii_analyzer(), 'fields': { 'ngram': TextField(analyzer=analyzers.bigram_analyzer()), }, } settings.update(kwargs) super(CharField, self).__init__(**settings)
class QuestionDocument(Document): content = TextField() answers = TextField() def prepare_content(self, instance): # TODO remove markdown return str(instance.content) def prepare_answers(self, instance): # TODO remove markdown return " ".join(instance.answers.values_list('content', flat=True)) class Index: name = 'questions' settings = settings.ELASTIC_INDEX_SETTINGS class Django: model = m.Question fields = ['title'] ignore_signals = settings.ELASTIC_IGNORE_SIGNALS auto_refresh = settings.ELASTIC_AUTO_REFRESH queryset_pagination = settings.ELASTIC_QUERYSET_PAGINATION
def __init__(self, **kwargs): # Default field definition. settings = { 'analyzer': analyzers.email_main_analyzer(), 'fields': { 'ngram': TextField(analyzer=analyzers.email_ngram_analyzer()), }, } # Override the defaults with the custom kwargs. settings.update(kwargs) super(EmailAddressField, self).__init__(**settings)
class ExerciseDocument(Document): content = TextField() def prepare_content(self, instance): # TODO remove markdown return str(instance.raw_text) class Index: name = 'exercises' settings = settings.ELASTIC_INDEX_SETTINGS class Django: model = m.Exercise ignore_signals = settings.ELASTIC_IGNORE_SIGNALS auto_refresh = settings.ELASTIC_AUTO_REFRESH queryset_pagination = settings.ELASTIC_QUERYSET_PAGINATION
class NewsItemDocument(Document): content = TextField() def prepare_content(self, instance): # TODO remove markdown return str(instance.content) class Index: name = 'news_items' settings = settings.ELASTIC_INDEX_SETTINGS class Django: model = m.NewsItem fields = ['title'] ignore_signals = settings.ELASTIC_IGNORE_SIGNALS auto_refresh = settings.ELASTIC_AUTO_REFRESH queryset_pagination = settings.ELASTIC_QUERYSET_PAGINATION
class CourseDocument(Document): description = TextField() def prepare_description(self, instance): # TODO remove markdown return str(instance.description) class Index: name = 'course' settings = settings.ELASTIC_INDEX_SETTINGS class Django: model = m.Course fields = ['name', 'abbreviation'] ignore_signals = settings.ELASTIC_IGNORE_SIGNALS auto_refresh = settings.ELASTIC_AUTO_REFRESH queryset_pagination = settings.ELASTIC_QUERYSET_PAGINATION
class ElasticSectionDocument(Document): default_field_name = "person_name" source_document_id = IntegerField() office_id = IntegerField() position_and_department = TextField() income_size = IntegerField() spouse_income_size = IntegerField() person_id = IntegerField() region_id = IntegerField() car_brands = KeywordField() class Django: model = Section fields = ['id', 'person_name', 'income_year', 'rubric_id', 'gender'] @property def rubric_str(self): if self.rubric_id is None: return "unknown" else: return get_russian_rubric_str(self.rubric_id)
class FileDocument(Document): names = TextField() def prepare_names(self, instance): if instance.meta is None: instance.analyse() names = set( filter(lambda n: n, instance.class_files.values_list('name', flat=True))) if len(names): return " ".join(names) class Index: name = 'files' settings = settings.ELASTIC_INDEX_SETTINGS class Django: model = m.File fields = ['hash', 'name'] ignore_signals = settings.ELASTIC_IGNORE_SIGNALS auto_refresh = settings.ELASTIC_AUTO_REFRESH queryset_pagination = settings.ELASTIC_QUERYSET_PAGINATION
class PropertyDocument(Document): """Property Elasticsearch document.""" id = IntegerField(attr="id") propertyName = TextField() landlord = TextField(attr="user_indexing", ) numberOfBedrooms = IntegerField() numberOfBathrooms = IntegerField() propertyType = TextField() availableFrom = DateField() listingDescription = TextField() unit = IntegerField() size = FloatField() propertyStatus = TextField() monthlyRent = FloatField() securityDeposit = FloatField() created_at = DateField() propertyRules = ObjectField( properties={ PROPERTY: TextField(attr="property_indexing", ), PropertyRules.SMOKING: BooleanField(), PropertyRules.PET: BooleanField(), PropertyRules.MUSICAL_INSTRUMENTS: BooleanField(), }) propertyAddress = ObjectField( properties={ PROPERTY: TextField(attr="property_indexing", ), PropertyAddress.ADDRESS: TextField(), PropertyAddress.STATE_NAME: TextField(), PropertyAddress.LATITUDE: FloatField(), PropertyAddress.LONGITUDE: FloatField(), }) propertyAmenities = ObjectField( properties={ PROPERTY: TextField(attr="property_indexing", ), PropertyAmenities.POOL: BooleanField(), PropertyAmenities.GARDEN: BooleanField(), PropertyAmenities.ELEVATOR: BooleanField(), PropertyAmenities.DOORMAN: BooleanField(), PropertyAmenities.DECK: BooleanField(), PropertyAmenities.WASHER: BooleanField(), PropertyAmenities.GYM: BooleanField(), PropertyAmenities.PARKING: BooleanField(), PropertyAmenities.FIRE_PLACE: BooleanField(), PropertyAmenities.AIR_CONDITION: BooleanField(), PropertyAmenities.DISH_WASHER: BooleanField(), PropertyAmenities.ITEM_STORAGE: BooleanField(), PropertyAmenities.WHEELCHAIR: BooleanField(), PropertyAmenities.BALCONY: BooleanField(), PropertyAmenities.HARD_FLOOR: BooleanField(), PropertyAmenities.FURNISHED: BooleanField(), PropertyAmenities.VIEW: BooleanField(), PropertyAmenities.HIGH_RISE: BooleanField(), PropertyAmenities.STUDENT_FRIENDLY: BooleanField(), PropertyAmenities.UTILITIES: BooleanField(), }) propertyImage = ObjectField( properties={ ID: IntegerField(attr="id"), PROPERTY: TextField(attr="property_indexing", ), PropertyImage.IMAGE: FileField(), }) class Django(object): """The Django model associate with this Document""" model = Property