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

Index is created without a timestamp, when created by scout model observer #204

Open
m-lotze opened this issue May 19, 2022 · 8 comments
Open
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@m-lotze
Copy link

m-lotze commented May 19, 2022

Describe the bug
If you are not using scout:import before you index the first model, the index is created without a timestamp. This leads to an invalid alias name error, if you then want to use scout:import:

{"error":{"root_cause":[{"type":"invalid_alias_name_exception","reason":"Invalid alias name [...]: an index or data stream exists with the same name as the alias"}],"type":"invalid_alias_name_exception","reason":"Invalid alias name [...]: an index or data stream exists with the same name as the alias"},"status":400} {"exception":"[object] (Elasticsearch\\Common\\Exceptions\\BadRequest400Exception(code: 400): {\"error\":{\"root_cause\":[{\"type\":\"invalid_alias_name_exception\",\"reason\":\"Invalid alias name [...]: an index or data stream exists with the same name as the alias\"}],\"type\":\"invalid_alias_name_exception\",\"reason\":\"Invalid alias name [...]: an index or data stream exists with the same name as the alias\"},\"status\":400}

To Reproduce
Steps to reproduce the behavior:

  1. Create a searchable model e.g. with a model factory and save it, so the scout observer gets triggered and the index is created
  2. list elasticsearch indexes with localhost:9200/_cat/indices
  3. there should be an index with the searchableAs name without an appended timestamp
  4. try to scout:import App\Model
  5. the error above should be logged

Expected behavior
The index should always be created with a timestamp appended.

Version
Laravel: 9.13.0
Scout: 9.4.9
Matchish\Elasticsearch: 5.0.2

@m-lotze m-lotze added the bug Something isn't working label May 19, 2022
@matchish matchish added the help wanted Extra attention is needed label May 19, 2022
@hkulekci
Copy link
Contributor

Thinking aloud;

I checked the how that part is working, and I see that import command using the ImportStages class is creating an index name with Index::fromSource method as below :

    public static function fromSource(ImportSource $source): Index
    {
        $name = $source->searchableAs().'_'.time();
        $settingsConfigKey = "elasticsearch.indices.settings.{$source->searchableAs()}";
        $mappingsConfigKey = "elasticsearch.indices.mappings.{$source->searchableAs()}";
        $defaultSettings = [
            'number_of_shards' => 1,
            'number_of_replicas' => 0,

        ];
        $settings = config($settingsConfigKey, config('elasticsearch.indices.settings.default', $defaultSettings));
        $mappings = config($mappingsConfigKey, config('elasticsearch.indices.mappings.default'));

        return new static($name, $settings, $mappings);
    }

So index name getting a suffix in this method is expected.
But the following method is used when I try to index data by updating the object of the model or creating something :

    public function update($models)
    {
        $params = new Bulk();
        $params->index($models);
        $response = $this->elasticsearch->bulk($params->toArray())->asArray();
        if (array_key_exists('errors', $response) && $response['errors']) {
            $error = new ServerResponseException(json_encode($response, JSON_PRETTY_PRINT));
            throw new \Exception('Bulk update error', $error->getCode(), $error);
        }
    }

Inside this method, inside the Bulk class, the index name using as products directly. So, Elasticsaerch creates the index with model name instantly without suffix and alias when we are trying to use model create or update events. I thought that we could check whether or not we have an index or alias related to this model, and we can create an index before doing any update operation with the previous method. This makes every update or creation event a little bit slower. What do you think? Do you have any other suggestions?

@matchish
Copy link
Owner

Hey) Could you create a test that reproduce the issue? I think it can be possible when we do update, and before we didn't do an import by using the command. I would like to not do extra checks before update, but let's think what we can do. It would be great if we can put a request to a bulk that will create an index with alias if it's not exists

@hkulekci
Copy link
Contributor

Sure, I will write a test for the issue.

@hkulekci
Copy link
Contributor

@matchish you can check the problem with the test.

@matchish
Copy link
Owner

https://stackoverflow.com/questions/54690343/elasticsearch-when-inserting-a-record-to-index-i-dont-want-to-create-an-index

What do you think if we add this parameter as default settings to a cluster? It will prevent the bug in default configuration. There is another case when user don't have access to the cluster. But until we don't have a feedback that devs actually have this case, I think we can postpone implementing more sophisticated solution. At least for now I don't see what we can do without adding extra check.

@hkulekci
Copy link
Contributor

hkulekci commented Jun 24, 2022

For now, I could not find any other solution without adding the extra check, too. Maybe, we need to sleep on this a little bit more :D

@Firman95
Copy link

Firman95 commented Apr 4, 2024

Any update about this issue? I still got the error using version 7.6. Is there a workaround that we can do?

@matchish
Copy link
Owner

matchish commented Apr 4, 2024

@Firman95 would it solve the issue for you?
https://stackoverflow.com/q/54690343

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants