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

[hibernate-search] Cannot inject CDI bean in @SearchExtension classes #41384

Closed
cthiebault opened this issue Jun 24, 2024 · 7 comments
Closed
Labels
area/arc Issue related to ARC (dependency injection) area/hibernate-search Hibernate Search kind/bug Something isn't working triage/needs-reproducer We are waiting for a reproducer.

Comments

@cthiebault
Copy link

Describe the bug

I would like to prefix the name of my indices with the name of the environment in which they are created.
The environment is defined in the application.properties file.

I tried to implement org.hibernate.search.backend.elasticsearch.index.layout.IndexLayoutStrategy
but I cannot inject the prefix: the injected prefix is always null.

// adapted version of org.hibernate.search.backend.elasticsearch.index.layout.impl.SimpleIndexLayoutStrategy
@SearchExtension
public class PrefixIndexLayoutStrategy implements IndexLayoutStrategy {

    @ConfigProperty(name = "index-prefix")
    String prefix;

    @Override
    public String createInitialElasticsearchIndexName(String hibernateSearchIndexName) {
        return prefix + hibernateSearchIndexName + "-000001";
    }

    @Override
    public String createWriteAlias(String hibernateSearchIndexName) {
        return prefix + hibernateSearchIndexName + "-write";
    }

    @Override
    public String createReadAlias(String hibernateSearchIndexName) {
        return prefix + hibernateSearchIndexName + "-read";
    }

    @Override
    public String extractUniqueKeyFromHibernateSearchIndexName(String hibernateSearchIndexName) {
        return prefix + hibernateSearchIndexName;
    }

    @Override
    public String extractUniqueKeyFromElasticsearchIndexName(String elasticsearchIndexName) {
        Matcher matcher = SimpleIndexLayoutStrategy.UNIQUE_KEY_EXTRACTION_PATTERN.matcher(elasticsearchIndexName);
        if (!matcher.matches()) {
            throw new SearchException(
                    String.format(
                            "Invalid Elasticsearch index layout:"
                                    + " primary (non-alias) name for existing Elasticsearch index '%1$s'"
                                    + " does not match the expected pattern '%2$s'.",
                            elasticsearchIndexName, SimpleIndexLayoutStrategy.UNIQUE_KEY_EXTRACTION_PATTERN));
        }
        return matcher.group(1);
    }

}

Expected behavior

I would expect to be able to inject CDI beans to @SearchExtension beans

Actual behavior

No response

How to Reproduce?

No response

Output of uname -a or ver

No response

Output of java -version

21.0.2

Quarkus version or git rev

3.11.3

Build tool (ie. output of mvnw --version or gradlew --version)

No response

Additional information

No response

@cthiebault cthiebault added the kind/bug Something isn't working label Jun 24, 2024
@quarkus-bot quarkus-bot bot added area/arc Issue related to ARC (dependency injection) area/hibernate-search Hibernate Search labels Jun 24, 2024
Copy link

quarkus-bot bot commented Jun 24, 2024

/cc @Ladicek (arc), @gsmet (hibernate-search), @manovotn (arc), @marko-bekhta (hibernate-search), @mkouba (arc), @yrodiere (hibernate-search)

@marko-bekhta
Copy link
Contributor

hey @cthiebault
Thanks for reporting the issue. I tried to reproduce this, but in my case I got the property injected. Any chance you could create a simple reproducer?

@yrodiere yrodiere added the triage/needs-reproducer We are waiting for a reproducer. label Jun 24, 2024
@cthiebault
Copy link
Author

Thanks @marko-bekhta!
I will try to setup a repo to reproduce this.
FYI I'm using Kotlin... I don't know if it could be the reason...

@cthiebault
Copy link
Author

My bad! It works!

When I wrote a simple app to reproduce this issue, it worked.
Checking my main app, I saw a mismatch config between elasticsearch and opensearch...

Thanks @marko-bekhta for your quick anwers :-)

@marko-bekhta
Copy link
Contributor

No worries 😃, glad you figured it out.

@yrodiere yrodiere closed this as not planned Won't fix, can't repro, duplicate, stale Jun 24, 2024
@cthiebault
Copy link
Author

I don't know if I should open another issue, but when I run my application (prod profile in k8s) with this custom IndexLayoutStrategy and an injected config property (as optional), I get

A runtime config property value differs from the value that was injected during the static intialization phase:
- the runtime value of 'index-prefix' is [dev-] but the value [null] was injected into myapp.PrefixIndexLayoutStrategy()

If that's intentional then annotate the injected field/parameter with @io.quarkus.runtime.annotations.StaticInitSafe to eliminate the false positive.

I read #37444 and tried to inject

@ConfigProperty(name = "index-prefix")
Instance<Optional<String>> prefix;

but it didn't work better :-(

@yrodiere
Copy link
Member

yrodiere commented Jun 25, 2024

I believe the index layout strategy is used by Hibernate Search at static init, so you can't use runtime properties in there. I'd suggest opening a feature request to make the index layout strategy a runtime component (which makes sense)

FWIW it'll probably require changes to Hibernate Search itself, since it currently creates index names very (too?) early, for no good reason that I can see (the names aren't used until runtime): https://github.com/hibernate/hibernate-search/blob/0b9e3f0d87250daa800f10223bcc81cdb0da773a/backend/elasticsearch/src/main/java/org/hibernate/search/backend/elasticsearch/impl/ElasticsearchBackendImpl.java#L206

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/arc Issue related to ARC (dependency injection) area/hibernate-search Hibernate Search kind/bug Something isn't working triage/needs-reproducer We are waiting for a reproducer.
Projects
None yet
Development

No branches or pull requests

3 participants