adamfairholm / Elasticquent

Map Larvel Eloquent models to Elasticsearch types
MIT License
201 stars 38 forks source link

creating index, put mapping and complex type storing #16

Closed zkrhm closed 9 years ago

zkrhm commented 9 years ago

hello @adamfairholm , I am fairly new to laravel development so forgive me if I ask a stupid question.

  1. Elasticquent provide method for creating index and put mapping. my question is if I decided to use elasticquent instead of using elasticsearch API, when and where it should be called on my laravel application?
  2. when using elasticsearch api I can store complex structure by storing it in json formatted document. can I do this by using elasticquent? do I have to take other approach like using relationship?

thank you

yswery commented 9 years ago

To achieve mappings you will need to add something like following into your Elasticquent model:

protected $mappingProperties = [
           id' => [
                'type' => 'integer',
            ],
            'name' => [
                'type' => 'string',
                'analyzer' => 'string_lowercase'
            ]
];

and in your app you will need to do something like this:

$users = User::all();
$users->rebuildMapping();
zkrhm commented 9 years ago

hi @yswery thank you for replying. what about index creation can I use migration to do it? since when I use elasticsearch API usually I only need to execute it once.

yswery commented 9 years ago

If you wished for example to do the one off current DB index $users->addToIndex() for example. You can use a migration to do this, but instead what I ended up doing with mine was creating a command in laravel http://laravel.com/docs/4.2/commands

In theory this will enable you run this manually just like "flushing cache" would be.

After that everytime a new user registers you would index them after your save() method is called for example.

adamfairholm commented 9 years ago

Thanks @yswery - I usually have these set up as commands as well. @zkrhm - you can also definitely use the Elasticsearch API with this - the mapping functionality in Elasticquent is more of just a convenience feature - there is no other processes that go on that cannot just be performed with the PHP API they provide.

As for your second question - you can control what each model stores by overriding the getIndexDocumentData function. Whenever Elasticquent stores a model's data in Elasticsearch, it uses this function. By default it just returns the array of data, but you can extend it to store whatever custom structure that you'd like to.