LuckyLuke
LuckyLuke

Reputation: 49097

@SearchableComponent and prefix

I am reading the documentation about Compass (legacy code) but I don't understand @SearchableComponent and the prefix attribute. Could someone try to explain in own words without referring to the documentation what they mean, and how you should use them?

Upvotes: 1

Views: 87

Answers (1)

acanby
acanby

Reputation: 2993

3 year old question, but hopefully someone will stumble on this and get some value.

The prefix that you can optionally specify will form part of the property name that fields are stored under. Compass will essentially traverse the object graph and construct names for each of the indexable properties.

Without specifying a prefix for a @SearchableComponent, you could have something like this:

@Searchable(root=true)
class Customer {
    @SearchableProperty(name="name")
    String name;

    @SearchableComponent
    Address billingAddress;

    @SearchableComponent
    Address mailingAddress;
}

@Searchable(root=false)
class Address {
    @SearchableProperty(name="street")
    String street;

    @SearchableProperty(name="suburb")
    String suburb;
}

This will create the following 3 index fields:

  • name
  • street
  • suburb

As you can see, you end up with both the Mailing and Billing address fields stored in the same index paths - street & suburb.

Now, this may or may not be what you want. If you need to distinguish Mailing address and Billing address (e.g. find people who have different Mailing and Billing addresses), you can use a prefix. Take a look at the following amended code, which adds the prefix:

@Searchable(root=true)
class Customer {
    @SearchableProperty(name="name")
    String name;

    @SearchableComponent(prefix="billing_")
    Address billingAddress;

    @SearchableComponent(prefix="mailing_")
    Address mailingAddress;
}

@Searchable(root=false)
class Address {
    @SearchableProperty(name="street")
    String street;

    @SearchableProperty(name="suburb")
    String suburb;
}

This will give you search keys as follows:

  • name
  • billing_street
  • billing_suburb
  • mailing_street
  • mailing_suburb

In this way, you can search for Mailing and Billing address fields as distinct entries.

All in all it depends on your use case as to whether adding a prefix will provide value.

Upvotes: 0

Related Questions