Skip to content

Commit 04c9626

Browse files
committed
Update doc
1 parent 6659cd5 commit 04c9626

File tree

1 file changed

+59
-51
lines changed

1 file changed

+59
-51
lines changed

README.md

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
### Introducing the ElasticSearch View Plugin
1+
# Introducing the ElasticSearch View Plugin
22

3-
ElasticSearch fournit un moyen simple et rapide de récupérer un document indexé grâce à l'API Get. Jusqu'à présent, cette
4-
API ne permet de récupérer un document qu'au format JSON:
5-
6-
````
3+
Elasticsearch provides a fast and simple way to retrieve a document with the [GET API](http://www.elasticsearch.org/guide/reference/api/get.html).
4+
```
75
curl -XGET 'http://localhost:9200/catalog/product/1'
6+
```
87

9-
8+
Until now, this API only allows to get the document in JSON format:
9+
```
1010
{
1111
"_index": "catalog",
1212
"_type": "product",
@@ -20,7 +20,11 @@ curl -XGET 'http://localhost:9200/catalog/product/1'
2020
...
2121
}
2222
}
23-
````
23+
```
24+
25+
Although this format is really useful, it is not directly presentable to a final user. It is more dedicated to
26+
applications which are in charge of the
27+
The goal of these applications is to generate a graphic result of one or more documents for instance on HTML page. They need a specific executive environment (...) which is necessary to install, configure and maintain. as well as regular deliveries of the application when the graphic content of one document is modified.
2428

2529
Bien que très pratique, ce format n'est pas directement présentable à un utilisateur final. Il est plutot destiné à être
2630
exploité par des applications tierces dont le rôle est de générer un rendu graphique d'un ou plusieurs documents, par
@@ -50,22 +54,19 @@ scripts prédéfinis, ou encore de créer une vue sur-mesure pour afficher le r
5054

5155
## Installing the plugin
5256

53-
|_. elasticsearch-view-plugin |_. ElasticSearch|
54-
| master (0.0.1) | 0.20.1 |
55-
56-
In order to install the plugin, simply run:
57+
The plugin can be installed as any other ElasticSearch's plugins:
5758

5859
```
5960
bin/plugin -install tlrx/elasticsearch-view-plugin/0.0.1
6061
6162
```
6263

63-
And start your ElasticSearch cluster.
64+
The current version of the plugin is compatible with [ElasticSearch 0.20.1](http://www.elasticsearch.org/download/2012/12/07/0.20.1.html).
6465

6566

6667
## Creating views for existing documents
6768

68-
Let's imagine that we have a `catalog` index in which we indexed many documents of `product` type:
69+
Let's imagine that we have a `catalog` index and few documents of `product` type:
6970
```
7071
curl -XPUT 'http://localhost:9200/catalog/product/1' -d '
7172
{
@@ -82,12 +83,13 @@ curl -XPUT 'http://localhost:9200/catalog/product/1' -d '
8283
```
8384

8485
ElasticSearch View Plugin uses the mapping's [meta data](http://www.elasticsearch.org/guide/reference/mapping/meta.html)
85-
to store all the views that are associated with a specific document type. Each view has a unique name, a template language
86-
and a template content.
86+
to store all the views that are associated with a specific document type. Each view has a unique name, a scripting language,
87+
a content and eventually a content type.
8788

88-
Be careful, as the [Update API](http://www.elasticsearch.org/guide/reference/api/update.html), the `_source` field need to be enabled for this feature to work.
89+
Be careful, as the [Update API](http://www.elasticsearch.org/guide/reference/api/update.html), the `_source` field need
90+
to be enabled for this feature to work.
8991

90-
Let's create a basic view using the [MVEL templating](http://mvel.codehaus.org/MVEL+2.0+Basic+Templating) language:
92+
First, we can create a basic view using the [MVEL templating](http://mvel.codehaus.org/MVEL+2.0+Basic+Templating) language:
9193
```
9294
curl -XPUT 'http://localhost:9200/catalog/product/_mapping' -d '
9395
{
@@ -106,17 +108,23 @@ curl -XPUT 'http://localhost:9200/catalog/product/_mapping' -d '
106108

107109
The previous command creates a view called `default`. The property `view_lang` can be used to specify the templating
108110
engine to use (default is `mvel`) whereas the `view` property holds the content of the view. When needed, a specific `content_type`
109-
can be set. The view named `default` will be used by default to render the documents of type `product`.
111+
can be set. Note that the view named `default` will be used by default to render the documents of type `product`.
112+
113+
In MVEL, the coordinates of the document are available with `@{_id}`, `@{_type}` and `@{_index}` instructions. The original
114+
`_source` of the document can be accessed with `@{_source._x_}` where _x_ is a document property name.
115+
116+
Now the view is created, opening the URL `http://localhost:9200/_view/catalog/product/1` in a web browser will trigger
117+
the rendering of document with id 1. The result looks like:
110118

111-
Now the view is created, opening the URL `http://localhost:9200/_view/catalog/product/1` in a web browser will trigger the rendering of document with id 1.
119+
![Default view for product #1](https://raw.github.com/tlrx/elasticsearch-view-plugin/gh-pages/samples/render_default.png)
112120

113-
The result looks like:
121+
Simple, no?
114122

115-
imag render_default.png
116123

117124
### Using multiple views
118125

119-
Many views can be defined for the same type of document, allowing differents renderings of the same document:
126+
In most use cases, a unique view is not sufficient. That's why the plugins allows to define many views for the same type of document,
127+
allowing differents renderings of the same document:
120128

121129
```
122130
curl -XPUT 'http://localhost:9200/catalog/product/_mapping' -d '
@@ -140,9 +148,9 @@ curl -XPUT 'http://localhost:9200/catalog/product/_mapping' -d '
140148
```
141149

142150

143-
The URL `http://localhost:9200/_view/catalog/product/1/xml` can be used to access to the XML view of document 1:
151+
This way the URL `http://localhost:9200/_view/catalog/product/1/xml` can be used to access to the XML view of document 1:
144152

145-
image render_xml.png
153+
![XML view for product #1](https://raw.github.com/tlrx/elasticsearch-view-plugin/gh-pages/samples/render_xml.png)
146154

147155

148156
### Rendering binary fields
@@ -167,12 +175,11 @@ curl -XPUT 'http://localhost:9200/catalog/product/1' -d '
167175
168176
```
169177

170-
The picture field contains a base64 encoded image of Harley Davidson's logo.
178+
The picture field contains a base64 encoded image of the Harley Davidson's logo.
171179

172180
We can now define two more views:
173-
* logo: which renders the picture as binary content
174-
* full: which renders the document as HTML content
175-
181+
* **logo**: which renders the picture as binary content
182+
* **full**: which renders the document as HTML block
176183

177184
```
178185
curl -XPUT 'http://localhost:9200/catalog/product/_mapping' -d '
@@ -206,26 +213,26 @@ curl -XPUT 'http://localhost:9200/catalog/product/_mapping' -d '
206213
The URL `http://localhost:9200/_view/catalog/product/1/logo` can be used to get the picture of the product, whereas
207214
`http://localhost:9200/_view/catalog/product/1/full` renders the full HTML view:
208215

209-
image render_html.png
210-
216+
![HTML view of document #1](https://raw.github.com/tlrx/elasticsearch-view-plugin/gh-pages/samples/render_html.png)
211217

212218

213219
## Using preloaded templates
214220

215221
Similar to the [scripting module](http://www.elasticsearch.org/guide/reference/modules/scripting.html), the ElasticSearch
216222
View Plugin supports predefined templates scripts.
217223

218-
The scripts can be placed under the `config/views` directory and then referencing them by the script name. The way to
224+
The scripts must be placed under the `config/views` directory and then referencing them by the script name. The way to
219225
reference a script differs according to the view language.
220226

221227
For example, we can create the file `config/views/copyright.mv` with the following content:
222228
```
223229
<p>© Copyright @{_source.brand}</p>
224230
```
225231

226-
The `.mv` extension indicates that the file contains a template in MVEL language.
232+
The `.mv` extension indicates that the file contains a template written in MVEL.
227233

228-
After a cluster restart, we will be able to update the `full` view in order to use the preloaded template script:
234+
After a cluster restart, we will be able to update the `full` view in order to use the preloaded template script (note the
235+
@includeNamed{} instruction):
229236
```
230237
...
231238
"full": {
@@ -235,12 +242,14 @@ After a cluster restart, we will be able to update the `full` view in order to u
235242
...
236243
```
237244

245+
Preloaded templates are great candidates for code o text that are used in mulitple views.
246+
238247
## Creating complete views from queries
239248

240249
The plugin allows to create custom views from query hits. Everytime such a view is requested, a set of predefined queries
241-
are executed and the results are used to create the view.
250+
are executed and the results are used to create the view. Such views are stored in ElasticSearch as documents.
242251

243-
This kind of view are really powerful and are a simple way to create complete web pages.
252+
This kind of view is really powerful and are a simple way to create complete web pages.
244253

245254
First, let's create a more complex template called `list-of-products` and stored in the file `config/views/list-of-products.mv`:
246255
```html
@@ -252,8 +261,6 @@ First, let's create a more complex template called `list-of-products` and stored
252261
<meta name="viewport" content="width=device-width, initial-scale=1.0">
253262
<meta name="description" content="">
254263
<meta name="author" content="">
255-
256-
<!-- Le styles -->
257264
<link href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" rel="stylesheet">
258265
<style>
259266
body {
@@ -268,7 +275,6 @@ First, let's create a more complex template called `list-of-products` and stored
268275
</head>
269276

270277
<body>
271-
272278
<div class="navbar navbar-inverse navbar-fixed-top">
273279
<div class="navbar-inner">
274280
<div class="container">
@@ -286,7 +292,6 @@ First, let's create a more complex template called `list-of-products` and stored
286292
</div>
287293
</div>
288294
</div>
289-
290295
<div class="container">
291296
<h1>List of products with scale 1:10</h1>
292297
<table class="table table-striped">
@@ -310,12 +315,11 @@ First, let's create a more complex template called `list-of-products` and stored
310315
</tbody>
311316
</table>
312317
</div>
313-
314318
</body>
315319
</html>
316320
```
317321

318-
Next, we can create the view:
322+
Next, we can create a view:
319323
```
320324
curl -XPUT "http://localhost:9200/catalog/list-of-products-by-size/1:10" -d "{
321325
\"views\":{
@@ -340,7 +344,8 @@ curl -XPUT "http://localhost:9200/catalog/list-of-products-by-size/1:10" -d "{
340344
}"
341345
```
342346

343-
This view is called `default` (but could have another name) and uses the `list-of-products` template to render a list
347+
Note that the view is indexed in `catalog` index with the `list-of-products-by-size` document type and id `1:10`. It defines
348+
a view called `default` (but could have another name) and uses the `list-of-products.mv` template to render a list
344349
of products.
345350

346351
The list of products is defined by the `products_with_size_1_10` query in the `queries` field of the view. This query
@@ -364,9 +369,7 @@ final HTML page. Of course, multiple queries can be used in the same view.
364369

365370
The result is available at `http://localhost:9200/_view/catalog/list-of-products-by-size/1:10` and looks like:
366371

367-
img render_html_list.png
368-
369-
These kind of view are indexed as normal ElasticSearch docs.
372+
![List of products with scale 1:10](https://raw.github.com/tlrx/elasticsearch-view-plugin/gh-pages/samples/render_html_list.png)
370373

371374

372375
## Going further...
@@ -376,22 +379,27 @@ These kind of view are indexed as normal ElasticSearch docs.
376379
The plugin [elasticsearch-view-mustache-plugin](https://github.com/tlrx/elasticsearch-view-mustache-plugin) adds
377380
[Mustache](http://mustache.github.com/) as templating language for views.
378381

379-
Mustache is a great templating engine that supports template encapsulation.
382+
Mustache is a great templating engine that supports template encapsulation. To defined views with Mustache template engine,
383+
use `"view_lang": "mustache"`.
384+
385+
Some sample usage of this plugin can be found in the Github project.
380386

381-
### Rewriting URL with Apache2
387+
### Rewriting URLs with Apache2
382388

383-
Apache2 server with mod_proxy and mod_rewrite can be used to redirect ElasticSearch Views Plugin URLs with some configuration:
389+
Apache2 server with mod_proxy and mod_rewrite can be used to redirect ElasticSearch Views Plugin URLs to better looking URLs.
384390

391+
The goal is to have nicer URLs like `http://www.domain.com/catalog/list-of-products-by-size/1:10` that points
392+
to internal `http://localhost:9200/_view/catalog/list-of-products-by-size/1:10`.
393+
394+
Here is a basic sample of such URL rewriting:
385395
```
386396
RewriteEngine on
387397
RewriteRule ^catalog/(.*)$ http://localhost:9200/_view/catalog/$1 [P,L]
388398
```
389399

390-
This way, the URL `http://www.domain.com/catalog/list-of-products-by-size/1:10` points to `http://localhost:9200/_view/catalog/list-of-products-by-size/1:10`.
391-
392400

393401

394-
Hope this plugin will be as useful as it is for us :)
402+
We hope that this plugin will be as useful for you as it is for us, and we welcome your feedback and comments about this new plugin.
395403

396404

397405

0 commit comments

Comments
 (0)