Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update madewithlove/elasticsearcher to work with Elasticsearch 7. #59

Merged
merged 7 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ composer require madewithlove/elasticsearcher

| Elasticsearch | Elasticsearcher |
|---------------|-----------------|
| >= 7.0 | >= 0.7 |
| >= 5.0 | >= 0.5 |
| >= 2.0 | >= 0.4 |
| >= 1.0, < 2.0 | 0.3 |
Expand Down Expand Up @@ -107,23 +108,21 @@ foreach ($result->getResults() as $movie) {

```php
$searcher->indicesManager()->exists('listings');
$searcher->indicesManager()->existsType('suggestions', 'movies');
$searcher->indicesManager()->create('suggestions');
$searcher->indicesManager()->update('suggestions');
$searcher->indicesManager()->delete('suggestions');
$searcher->indicesManager()->deleteType('suggestions', 'movies');
```

### Documents management

```php
$manager->index('suggestions', 'movies', $data);
$manager->bulkIndex('suggestions', 'movies', [$data, $data, $data]);
$manager->update('suggestions', 'movies', 123, ['name' => 'Fight Club 2014']);
$manager->updateOrIndex('suggestions', 'movies', 123, ['name' => 'Fight Club 2014']);
$manager->delete('suggestions', 'movies', 123);
$manager->exists('suggestions', 'movies', 123);
$manager->get('suggestions', 'movies', 123);
$manager->index('suggestions', $data);
$manager->bulkIndex('suggestions', [$data, $data, $data]);
$manager->update('suggestions', 123, ['name' => 'Fight Club 2014']);
$manager->updateOrIndex('suggestions', 123, ['name' => 'Fight Club 2014']);
$manager->delete('suggestions', 123);
$manager->exists('suggestions', 123);
$manager->get('suggestions', 123);
```

### Cluster Health
Expand Down
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ machine:
java:
version: openjdk8
environment:
ES_VERSION: 5.6.16
ES_VERSION: 7.17.1

dependencies:
pre:
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
}
},
"require": {
"elasticsearch/elasticsearch": "^5.1",
"elasticsearch/elasticsearch": "^7.0",
"jdrieghe/array-helpers": "^0.2.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
"phpunit/phpunit": "^9.5"
},
"extra": {
"branch-alias": {
Expand Down
8 changes: 7 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
version: '3.6'
services:
elasticsearch:
image: elasticsearch:5.6-alpine
image: elasticsearch:7.17.1
ports:
- 9200:9200

environment:
- cluster.name=docker-cluster
- bootstrap.memory_lock=true
- discovery.type=single-node
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- HTTP_EXPOSE=9200
ulimits:
memlock:
soft: -1
hard: -1
14 changes: 7 additions & 7 deletions docs/document-management.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ $data = [
'id' => 123
'name' => 'Fight club'
];
$manager->index('suggestions', 'movies', $data);
$manager->bulkIndex('suggestions', 'movies', [$data, $data, $data]);
$manager->update('suggestions', 'movies', 123, ['name' => 'Fight Club 2014']);
$manager->updateOrIndex('suggestions', 'movies', 123, ['name' => 'Fight Club 2014']);
$manager->delete('suggestions', 'movies', 123);
$manager->exists('suggestions', 'movies', 123);
$manager->get('suggestions', 'movies', 123);
$manager->index('suggestions', $data);
$manager->bulkIndex('suggestions', [$data, $data, $data]);
$manager->update('suggestions', 123, ['name' => 'Fight Club 2014']);
$manager->updateOrIndex('suggestions', 123, ['name' => 'Fight Club 2014']);
$manager->delete('suggestions', 123);
$manager->exists('suggestions', 123);
$manager->get('suggestions', 123);
```
20 changes: 5 additions & 15 deletions docs/index-management.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ $manager = $searcher->indicesManager();
## Defining an index

A simple [index](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/_basic_concepts.html#_index) exists
out of a name and one or more types (+mappings). This can be created as:
out of a name and +mappings. This can be created as:

```php
class SuggestionsIndex extends \ElasticSearcher\Abstracts\AbstractIndex
Expand All @@ -22,17 +22,10 @@ class SuggestionsIndex extends \ElasticSearcher\Abstracts\AbstractIndex

public function setup()
{
$this->setTypes([
'books' => [
'properties' => [
'id' => ['type' => 'integer'],
'name' => ['type' => 'text'],
]
],
'movies' => [
'properties' => [
'name' => ['type' => 'text'],
]
$this->setMappings([
'properties' => [
'id' => ['type' => 'integer'],
'name' => ['type' => 'text'],
]
]);
}
Expand Down Expand Up @@ -77,13 +70,10 @@ $searcher->indicesManager()->registeredIndices();
// Indices that exist in the server, not linked to the registered indices.
$searcher->indicesManager()->indices();
$searcher->indicesManager()->get('suggestions');
$searcher->indicesManager()->getType('suggestions', 'books');

// Other
$searcher->indicesManager()->exists('listings');
$searcher->indicesManager()->existsType('suggestions', 'movies');
$searcher->indicesManager()->create('suggestions');
$searcher->indicesManager()->update('suggestions');
$searcher->indicesManager()->delete('suggestions');
$searcher->indicesManager()->deleteType('suggestions', 'movies');
```
5 changes: 2 additions & 3 deletions docs/query-building.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ like the ElasticSearch SDK offers.

## Minimum query

This basic example will return all documents in the `movies` type in the `suggestions` index. The type is optional, which
would result in all documents inside the `suggestions` index would be returned.
This basic example will return all documents in the `suggestions` index.

```php
use ElasticSearcher\Abstracts\AbstractQuery;
Expand All @@ -16,7 +15,7 @@ class MoviesYouMightLikeQuery extends AbstractQuery
{
public function setup()
{
$this->searchIn('suggestions', 'movies');
$this->searchIn('suggestions');
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion docs/re-useable-fragments.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class AuthorsIndex extends AbstractIndex
]
]
],
'mappings' => $this->getTypes()
'mappings' => $this->getMappings()
];
}
}
Expand Down
2 changes: 1 addition & 1 deletion install-elasticsearch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
echo ">>> Installing Elasticsearch"

# Set some variables
ELASTICSEARCH_VERSION=5.6.16 # Check http://www.elasticsearch.org/download/ for latest version
ELASTICSEARCH_VERSION=7.17.1 # Check http://www.elasticsearch.org/download/ for latest version

# Install prerequisite: Java8
echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | sudo /usr/bin/debconf-set-selections
Expand Down
18 changes: 4 additions & 14 deletions src/Abstracts/AbstractIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ public function getBody()
}

/**
* @param array $types
* @param array $mappings
*
* @return array
*/
public function setTypes(array $types)
public function setMappings(array $mappings)
{
return $this->set('mappings', $types);
return $this->set('mappings', $mappings);
}

/**
* @return array
*/
public function getTypes()
public function getMappings()
{
return $this->get('mappings');
}
Expand All @@ -91,14 +91,4 @@ public function getSettings()
{
return $this->get('settings');
}

/**
* @param string $type
*
* @return array
*/
public function getType($type)
{
return $this->get('mappings.'.$type);
}
}
49 changes: 2 additions & 47 deletions src/Abstracts/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ abstract class AbstractQuery
*/
protected $indices = [];

/**
* Types on which the query should be executed.
*
* @var array
*/
protected $types = [];

/**
* Data that can be used when building a query.
*
Expand Down Expand Up @@ -100,22 +93,16 @@ public function getData($key = null)
}

/**
* Define on which indices and types the query should be run.
* Define on which indices the query should be run.
*
* @param string|array $index
* @param null|string|array $type
*/
public function searchIn($index, $type = null)
public function searchIn($index)
{
// Reset the current state, in case the same instance is re-used.
$this->indices = [];
$this->types = [];

$this->searchInIndices((array) $index);

if ($type !== null) {
$this->searchInTypes((array) $type);
}
}

/**
Expand Down Expand Up @@ -172,23 +159,6 @@ protected function getQueryStringParam($name)
return Arr::get($this->queryStringParams, $name);
}

/**
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-search-type.html
* @param string $type
*/
protected function setSearchType($type)
{
$this->setQueryStringParam('search_type', $type);
}

/**
* @return string
*/
protected function getSearchType()
{
return $this->getQueryStringParam('search_type');
}

/**
* Build the query by adding all chunks together.
*
Expand All @@ -203,11 +173,6 @@ protected function buildQuery()
// Index always needs to be provided. _all means a cross index search.
$query['index'] = empty($this->indices) ? '_all' : implode(',', array_values($this->indices));

// Type is not required, will search in the entire index.
if (!empty($this->types)) {
$query['type'] = implode(',', array_values($this->types));
}

// Replace Fragments with their raw body.
$query['body'] = $this->fragmentParser->parse($this->body);

Expand Down Expand Up @@ -237,16 +202,6 @@ public function getIndices()
return $this->indices;
}

/**
* Types we are searching in.
*
* @return array
*/
public function getTypes()
{
return $this->types;
}

/**
* Get the query after being build.
* This is what will be sent to the elasticsearch SDK.
Expand Down
Loading