简体   繁体   中英

@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?

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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM