# How to create a template based on a Lucene document?

**URL:** <https://discuss.elastic.co/t/how-to-create-a-template-based-on-a-lucene-document/10438>\
**Category:** Elasticsearch\
**Created:** [January 21, 2013, 2:52pm UTC](https://discuss.elastic.co/t/how-to-create-a-template-based-on-a-lucene-document/10438 "2013-01-21T14:52:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tullio\_Coppotelli](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@Tullio\_Coppotelli](https://discuss.elastic.co/u/Tullio_Coppotelli)\
**Post date:** [January 21, 2013, 2:52pm UTC](https://discuss.elastic.co/t/how-to-create-a-template-based-on-a-lucene-document/10438/1 "2013-01-21T14:52:29Z")

</div>

Hello everyone,  
I'm new to ElasticSearch and I need to migrate an application that is  
indexing documents with Lucene to ES.  
I set up the environment and run few tests.  
Coming from Lucene, I have already defined all my index schema (read  
documents) and I want to maintain exactly the same indexing approach.

This is the Lucene document:  
public static Document getDocument(ResultSet rs) throws SQLException {  
Document doc = new Document();  
String postcode = rs.getString("POSTCODE");  
doc.add(new Field("postcode", postcode,  
Field.Store.YES,Field.Index.ANALYZED));

```
doc.add(new Field("longitude", 

```

Util.encodeLongitudeForIndex(rs.getDouble("LONGITUDE")), Field.Store.YES,  
Field.Index.NOT\_ANALYZED));

```
doc.add(new Field("latitude", 

```

Util.encodeLatitudeForIndex(rs.getDouble("LATITUDE")), Field.Store.YES,  
Field.Index.NOT\_ANALYZED));

```
return doc;

```

}

I have some problem understanding how to map this in ES and the meaning of  
the syntax to create a schema template.  
I created a template like this:

curl -XPUT localhost:9200/\_template/\*postcode \*-d '  
{  
"template" : "_postcode_",  
"settings" : {  
"number\_of\_shards" : 1  
},  
"mappings" : {  
"_postcode_" : {  
"\_source" : { "enabled" : false },  
"properties" : {  
"postcode" : { "type" : "string", "index" : "analyzed",  
"store" : "yes" },  
"longitude" : { "type" : "string", "index" : "not\_analyzed", "store" :  
"yes" },  
"latitude" : { "type" : "string", "index" : "not\_analyzed", "store" : "yes"  
}  
}  
}  
}  
}

Postcode is the type of the document in Lucene.  
Now it looks like I've more objects with the same _postcode_ name in ES:

1. I need to use postcode in the url so that ES knows what I am talking  
about. This should identify the name of the template.
2. I need to inform ES that I want to map calls to postcode to this  
template
3. When I declare the mapping again it looks like I've to provide an id

Am I doing it right?  
When I index a document is there a way to look at the indexed document in a  
way similar to Luke for Lucene?

Thank you for your help,  
Tullio

--

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [January 22, 2013, 10:57am UTC](https://discuss.elastic.co/t/how-to-create-a-template-based-on-a-lucene-document/10438/2 "2013-01-22T10:57:27Z")

</div>

Hiya

I'm not completely sure what you're trying to achieve in your code, but I  
_think_ you might have misunderstood some terminology.

In Elasticsearch:

- An "index" is like a database
- A "type" is like a table in the database
- A "mapping" is like the column definition in the table
- A "document" is like a row in the table

So presumably, you have a "type" called "postcode. You create a mapping  
for that type (once).

Then you index documents into that type, specifying: / index / type / ID,  
eg "/my\_index/postcode/12345"  
You can then retrieve that document using the same parameters.

The ID could be the postcode itself.

Apologies if I haven't understood what you're trying to achieve (eg not  
sure why you're trying to use templates here?)

Re examining the document: the easiest way is probably just to use the  
terms facet to see what values have been indexed.

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Also, you're disabling \_source and then setting all of your fields to  
stored. This is an anti-pattern. It is almost always more efficient to  
retrieve the single \_source field than to retrieve a list of stored fields,  
as each field requires a disk seek.

clint

On Mon, Jan 21, 2013 at 3:52 PM, Tullio Coppotelli [coppotelli@gmail.com](mailto:coppotelli@gmail.com)wrote:

> Hello everyone,  
> I'm new to Elasticsearch and I need to migrate an application that is  
> indexing documents with Lucene to ES.  
> I set up the environment and run few tests.  
> Coming from Lucene, I have already defined all my index schema (read  
> documents) and I want to maintain exactly the same indexing approach.
> 
> This is the Lucene document:  
> public static Document getDocument(ResultSet rs) throws SQLException {  
> Document doc = new Document();  
> String postcode = rs.getString("POSTCODE");  
> doc.add(new Field("postcode", postcode,  
> Field.Store.YES,Field.Index.ANALYZED));
> 
> ```
> doc.add(new Field("longitude",
> 
> ```
> 
> Util.encodeLongitudeForIndex(rs.getDouble("LONGITUDE")), Field.Store.YES,  
> Field.Index.NOT\_ANALYZED));
> 
> ```
> doc.add(new Field("latitude",
> 
> ```
> 
> Util.encodeLatitudeForIndex(rs.getDouble("LATITUDE")), Field.Store.YES,  
> Field.Index.NOT\_ANALYZED));
> 
> ```
> return doc;
> 
> ```
> 
> }
> 
> I have some problem understanding how to map this in ES and the meaning of  
> the syntax to create a schema template.  
> I created a template like this:
> 
> curl -XPUT localhost:9200/\_template/\*postcode \*-d '  
> {  
> "template" : "_postcode_",  
> "settings" : {  
> "number\_of\_shards" : 1  
> },  
> "mappings" : {  
> "_postcode_" : {  
> "\_source" : { "enabled" : false },  
> "properties" : {  
> "postcode" : { "type" : "string", "index" : "analyzed",  
> "store" : "yes" },  
> "longitude" : { "type" : "string", "index" : "not\_analyzed", "store" :  
> "yes" },  
> "latitude" : { "type" : "string", "index" : "not\_analyzed", "store" :  
> "yes" }  
> }  
> }  
> }  
> }
> 
> Postcode is the type of the document in Lucene.  
> Now it looks like I've more objects with the same _postcode_ name in ES:
> 
> 1. I need to use postcode in the url so that ES knows what I am  
> talking about. This should identify the name of the template.
> 2. I need to inform ES that I want to map calls to postcode to this  
> template
> 3. When I declare the mapping again it looks like I've to provide an id
> 
> Am I doing it right?  
> When I index a document is there a way to look at the indexed document in  
> a way similar to Luke for Lucene?
> 
> Thank you for your help,  
> Tullio
> 
> --

--

---

<div class="post-metadata">

**Author:** ![Tullio\_Coppotelli](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@Tullio\_Coppotelli](https://discuss.elastic.co/u/Tullio_Coppotelli)\
**Post date:** [January 22, 2013, 3:18pm UTC](https://discuss.elastic.co/t/how-to-create-a-template-based-on-a-lucene-document/10438/3 "2013-01-22T15:18:33Z")

</div>

Hi Clint,  
Thank you for your reply. Actually you helped a lot.  
U were totally right, I was confused about the terminology. Moreover I was  
using template without a reason.  
I was not familiar with the concept of source, since it is not in lucene.  
it was there only as a copy and paste error. I'm happy you pointed it out  
because now I'm starting to think at the advantages of it.

At the end what I wanted was something like this:  
post on [http://localhost:9200/myproject/](http://localhost:9200/myproject/)  
"settings" : {  
"number\_of\_shards" : 1,  
"number\_of\_replicas" : 1  
},  
"mappings" : {  
"postcode" : {  
"\_source" : { "enabled" : true  
},  
"properties" : {  
"postcode" : { "type" : "string", "index" : "analyzed",  
"store" : "no" },  
"combined\_postcode" : { "type" : "string", "index" : "analyzed", "store" :  
"no" },  
"longitude" : { "type" : "string", "index" : "not\_analyzed", "store" : "no"  
},  
"latitude" : { "type" : "string", "index" : "not\_analyzed", "store" : "no" }  
}  
}  
}

Now I'm moving forward and looking at percolation.  
Thank you again,  
Tullio

On Tuesday, January 22, 2013 10:57:27 AM UTC, Clinton Gormley wrote:

> Hiya
> 
> I'm not completely sure what you're trying to achieve in your code, but I  
> _think_ you might have misunderstood some terminology.
> 
> In Elasticsearch:
> 
> - An "index" is like a database
> - A "type" is like a table in the database
> - A "mapping" is like the column definition in the table
> - A "document" is like a row in the table
> 
> So presumably, you have a "type" called "postcode. You create a mapping  
> for that type (once).
> 
> Then you index documents into that type, specifying: / index / type / ID,  
> eg "/my\_index/postcode/12345"  
> You can then retrieve that document using the same parameters.
> 
> The ID could be the postcode itself.
> 
> Apologies if I haven't understood what you're trying to achieve (eg not  
> sure why you're trying to use templates here?)
> 
> Re examining the document: the easiest way is probably just to use the  
> terms facet to see what values have been indexed.  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/terms-facet.html)
> 
> Also, you're disabling \_source and then setting all of your fields to  
> stored. This is an anti-pattern. It is almost always more efficient to  
> retrieve the single \_source field than to retrieve a list of stored fields,  
> as each field requires a disk seek.
> 
> clint
> 
> On Mon, Jan 21, 2013 at 3:52 PM, Tullio Coppotelli \<[coppo...@gmail.com](mailto:coppo...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Hello everyone,  
> > I'm new to Elasticsearch and I need to migrate an application that is  
> > indexing documents with Lucene to ES.  
> > I set up the environment and run few tests.  
> > Coming from Lucene, I have already defined all my index schema (read  
> > documents) and I want to maintain exactly the same indexing approach.
> > 
> > This is the Lucene document:  
> > public static Document getDocument(ResultSet rs) throws SQLException {  
> > Document doc = new Document();  
> > String postcode = rs.getString("POSTCODE");  
> > doc.add(new Field("postcode", postcode,  
> > Field.Store.YES,Field.Index.ANALYZED));
> > 
> > ```
> > doc.add(new Field("longitude", 
> > 
> > ```
> > 
> > Util.encodeLongitudeForIndex(rs.getDouble("LONGITUDE")), Field.Store.YES,  
> > Field.Index.NOT\_ANALYZED));
> > 
> > ```
> > doc.add(new Field("latitude", 
> > 
> > ```
> > 
> > Util.encodeLatitudeForIndex(rs.getDouble("LATITUDE")), Field.Store.YES,  
> > Field.Index.NOT\_ANALYZED));
> > 
> > ```
> > return doc;
> > 
> > ```
> > 
> > }
> > 
> > I have some problem understanding how to map this in ES and the meaning  
> > of the syntax to create a schema template.  
> > I created a template like this:
> > 
> > curl -XPUT localhost:9200/\_template/\*postcode \*-d '  
> > {  
> > "template" : "_postcode_",  
> > "settings" : {  
> > "number\_of\_shards" : 1  
> > },  
> > "mappings" : {  
> > "_postcode_" : {  
> > "\_source" : { "enabled" : false },  
> > "properties" : {  
> > "postcode" : { "type" : "string", "index" : "analyzed",  
> > "store" : "yes" },  
> > "longitude" : { "type" : "string", "index" : "not\_analyzed", "store" :  
> > "yes" },  
> > "latitude" : { "type" : "string", "index" : "not\_analyzed", "store" :  
> > "yes" }  
> > }  
> > }  
> > }  
> > }
> > 
> > Postcode is the type of the document in Lucene.  
> > Now it looks like I've more objects with the same _postcode_ name in ES:
> > 
> > 1. I need to use postcode in the url so that ES knows what I am  
> > talking about. This should identify the name of the template.
> > 2. I need to inform ES that I want to map calls to postcode to this  
> > template
> > 3. When I declare the mapping again it looks like I've to provide an  
> > id
> > 
> > Am I doing it right?  
> > When I index a document is there a way to look at the indexed document  
> > in a way similar to Luke for Lucene?
> > 
> > Thank you for your help,  
> > Tullio
> > 
> > --

--

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 2:55am UTC](https://discuss.elastic.co/t/how-to-create-a-template-based-on-a-lucene-document/10438/4 "2017-07-06T02:55:16Z")

</div>


