Skip to content

Commit

Permalink
merge search
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Naumov committed May 19, 2016
1 parent 72e4bc4 commit c109a03
Show file tree
Hide file tree
Showing 46 changed files with 3,053 additions and 233 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ php:
- 7.0
- hhvm

sudo: true

matrix:
fast_finish: true
allow_failures:
- php: hhvm

before_install:
- curl -L -O https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.3.3/elasticsearch-2.3.3.tar.gz
- tar -xvf elasticsearch-2.3.3.tar.gz

install:
- travis_retry composer self-update && composer --version
- travis_retry composer global require fxp/composer-asset-plugin:~1.0
Expand All @@ -20,6 +26,8 @@ before_script:
- mysql -e 'create database yii2_datastructure;'

script:
- elasticsearch-2.3.3/bin/elasticsearch &
- sleep 10
- vendor/bin/phpunit --coverage-clover=coverage.xml --verbose $PHPUNIT_FLAGS

after_script:
Expand Down
134 changes: 134 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,138 @@ TBD
Credits and inspiration sources
-------------------------------

Search concept
--------------
*for now it works only with STATIC VALUES property storage*

*Note, that search will work only with models with `DevGroup\DataStructure\traits\PropertiesTrait` trait
and `DevGroup\DataStructure\behaviors\HasProperties` behavior connected. See [how to connect (Russian for now)](/docs/ru/how-to-use.md)*

Extension provides flexible system of search. Each property have configuration point that switches ability to use this property in search.

Basic search will be done in two ways:

- common search against regular databases e.g.: `mysql`, `mariadb` etc;
- elasticsearch indices search.

Main feature is that if you want to use elasticseatch, and defined it in the app config,
but not still configure it your search will just work fine with auto fallback to simple mysql searching. And when your
elasticsearch will be properly started search will be automatically switched for elasticseatch.

Preferred search mechanism you can define in the application configuration files, like this:
```
'modules' => [
...
'properties' => [
'class' => 'DevGroup\DataStructure\Properties\Module',
'searchClass' => \DevGroup\DataStructure\search\elastic\Search::class,
'searchConfig' => [
'hosts' => ['host1:9200', 'https://host2:9200'],
'watcherClass' => MyWatch::class,
]
],
...
],
```

- `searchClass` - class to be used for search. If omit - there will be no search configured,
- `searchConfig` - array additional parameters to be applied for search object, except common search.
For elastic search following special keys may be set:
- `hosts` see [hosts config](https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_configuration.html),
- `watcherClass` - you can use your own watcher for elsticsearch if needed.

If you want to start using elasticsearch, first of all you have to [install and configure it](https://www.elastic.co/guide/en/elasticsearch/reference/current/setup.html).

Then, if you already have entries in your database you may want to generate and load start indices. For this run in console:
```
./yii properties/elastic/fill-index
```
This command will create indices for all properties that you allowed to search.

# How to search
*For now only available to perform filtering against properties static values*
## At any place you want
```
<?= \DevGroup\DataStructure\search\elastic\widgets\FilterFormWidget::widget([
'modelClass' => \app\models\Page::class,
'filterRoute' => ['/url/to/filter'],
'options' => [
'storage' => [
EAV::class,
StaticValues::class,
]
]
]) ?>
```
This will render basic filter form with all properties and values contained in the elasticsearch index
- `'modelClass'` - required param, any model class name you have in your app with assigned properties and their static values,
- `'filterRoute'` - required param, `action` attribute for rendered filter form,
- `'options'` - optional, additional array of config. Special key `storage` will be used for definition against what property storage
search will be proceed. If you omit it search will be work only against `StaticValues` storage by default

## In your controller
```
public function actionFilter()
{
/** @var AbstractSearch $component */
$search = \Yii::$app->getModule('properties')->getSearch();
$config = ['storage' => [
EAV::class,
StaticValues::class,
]
];
$modelIds = $search->findInProperties(Page::class, $config);
$dataProvider = new ActiveDataProvider([
//provider config
]);
//other stuff here
}
```
- `Page` - any model class name you have in your app with assigned properties and their static values
- `$modelIds` will contain all found model ids, according to selected property values in filter. Using them you can show anything you want,
- `'$config'` - optional, additional array of config. Special key `storage` will be used for definition against what property storage
search will be proceed. If you omit it search will be work only against `StaticValues` storage by default

## Filtering logic

Filters uses both intersection and union operations while search.

Lets see, for example you have filter request like this:
```
[
1 => [2,3],
13 => [18,9,34]
]
```
First of all this means that we want to find products that has property values assigned with id 2,3 from property with id 1,
and 18, 9, 34 from property with id 13.

*What will filter do?*
- For now it will find all products with assigned values with ids IN(2,3);
- then it will find all products with assigned values with ids IN(12,9,34);
- and finally it will return to you result of intersection from both previous results.

## How to extend and implement
For all `Search` and `Watch` mechanisms you can use your custom implementation.

Actually you can create and use your own database connection, e.g.: `MongoDB`, `ArangoDB`.

Or you can just use your custom `Watch` class for elasticsearch index actualization.

Both `Search` and `Watch` classes are implements according interfaces and extends abstract classes
- `DevGroup\DataStructure\search\interfaces\Search` and `DevGroup\DataStructure\search\base\AbstractSearch` for `Search`
- `DevGroup\DataStructure\search\interfaces\Watch` and `DevGroup\DataStructure\search\base\AbstractWatch` for `Watch`

Just extend your class from needed abstract class and define it in application config, like described upper.

If you are realizing custom index, you probably need to create own controller for first time index initialization, like
`DevGroup\DataStructure\commands\ElasticIndexController`

Define your own `Watch` class in your own `Search` class if necessary.

Clearly created and defined `Watch` class will be automatically subscribed to according system events.




5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@
"php": ">=5.5.0",
"yiisoft/yii2": "^2.0.6",
"devgroup/yii2-tag-dependency-helper": "^1.3.0",
"yiisoft/yii2-elasticsearch": "^2.0@dev",
"unclead/yii2-multiple-input": "~1.2.3",
"devgroup/yii2-multilingual": "*",
"arogachev/yii2-sortable": "^0.1.5",
"kartik-v/yii2-widget-select2": "^2.0.0",
"hector68/yii2-map-input-widget": "^2.0",
"elasticsearch/elasticsearch": "^2.1",
"rmrevin/yii2-fontawesome": "~2.14"
},
"require-dev": {
"yiisoft/yii2-bootstrap": "^2.0@dev",
"devgroup/yii2-admin-utils": "dev-master",
"devgroup/dotplant-dev": "~1.0.0"
"devgroup/dotplant-dev": "~1.0.0",
"elasticsearch/elasticsearch": "^2.1"
},
"autoload": {
"psr-4": {
Expand Down
Loading

0 comments on commit c109a03

Please sign in to comment.