Issue
- Entering keywords in the search bar can only search for the Web Content at the beginning of the keyword. For example, if there’s a web content named “Text_name”, the users can find the web content typing “text” in the search bar, but when the user types “name”, no results are displayed.
- Can a 'contains' clause be emulated?
Environment
- Liferay DXP 7.3
- Liferay DXP 7.4
Resolution
- There is no proper, built-in functionality to accomplish this directly
- A story has been created to overcome this:
- LPD-4367 Allow users to search web content by noninitial partial name
-
However, while not recommended due to performance implications, a workaround does exist.
Workaround
- You can go to System Settings > Search > Index Query Preprocessor, and add a new entry with the pattern for the desired field, for example, title.*. See Search Configuration Reference > Index Query Preprocessor.
- Note: you must restart for the changes to take effect.
- After that, if you perform a search, a 2-star wildcard is appended to your queries for fields matching your pattern. You can use the Search Insights widget to inspect the query sent to Elasticsearch. There you will see that a wildcard query is used for specific clauses.
- For example:
"wildcard": {
"title_en_US": {
"wildcard": "*keyword*"
}
}
- For example:
- However, make sure you understand the risks of doing this:
- Wildcard queries with leading and trailing wildcards (
*keyword*
) have been known to be a bad pattern for years and are discouraged by Lucene and Elasticsearch/Solr as well. - The linked Index Query Preprocessor documentation highlights the fundamental problem with using this type of search:
- This is a resource-intensive operation that degrades search engine performance as indexes grow larger.
- In the product, this option was removed by default due to the severe performance issues it can cause. See LPS-130375.
- In fact, the aforementioned configuration has been deprecated since 7.4.
- In addition to that, Elasticsearch has a setting to disable these expensive queries (among others). See query-dsl-allow-expensive-queries:
- The execution of such queries can be prevented by setting the value of the
search.allow_expensive_queries
setting tofalse
(defaults totrue
).
- The execution of such queries can be prevented by setting the value of the
- Wildcard queries with leading and trailing wildcards (
Additional Information
- Search Configuration Reference > Index Query Preprocessor
- LPD-4367 Allow users to search web content by noninitial partial name
- LPS-130375 Discontinue "two star" wildcard search queries