Skip to content

Commit a734457

Browse files
committed
better example
1 parent 9651566 commit a734457

3 files changed

Lines changed: 16 additions & 18 deletions

File tree

README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ from elasticsearch_metrics.imps import elastic8
4747

4848

4949
class PageView(elastic8.EventLog):
50-
user_id = metrics.Integer(index=True, doc_values=True)
50+
page_id: int
5151
```
5252

5353
Use the `sync_metrics` management command to ensure that the [index template](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html)
@@ -63,12 +63,10 @@ Now add some data:
6363
```python
6464
from myapp.metrics import PageView
6565

66-
user = User.objects.latest()
67-
6866
# By default we create an index for each day.
6967
# Therefore, this will persist the document
70-
# to an index called, e.g. "myapp_pageview_2020.02.04"
71-
PageView.record(user_id=user.id)
68+
# to an index named for the record type and date
69+
PageView.record(page_id='my.page.id')
7270
```
7371

7472
Go forth and search!
@@ -102,7 +100,7 @@ You can configure the index template settings by setting
102100

103101
```python
104102
class PageView(metrics.Metric):
105-
user_id = metrics.Integer()
103+
page_id = metrics.Integer()
106104

107105
class Index:
108106
settings = {"number_of_shards": 2, "refresh_interval": "5s"}
@@ -130,7 +128,7 @@ Alternatively, you can set `template_name` and/or `template` explicitly.
130128

131129
```python
132130
class PageView(metrics.Metric):
133-
user_id = metrics.Integer()
131+
page_id = metrics.Integer()
134132

135133
class Meta:
136134
template_name = "myapp_pviews"
@@ -144,7 +142,7 @@ from elasticsearch_metrics import metrics
144142

145143

146144
class MyBaseMetric(metrics.Metric):
147-
user_id = metrics.Integer()
145+
page_id = metrics.Integer()
148146

149147
class Meta:
150148
abstract = True

elasticsearch_metrics/imps/elastic6.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class BaseMetric(metaclass=MetricMeta):
133133
134134
135135
class PageView(metrics.Metric):
136-
user_id = metrics.Integer(index=True, doc_values=True)
136+
page_id = metrics.Integer(index=True, doc_values=True)
137137
138138
class Index:
139139
settings = {

elasticsearch_metrics/tests/test_imps_elastic6.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
class PreprintView(elastic6.Metric):
3636
provider_id = elastic6.fields.Keyword(index=True)
37-
user_id = elastic6.fields.Keyword(index=True)
37+
page_id = elastic6.fields.Keyword(index=True)
3838
preprint_id = elastic6.fields.Keyword(index=True)
3939
route_name = elastic6.fields.Text(analyzer=route_prefix_analyzer)
4040

@@ -105,7 +105,7 @@ def test_get_index_template_creates_template_with_mapping(self):
105105
assert "timestamp" in properties
106106
assert properties["timestamp"] == {"doc_values": True, "type": "date"}
107107
assert properties["provider_id"] == {"type": "keyword", "index": True}
108-
assert properties["user_id"] == {"type": "keyword", "index": True}
108+
assert properties["page_id"] == {"type": "keyword", "index": True}
109109
assert properties["preprint_id"] == {"type": "keyword", "index": True}
110110

111111
# regression test
@@ -157,7 +157,7 @@ def test_template_name_defined_with_no_template_falls_back_to_default_template(
157157

158158
def test_inheritance(self):
159159
class MyBaseMetric(elastic6.Metric):
160-
user_id = elastic6.fields.Keyword(index=True)
160+
page_id = elastic6.fields.Keyword(index=True)
161161

162162
class Index:
163163
settings = {"number_of_shards": 2}
@@ -230,10 +230,10 @@ def test_save_sends_signals(self):
230230
signals.post_save.connect(mock_post_save_listener, sender=PreprintView)
231231

232232
provider_id = "12345"
233-
user_id = "abcde"
233+
page_id = "abcde"
234234
preprint_id = "zyxwv"
235235
doc = PreprintView(
236-
provider_id=provider_id, user_id=user_id, preprint_id=preprint_id
236+
provider_id=provider_id, page_id=page_id, preprint_id=preprint_id
237237
)
238238
doc.save()
239239

@@ -259,10 +259,10 @@ def es6_client(self):
259259

260260
def test_create_document(self):
261261
provider_id = "12345"
262-
user_id = "abcde"
262+
page_id = "abcde"
263263
preprint_id = "zyxwv"
264264
doc = PreprintView(
265-
provider_id=provider_id, user_id=user_id, preprint_id=preprint_id
265+
provider_id=provider_id, page_id=page_id, preprint_id=preprint_id
266266
)
267267
doc.save()
268268
document = PreprintView.get(id=doc.meta.id, index=PreprintView.get_index_name())
@@ -274,7 +274,7 @@ def test_create_document(self):
274274
properties = mapping[name]["mappings"]["doc"]["properties"]
275275
assert properties["timestamp"] == {"type": "date"}
276276
assert properties["provider_id"] == {"type": "keyword"}
277-
assert properties["user_id"] == {"type": "keyword"}
277+
assert properties["page_id"] == {"type": "keyword"}
278278
assert properties["preprint_id"] == {"type": "keyword"}
279279

280280

@@ -290,7 +290,7 @@ def test_init(self):
290290
properties = mapping[name]["mappings"]["doc"]["properties"]
291291
assert properties["timestamp"] == {"type": "date"}
292292
assert properties["provider_id"] == {"type": "keyword"}
293-
assert properties["user_id"] == {"type": "keyword"}
293+
assert properties["page_id"] == {"type": "keyword"}
294294
assert properties["preprint_id"] == {"type": "keyword"}
295295

296296
def test_check_index_template(self):

0 commit comments

Comments
 (0)