Ignore fields at Elastic Search

While I was developing with the Play framework and Elastic Search, I faced a really annoying problem.

In my model I had a Date field. It seems that Elastic Search could not convert it to a valid format of it’s own and I got the following exception:

org.elasticsearch.index.mapper.MapperParsingException: Failed to parse [added]
	at org.elasticsearch.index.mapper.core.AbstractFieldMapper.parse(AbstractFieldMapper.java:309)
	at org.elasticsearch.index.mapper.object.ObjectMapper.serializeValue(ObjectMapper.java:569)
	at org.elasticsearch.index.mapper.object.ObjectMapper.parse(ObjectMapper.java:441)
	at org.elasticsearch.index.mapper.DocumentMapper.parse(DocumentMapper.java:567)
	at org.elasticsearch.index.mapper.DocumentMapper.parse(DocumentMapper.java:491)
	at org.elasticsearch.index.shard.service.InternalIndexShard.prepareIndex(InternalIndexShard.java:289)
	at org.elasticsearch.action.index.TransportIndexAction.shardOperationOnPrimary(TransportIndexAction.java:185)
	at org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction.performOnPrimary(TransportShardReplicationOperationAction.java:428)
	at org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction$1.run(TransportShardReplicationOperationAction.java:341)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
	at java.lang.Thread.run(Thread.java:662)
Caused by: org.elasticsearch.index.mapper.MapperParsingException: failed to parse date field, tried both date format [dateOptionalTime], and timestamp number
	at org.elasticsearch.index.mapper.core.DateFieldMapper.parseStringValue(DateFieldMapper.java:343)
	at org.elasticsearch.index.mapper.core.DateFieldMapper.parseCreateField(DateFieldMapper.java:280)
	at org.elasticsearch.index.mapper.core.AbstractFieldMapper.parse(AbstractFieldMapper.java:296)
	... 11 more
Caused by: java.lang.IllegalArgumentException: Invalid format: "2012-03-01 00:00:00.0" is malformed at " 00:00:00.0"
	at org.elasticsearch.common.joda.time.format.DateTimeFormatter.parseMillis(DateTimeFormatter.java:644)
	at org.elasticsearch.index.mapper.core.DateFieldMapper.parseStringValue(DateFieldMapper.java:338)
	... 13 more

Since I didn’t want to search the Date field with Elastic Search at the first place, I found out I could set elastic search to ignore that field and since I’m only interested in searching the “name” and the “description” fields, I could add the @ElasticSearchIgnore annotation at all the other fields.

Now Elastic Search doesn’t bother to parse the Date field nor the others. To be honest, I didn’t found a solution for the Date conversion, but, hey, it works this way ;)

One feature I would like to see (I didn’t found out anything) is to be able to explicitly tell only which fields I want to be searched and exclude all the others.

Share

elastic search!

If you don’t know what elastic search is, you should read about it immediately.
Almost everything you are developing or may develop contains a search section.

As the creator of elastic search are declairing at the project’s first page:

So, we build a web site or an application and want to add search to it, and then it hits us: getting search working is hard. We want our search solution to be fast, we want a painless setup and a completely free search schema, we want to be able to index data simply using JSON over HTTP, we want our search server to be always available, we want to be able to start with one machine and scale to hundreds, we want real-time search, we want simple multi-tenancy, and we want a solution that is built for the cloud.

I use elastic search for the GreekAndroidApps project, I’m developing it with the play framework and it cames handy with a play module ready to use.

One feature I really love (among the others) is the fuzzy search.
This line does a fuzzy search on the “title” or the “description” fields.

BoolQueryBuilder qb1 = boolQuery().should(fuzzyQuery("title", searchString)).should(fuzzyQuery("description", searchString));

So, goodbye “select * from item where content like ‘%term%'” queries!!!

Share