forked from liferay/liferay-blade-samples
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
104 additions
and
202 deletions.
There are no files selected for viewing
60 changes: 4 additions & 56 deletions
60
liferay-workspace/extensions/index-settings-contributor/README.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,7 @@ | ||
# Index Settings Contributor | ||
|
||
This sample shows how to add a custom type mapping on Liferay Portal. | ||
This sample shows how to add a custom type mapping to Liferay Portal. | ||
|
||
### What does this sample do when it's deployed? [](id=what-does-this-sample-do-when-its-deployed) | ||
|
||
After you deploy the sample, open your Liferay Portal and go to "Control Panel -> Configuration -> Search -> Reindex all search indexes." click on the "Execute" button. | ||
After that, the properties you defined on you `.json` file will be added to Liferay's search engine. | ||
|
||
### What Api(S) and/or code components does this sample highlight? [](id=what-apis-and-or-code-components-does-this-sample-highlight) | ||
|
||
This sample leverages the IndexSettingsContributor API. | ||
|
||
### How does this sample leverage the api(s) and/or code component? [](id=how-does-this-sample-leverage-the-apis-and-or-code-component) | ||
|
||
Liferay's search engine provides an API to define custom mappings. To use it: | ||
|
||
1. Define the new mapping. | ||
In this sample, the mapping is defined on `META-INF/mappings/resources/index-type-mappings.json` file. | ||
Notice that the default document on Liferay is called `LiferayDocumentType`. | ||
The mapping's features can be found at [elastic search's docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html). | ||
|
||
2. Put the mapping into Elasticsearch. | ||
The `IndexSettingsContributor` components are invoked during the reindexing and receive a `TypeMappingsHelper` as a hook to add new mappings. | ||
|
||
The `ResourceUtil` is a utility class that read's the `.json` file. | ||
|
||
The `IndexSettingsContributor` is a class that allows the addition of type mappings on Liferay's search engine. | ||
|
||
@Override | ||
public void contribute( | ||
String indexName, TypeMappingsHelper typeMappingsHelper) { | ||
try { | ||
String mappings = ResourceUtil.readResouceAsString( | ||
"META-INF/resources/mappings/index-type-mappings.json"); | ||
|
||
typeMappingsHelper.addTypeMappings(indexName, mappings); | ||
} | ||
catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
On the `ResourceUtil.readResouceAsString` parameter you should pass the path for the `.json` that contains the properties to be added. | ||
|
||
Also, it is important to highlight the `@Component` annotation that register a new service to the OSGI: | ||
|
||
@Component( | ||
immediate = true, | ||
service = com.liferay.portal.search.elasticsearch6.settings.IndexSettingsContributor.class | ||
) | ||
|
||
## Where Is This Sample? [](id=where-is-this-sample) | ||
|
||
There are three different versions of this sample, each built with a different build tool: | ||
|
||
- [Gradle](https://github.com/luanmaoski/liferay-blade-samples/tree/blade-sample-index-settings-contributor/gradle/extensions/index-settings-contributor) | ||
- [Liferay Workspace](https://github.com/luanmaoski/liferay-blade-samples/tree/blade-sample-index-settings-contributor/liferay-workspace/extensions/index-settings-contributor) | ||
- [Maven](https://github.com/luanmaoski/liferay-blade-samples/tree/blade-sample-index-settings-contributor/maven/extensions/index-settings-contributor) | ||
For more details on this sample, see the | ||
[Index Settings Contributor](https://dev.liferay.com/en/develop/reference/-/knowledge_base/7-1/index-settings-contributor) | ||
article on Liferay's Developer Network. |
93 changes: 48 additions & 45 deletions
93
liferay-workspace/extensions/search-keyword-query-contributor/README.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,68 @@ | ||
# Search Query Contributor [](id=search-query-contributor) | ||
# Search Keyword Query Contributor | ||
|
||
## What does this sample do when it's deployed? [](id=what-does-this-sample-do-when-its-deployed) | ||
## What does this sample do when it's deployed? | ||
|
||
This sample allows searching for blog entries based on the caption value of the blog image. | ||
This sample adds functionality for searching for blog entries based on the | ||
caption value of its blog image. You can demo this sample by completing the | ||
following steps: | ||
|
||
Steps to test this feature: | ||
- Add the "Search Bar" and "Search Results" portlets to a web page; | ||
- Add a blog entry filling the field caption located below the blog's image and above the blog's title. | ||
- Try to search by the entry on the search bar portlet using caption value. No result must be displayed on search results portlet. | ||
- Deploy the module. | ||
- Try to search by the entry on the search bar portlet using caption value. The matching blog entry must be displayed on search results portlet. | ||
1. Add a blog entry with an image. | ||
|
||
## What API(s) and/or code components does this sample highlight? [](id=what-apis-and-or-code-components-does-this-sample-highlight) | ||
2. After saving the new blog entry, enter a value for the image's caption. | ||
|
||
This sample leverages the [KeywordQueryContributor](https://github.com/liferay/liferay-portal/blob/master/modules/apps/portal-search/portal-search-spi/src/main/java/com/liferay/portal/search/spi/model/query/contributor/KeywordQueryContributor.java) API. | ||
3. Search for the entry in the search bar by using the caption value. The | ||
matching blog entry is displayed in the search results. | ||
|
||
## How does this sample leverage the API(s) and/or code component? [](id=how-does-this-sample-leverage-the-apis-and-or-code-component) | ||
## What API(s) and/or code components does this sample highlight? [](id=what-apis-and-or-code-components-does-this-sample-highlight) | ||
|
||
This sample conveys the recommended approach to adding a field, which may contribute to the relevance of the search, in keyword queries. | ||
This sample leverages the | ||
[KeywordQueryContributor](https://docs.liferay.com/ce/apps/portal-search/latest/javadocs/com/liferay/portal/search/spi/model/query/contributor/KeywordQueryContributor.html) | ||
API. | ||
|
||
To achieve this goal, you need to create a component that implements the `com.liferay.portal.search.spi.model.query.contributor.KeywordQueryContributor`. | ||
## How does this sample leverage the API(s) and/or code component? [](id=how-does-this-sample-leverage-the-apis-and-or-code-component) | ||
|
||
Specifically, you must implement the `contribute` method, which is invoked when a user enters something in the search bars: | ||
This sample conveys the recommended approach to adding a field, which may | ||
contribute to the relevance of the search, in keyword queries. | ||
|
||
```.java | ||
@Override | ||
public void contribute( | ||
String keywords, BooleanQuery booleanQuery, | ||
KeywordQueryContributorHelper keywordQueryContributorHelper) { | ||
To achieve this goal, you must create a component that implements the | ||
`com.liferay.portal.search.spi.model.query.contributor.KeywordQueryContributor`. | ||
|
||
SearchContext searchContext = | ||
keywordQueryContributorHelper.getSearchContext(); | ||
Specifically, you must implement the `contribute` method, which is invoked when | ||
a user enters something in the search bar: | ||
|
||
queryHelper.addSearchLocalizedTerm( | ||
booleanQuery, searchContext, Field.CAPTION, true); | ||
} | ||
``` | ||
@Override | ||
public void contribute( | ||
String keywords, BooleanQuery booleanQuery, | ||
KeywordQueryContributorHelper keywordQueryContributorHelper) { | ||
|
||
This method uses Declarative Services to get a reference for the `QueryHelper` to invoke the `addSearchLocalizedTerm` method. | ||
SearchContext searchContext = | ||
keywordQueryContributorHelper.getSearchContext(); | ||
|
||
Also, it is important to highlight the `@Component` annotation that register a new service to the OSGI: | ||
queryHelper.addSearchLocalizedTerm( | ||
booleanQuery, searchContext, Field.CAPTION, true); | ||
} | ||
|
||
```.java | ||
@Component( | ||
immediate = true, | ||
property = "indexer.class.name=com.liferay.calendar.model.Calendar", | ||
service = KeywordQueryContributor.class | ||
) | ||
``` | ||
This method uses Declarative Services to get a reference for the `QueryHelper` | ||
to invoke the `addSearchLocalizedTerm` method. | ||
|
||
To chose between implementing a `KeywordQueryContributor` or a `ModelPreFilterContributor` | ||
consider these below items: | ||
- Filters are cached and don't influence the score, therefore faster than queries. | ||
- Query is usually something that the users type and pretty much unpredictable, while filters help users narrowing down the search results , for example using facets. | ||
Also, it is important to highlight the `@Component` annotation that registers a | ||
new service to OSGi: | ||
|
||
For more information read [Elasticsearch's documentation](https://www.elastic.co/guide/en/elasticsearch/guide/master/_queries_and_filters.html). | ||
@Component( | ||
immediate = true, | ||
property = "indexer.class.name=com.liferay.calendar.model.Calendar", | ||
service = KeywordQueryContributor.class | ||
) | ||
|
||
## Where Is This Sample? [](id=where-is-this-sample) | ||
You can implement similar functionality by using a `ModelPreFilterContributor`. | ||
To help you make an informed decision between implementing a | ||
`KeywordQueryContributor` or a `ModelPreFilterContributor`, consider these below | ||
items: | ||
|
||
There are three different versions of this sample, each built with a different build tool: | ||
- Filters are cached and don't influence the score; therefore, they're faster | ||
than queries. | ||
- A query is usually something unpredictable that the user types, while filters | ||
help users narrow down search results. For example, using facets. | ||
|
||
- [Gradle](https://github.com/liferay/liferay-blade-samples/tree/7.1/gradle/extensions/search-keyword-query-contributor) | ||
- [Liferay Workspace](https://github.com/liferay/liferay-blade-samples/tree/7.1/liferay-workspace/extensions/search-keyword-query-contributor) | ||
- [Maven](https://github.com/liferay/liferay-blade-samples/tree/7.1/maven/extensions/search-keyword-query-contributor) | ||
For more information, read | ||
[Elasticsearch's documentation](https://www.elastic.co/guide/en/elasticsearch/guide/master/_queries_and_filters.html). |
60 changes: 4 additions & 56 deletions
60
maven/extensions/index-settings-contributor/README.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,7 @@ | ||
# Index Settings Contributor | ||
|
||
This sample shows how to add a custom type mapping on Liferay Portal. | ||
This sample shows how to add a custom type mapping to Liferay Portal. | ||
|
||
### What does this sample do when it's deployed? [](id=what-does-this-sample-do-when-its-deployed) | ||
|
||
After you deploy the sample, open your Liferay Portal and go to "Control Panel -> Configuration -> Search -> Reindex all search indexes." click on the "Execute" button. | ||
After that, the properties you defined on you `.json` file will be added to Liferay's search engine. | ||
|
||
### What Api(S) and/or code components does this sample highlight? [](id=what-apis-and-or-code-components-does-this-sample-highlight) | ||
|
||
This sample leverages the IndexSettingsContributor API. | ||
|
||
### How does this sample leverage the api(s) and/or code component? [](id=how-does-this-sample-leverage-the-apis-and-or-code-component) | ||
|
||
Liferay's search engine provides an API to define custom mappings. To use it: | ||
|
||
1. Define the new mapping. | ||
In this sample, the mapping is defined on `META-INF/mappings/resources/index-type-mappings.json` file. | ||
Notice that the default document on Liferay is called `LiferayDocumentType`. | ||
The mapping's features can be found at [elastic search's docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html). | ||
|
||
2. Put the mapping into Elasticsearch. | ||
The `IndexSettingsContributor` components are invoked during the reindexing and receive a `TypeMappingsHelper` as a hook to add new mappings. | ||
|
||
The `ResourceUtil` is a utility class that read's the `.json` file. | ||
|
||
The `IndexSettingsContributor` is a class that allows the addition of type mappings on Liferay's search engine. | ||
|
||
@Override | ||
public void contribute( | ||
String indexName, TypeMappingsHelper typeMappingsHelper) { | ||
try { | ||
String mappings = ResourceUtil.readResouceAsString( | ||
"META-INF/resources/mappings/index-type-mappings.json"); | ||
|
||
typeMappingsHelper.addTypeMappings(indexName, mappings); | ||
} | ||
catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
On the `ResourceUtil.readResouceAsString` parameter you should pass the path for the `.json` that contains the properties to be added. | ||
|
||
Also, it is important to highlight the `@Component` annotation that register a new service to the OSGI: | ||
|
||
@Component( | ||
immediate = true, | ||
service = com.liferay.portal.search.elasticsearch6.settings.IndexSettingsContributor.class | ||
) | ||
|
||
## Where Is This Sample? [](id=where-is-this-sample) | ||
|
||
There are three different versions of this sample, each built with a different build tool: | ||
|
||
- [Gradle](https://github.com/luanmaoski/liferay-blade-samples/tree/blade-sample-index-settings-contributor/gradle/extensions/index-settings-contributor) | ||
- [Liferay Workspace](https://github.com/luanmaoski/liferay-blade-samples/tree/blade-sample-index-settings-contributor/liferay-workspace/extensions/index-settings-contributor) | ||
- [Maven](https://github.com/luanmaoski/liferay-blade-samples/tree/blade-sample-index-settings-contributor/maven/extensions/index-settings-contributor) | ||
For more details on this sample, see the | ||
[Index Settings Contributor](https://dev.liferay.com/en/develop/reference/-/knowledge_base/7-1/index-settings-contributor) | ||
article on Liferay's Developer Network. |
93 changes: 48 additions & 45 deletions
93
maven/extensions/search-keyword-query-contributor/README.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,68 @@ | ||
# Search Query Contributor [](id=search-query-contributor) | ||
# Search Keyword Query Contributor | ||
|
||
## What does this sample do when it's deployed? [](id=what-does-this-sample-do-when-its-deployed) | ||
## What does this sample do when it's deployed? | ||
|
||
This sample allows searching for blog entries based on the caption value of the blog image. | ||
This sample adds functionality for searching for blog entries based on the | ||
caption value of its blog image. You can demo this sample by completing the | ||
following steps: | ||
|
||
Steps to test this feature: | ||
- Add the "Search Bar" and "Search Results" portlets to a web page; | ||
- Add a blog entry filling the field caption located below the blog's image and above the blog's title. | ||
- Try to search by the entry on the search bar portlet using caption value. No result must be displayed on search results portlet. | ||
- Deploy the module. | ||
- Try to search by the entry on the search bar portlet using caption value. The matching blog entry must be displayed on search results portlet. | ||
1. Add a blog entry with an image. | ||
|
||
## What API(s) and/or code components does this sample highlight? [](id=what-apis-and-or-code-components-does-this-sample-highlight) | ||
2. After saving the new blog entry, enter a value for the image's caption. | ||
|
||
This sample leverages the [KeywordQueryContributor](https://github.com/liferay/liferay-portal/blob/master/modules/apps/portal-search/portal-search-spi/src/main/java/com/liferay/portal/search/spi/model/query/contributor/KeywordQueryContributor.java) API. | ||
3. Search for the entry in the search bar by using the caption value. The | ||
matching blog entry is displayed in the search results. | ||
|
||
## How does this sample leverage the API(s) and/or code component? [](id=how-does-this-sample-leverage-the-apis-and-or-code-component) | ||
## What API(s) and/or code components does this sample highlight? [](id=what-apis-and-or-code-components-does-this-sample-highlight) | ||
|
||
This sample conveys the recommended approach to adding a field, which may contribute to the relevance of the search, in keyword queries. | ||
This sample leverages the | ||
[KeywordQueryContributor](https://docs.liferay.com/ce/apps/portal-search/latest/javadocs/com/liferay/portal/search/spi/model/query/contributor/KeywordQueryContributor.html) | ||
API. | ||
|
||
To achieve this goal, you need to create a component that implements the `com.liferay.portal.search.spi.model.query.contributor.KeywordQueryContributor`. | ||
## How does this sample leverage the API(s) and/or code component? [](id=how-does-this-sample-leverage-the-apis-and-or-code-component) | ||
|
||
Specifically, you must implement the `contribute` method, which is invoked when a user enters something in the search bars: | ||
This sample conveys the recommended approach to adding a field, which may | ||
contribute to the relevance of the search, in keyword queries. | ||
|
||
```.java | ||
@Override | ||
public void contribute( | ||
String keywords, BooleanQuery booleanQuery, | ||
KeywordQueryContributorHelper keywordQueryContributorHelper) { | ||
To achieve this goal, you must create a component that implements the | ||
`com.liferay.portal.search.spi.model.query.contributor.KeywordQueryContributor`. | ||
|
||
SearchContext searchContext = | ||
keywordQueryContributorHelper.getSearchContext(); | ||
Specifically, you must implement the `contribute` method, which is invoked when | ||
a user enters something in the search bar: | ||
|
||
queryHelper.addSearchLocalizedTerm( | ||
booleanQuery, searchContext, Field.CAPTION, true); | ||
} | ||
``` | ||
@Override | ||
public void contribute( | ||
String keywords, BooleanQuery booleanQuery, | ||
KeywordQueryContributorHelper keywordQueryContributorHelper) { | ||
|
||
This method uses Declarative Services to get a reference for the `QueryHelper` to invoke the `addSearchLocalizedTerm` method. | ||
SearchContext searchContext = | ||
keywordQueryContributorHelper.getSearchContext(); | ||
|
||
Also, it is important to highlight the `@Component` annotation that register a new service to the OSGI: | ||
queryHelper.addSearchLocalizedTerm( | ||
booleanQuery, searchContext, Field.CAPTION, true); | ||
} | ||
|
||
```.java | ||
@Component( | ||
immediate = true, | ||
property = "indexer.class.name=com.liferay.calendar.model.Calendar", | ||
service = KeywordQueryContributor.class | ||
) | ||
``` | ||
This method uses Declarative Services to get a reference for the `QueryHelper` | ||
to invoke the `addSearchLocalizedTerm` method. | ||
|
||
To chose between implementing a `KeywordQueryContributor` or a `ModelPreFilterContributor` | ||
consider these below items: | ||
- Filters are cached and don't influence the score, therefore faster than queries. | ||
- Query is usually something that the users type and pretty much unpredictable, while filters help users narrowing down the search results , for example using facets. | ||
Also, it is important to highlight the `@Component` annotation that registers a | ||
new service to OSGi: | ||
|
||
For more information read [Elasticsearch's documentation](https://www.elastic.co/guide/en/elasticsearch/guide/master/_queries_and_filters.html). | ||
@Component( | ||
immediate = true, | ||
property = "indexer.class.name=com.liferay.calendar.model.Calendar", | ||
service = KeywordQueryContributor.class | ||
) | ||
|
||
## Where Is This Sample? [](id=where-is-this-sample) | ||
You can implement similar functionality by using a `ModelPreFilterContributor`. | ||
To help you make an informed decision between implementing a | ||
`KeywordQueryContributor` or a `ModelPreFilterContributor`, consider these below | ||
items: | ||
|
||
There are three different versions of this sample, each built with a different build tool: | ||
- Filters are cached and don't influence the score; therefore, they're faster | ||
than queries. | ||
- A query is usually something unpredictable that the user types, while filters | ||
help users narrow down search results. For example, using facets. | ||
|
||
- [Gradle](https://github.com/liferay/liferay-blade-samples/tree/7.1/gradle/extensions/search-keyword-query-contributor) | ||
- [Liferay Workspace](https://github.com/liferay/liferay-blade-samples/tree/7.1/liferay-workspace/extensions/search-keyword-query-contributor) | ||
- [Maven](https://github.com/liferay/liferay-blade-samples/tree/7.1/maven/extensions/search-keyword-query-contributor) | ||
For more information, read | ||
[Elasticsearch's documentation](https://www.elastic.co/guide/en/elasticsearch/guide/master/_queries_and_filters.html). |