2

im trying to use elasticsearch to search through products. If product is a car for instance, it will have some field like "color", "brand", "model", "km", ... If it is clothes, it will only have "color", "size", ... I would like to index all this info in elastic to be able then to search cars with km between aaa km and bbb km, and / or xxxx model, same for clothes or any other products.

how can I create such field(s) in elasticsearch ? I want all products to be in same index, so user can search through all products, but also if user search a type a product, then he should be able to specify some more details according to this kind of product.

I was thinking about array field, but does that mean that all products will have all fields corresponding to all type of products even if some fields are not relevant with some products (ie clothes will have km field ??) ? Or is it possible on indexing to put just info needed corresponding to each product ?

thanks

user2016483
  • 573
  • 1
  • 5
  • 12

1 Answers1

0

You could use types. Create a type called car with fields color, brand, model, k etc. and then a type called cloth with fields color, size, etc.

A single index can have many types. The following two links might help you in this:

  1. Creating indices

  2. Creating types and mapping to the index

You could easily search across types so that you could issue a search like this to return all documents form all types within that index:

curl -XGET http://localhost:9200/_search?pretty=true -d '{"query":{"matchAll":{}}}'

Additional information - Searching across types

Having an array field is not a good idea since you would not be utilizing the ability of elasticsearch to index semi structured documents.

All the best.

Sai
  • 441
  • 7
  • 24
  • 1
    thanks for answer. It seems to be what I need. But Is there a way to optimize or to make kind of inheritance, as all products have some common fields, like price, category, date, images, ... – user2016483 Jun 25 '13 at 13:13
  • There is no direct way to bring the kind of inheritance you require into elasticsearch, but you could easily simulate the same behavior by defining the same fields in all the types just once in the mapping phase of insertion. Once defined the inserts can be made and search can be executed as though there is an inheritance among the fields. – Sai Jun 25 '13 at 14:18
  • As for optimizing the process, have a look at http://stackoverflow.com/questions/15694724/shards-and-replicas-in-elasticsearch and also at bulk indexing. All the best. – Sai Jun 25 '13 at 14:26
  • 1
    thanks again for answer. Im using rails and tire. Do you know maybe if this is possible to do ? I was searching for examples but I couldnt. – user2016483 Jun 25 '13 at 17:42
  • do you mean adding the indices and types? or the sharding & replication? – Sai Jun 25 '13 at 18:47
  • I mean the mapping phase of insertion. – user2016483 Jun 25 '13 at 19:16
  • 1
    currently my mapping is like this mapping do indexes :id, type: 'integer', :index => :not_analyzed indexes :category_id, type: 'integer', :index => :not_analyzed indexes :created_at, type: 'date', :index => :not_analyzed indexes :price, type: 'float', :index => :not_analyzed indexes :photo, type: 'boolean', :index => :not_analyzed indexes :title indexes :text end PS : sorry for format, it seems it is not possible to put new line in comment – user2016483 Jun 25 '13 at 19:19
  • Ok try this link on using elasticsearch using rails and tire. https://github.com/karmi/tire It has a section for mapping. It looks like you're almost there. Also try this link http://robert-reiz.com/2013/01/09/ruby-on-rails-elasticsearch/. – Sai Jun 25 '13 at 19:50
  • I already read this two page before, ut didnt saw how to make kind of inheritance you were seaking in your first comment. Or maybe i missunderstood what u were telling. – user2016483 Jun 25 '13 at 22:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32361/discussion-between-sai-and-user2016483) – Sai Jun 25 '13 at 22:21
  • 1
    I finally choose to go with dynamic mapping, as I just had to add one line to my mapping to make it works. – user2016483 Jun 27 '13 at 13:00
  • 1
    anyway thanks for your help ;-) since i did the change, from time to time, elasticsearch start to be very long and I have to restart server. But as I also make some other changes, Im not sure it is linked. Do you know maybe how I can figure what is wrong ? (I created another post for this problem : http://stackoverflow.com/questions/17344460/rails-tire-elasticsearch-need-to-restart-elasticsearch-server ) – user2016483 Jun 29 '13 at 00:34
  • elasticsearch has a lot of dependence on heap space. Tty increasing the heap space. Other than that, if it is a specific issue to rails, I have no experienz with that. – Sai Jul 01 '13 at 15:08