legacy-knowledge-base
公開されました Jun. 30, 2025

How to Exclude User Names (userName field) from Search Queries in Liferay

投稿者

Jorge Diaz

knowledge-article-header-disclaimer-how-to

knowledge-article-header-disclaimer

legacy-article

learn-legacy-article-disclaimer-text

Issue

We want to exclude the userName field from search queries. This is to prevent irrelevant search results based on user names.

For instance, if a user's name contains a common search term, Liferay will return content and documents created by that user, even if the content itself does not contain the search term. We want to avoid this. 

Environment

  • Liferay 2024.Q1

Resolution

If you need to prevent the userName field from being searched, there are two alternative solutions:

Option 1: Customize Elasticsearch mappings

You can create a custom analyzer that returns an empty string, effectively indexing the userName as empty.

Step 1: create a custom empty string analyzer that always returns the empty string

  • This can be done using the pattern_replace filter in Elasticsearch, replacing every text with the empty string.
  • Add the following configuration to the Additional Index Configurations section of the Elasticsearch connector:
"analysis": {
"filter": {
"remove_text": {
"type": "pattern_replace",
"pattern": ".*",
"replacement": ""
}
},
"analyzer": {
"empty_string_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": ["remove_text"]
}
}
}

Step 2: apply the created custom analyzer to the userName field.

  • It is important to apply this analyzer for indexing only, not searching:
    1. Set "analyzer": "empty_string_analyzer" so that at index time the new empty string analyzer is used.
    2. Set "search_analyzer": "standard" so that at search time the default standard analyzer is used.
  • Add the following configuration to the Override Type Mappings section of the Elasticsearch connector:
...
"userName": {
"analyzer": "empty_string_analyzer",
"search_analyzer": "standard",
"fields": {
"text": {
"analyzer": "empty_string_analyzer",
"search_analyzer": "standard",
"type": "text"
}
},
"type": "text"
},
...

Step 3: Reindex after making these changes to apply the new mapping (see Search Administration section)

Option 2: Disable the UserNameFieldQueryBuilderFactory component

The UserNameFieldQueryBuilderFactory component is responsible for including the userName field in search queries.

Disabling it prevents searches on this field. You can disable it temporarily using the gogo shell command:

scr:disable com.liferay.users.admin.internal.search.analysis.UserNameFieldQueryBuilderFactory

For a permanent solution, blacklist the component by following the instructions on this page: Blacklisting OSGi Components

Choosing the Best Option to Prevent userName Search

When choosing between the two options, it's essential to consider the trade-offs carefully:

  1. The first option guarantees that the userName field cannot be searched by having the analyzer store an empty value in the index during indexing. However, this approach is more complex to implement and requires reindexing.
  2. The second option simplifies implementation by disabling the component that generates queries against the userName field. However, this approach is less robust, as future code modifications or new components could bypass this restriction, making the solution ineffective.

 

 

did-this-article-resolve-your-issue

legacy-knowledge-base