Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: Add custom field in index of JounalArticle
Hello guys!
I need add a custom field to all JournalArticle, in order to create a counter, said counter must be present and we will have to modify the document in order to make a +1 every time Elastic finds the document (put a counter on how many times that Document is used). how can i update the found document?!
I am currently working with the results of the BooleanQuery (hits -> document) and to the Document I am adding an addNumber ("counter", 1), but I don't know how to save this data in the index?
Can I modify the index or call it in some way so that this document persists?
Regards, and thank you very much
I need add a custom field to all JournalArticle, in order to create a counter, said counter must be present and we will have to modify the document in order to make a +1 every time Elastic finds the document (put a counter on how many times that Document is used). how can i update the found document?!
I am currently working with the results of the BooleanQuery (hits -> document) and to the Document I am adding an addNumber ("counter", 1), but I don't know how to save this data in the index?
Can I modify the index or call it in some way so that this document persists?
Regards, and thank you very much
Hi Adrian,If you have configured the Custom Field on the Document model object and set it to be "indexable" then it will be stored on the index for you. Note though that the Custom Fields, when referenced in the API/Data Model are referred to as "Expando XXX" .. so you have Expando Table, Expando Column, Expando Value, etc. If you make a custom field searcheable, then it will be in the index, but prefixed with expando__ to provide an indicator that it is a field that is the result of a custom field configuration -- the type mapping looks like this:
{
"template_expando": {
"mapping": {
"store": true,
"type": "text"
},
"match": "expando__*",
"match_mapping_type": "string"
}
}
Now, when you get your Hits, you would need to access the "document" object for the items (DocumentLocalService), retrieve the custom fields (getExpandoBridge()) then get the attribute (getAttribute("your-custom-field-name")) .. and then change the value. When you save the Document object again (using the DocumentLocalService) it will cause the indexed record to be updated for you. Note though that the Document objects also have related AssetEntry records and I believe it is possible to have a view counter on them so that Liferay will track the number of times they have been looked at. That count won't be stored in the index though so if you are looking to filter on it, then you would have to use an AssetEntryQuery instead of a Search Query.
So it is more complex than I thought, I currently have a PostProcessor to modify the document and add a field (document.addNumber("counter", 0);
How can i configured the Custom Field on the Journal Article object?
Thank you so much!
How can i configured the Custom Field on the Journal Article object?
Thank you so much!
Hey Adrian, That works -- you can do it that way, but that is actually more difficult
. If you want to use the custom fields then you do this 1. Log in as the admin2. Control Panel > Configuration > Custom Fields3. Choose the "Web Content" option4. Add the custom field (give it the name, etc)5. Edit the custom field to validate the options; searchable, visible, etc6. Save... now if you go to any of the journal articles (web content) items you have created, choose to edit one of them, or add a new one. If you scroll to the bottom below the form, categorization etc, you will see there is a section there where the custom fields are rendered (assuming you made the field visible). You can set the value there manually, but it also validates that the custom field is present. Now set the value, save and then check your index, you will see it in there under the expando__ field, again, assuming you made it searchable.

Thank you! I try it right now!!
Andrew Jardine:
Hey Adrian, That works -- you can do it that way, but that is actually more difficult. If you want to use the custom fields then you do this 1. Log in as the admin2. Control Panel > Configuration > Custom Fields3. Choose the "Web Content" option4. Add the custom field (give it the name, etc)5. Edit the custom field to validate the options; searchable, visible, etc6. Save... now if you go to any of the journal articles (web content) items you have created, choose to edit one of them, or add a new one. If you scroll to the bottom below the form, categorization etc, you will see there is a section there where the custom fields are rendered (assuming you made the field visible). You can set the value there manually, but it also validates that the custom field is present. Now set the value, save and then check your index, you will see it in there under the expando__ field, again, assuming you made it searchable.
Hello Andrew Jardine, I have the field "expando__keyword__custom_fields__counter" and I can see it on the document, it has a default value(0) and set as indexable, the problem is the same, When I edit an article with the counter at 5, Liferay creates a new document and sets the default value (0), Does this have any logic?
Hi Adrian,It's normal to have more than one indexed record for the same Journal Article. What you will find though is that only one of the records will have a value of true for the field "head" in the index. This indicates that it is the latest published version. I'm surprised that it is not working though as I have done similar (not counter, but certainly custom fields that are searchable) solutions before without issue. Is it possible for you to share your project code with me so that I can try it on my side?
I just noticed that you referenced the Journal Article in your response. Just for the record, I'm not entirely sure under what context you need the count, but if you are using the Web Content Display portlet, I believe there is a setting on the portlet that you can use to "increment the view counter" for you.
Curious: Why do you want that? What is the idea behind the requirement?
My customers are usually interested in "how often is a link clicked/a file downloaded" and don't care, how often a file is found.
Have you checked Elasticsearch itself? Maybe it a feature that could help you.
My customers are usually interested in "how often is a link clicked/a file downloaded" and don't care, how often a file is found.
Have you checked Elasticsearch itself? Maybe it a feature that could help you.
The idea of this requirement is to take into account the most searched contents within a custom search portlet (which uses elastic), so we cannot use the functionalized visits or something similar

Hmmm, I mean I get the idea here, but I am wondering if maybe there is a better way to record this information. One thing that comes to mind for me is that you may find that article 1234 is found 50 times, but there is no context to indicate "why" it was found that many times. Even something simple like the search terms that were used, or the search results page the user was on, etc. Admittedly, I don't know all the details of your requirements here, but I think I'd be more inclined to create a service that can be used to record a log entry with the details of the search (the search context), the user information maybe (can possibly be used for geography analytics), and the results -- stored in a format that can easily be loaded into an analytics tool to pull apart and report the findings. IMHO, that makes more sense than storing a counter on the indexed record itself, if for no other reason than, the counter is not actually an attribute of the Journal Article -- it is a by product of an action that surfaces the Journal Article.
If you're willing to share the full scope of what you are trying to achieve, maybe we can provide an alternative approach that best leverages what comes out of the box with Liferay?
If you're willing to share the full scope of what you are trying to achieve, maybe we can provide an alternative approach that best leverages what comes out of the box with Liferay?
<p>I have to agree with Andrew here.</p>
<p>I also don't understand what you update strategy should be.</p>
<p>Let's say, I search for "Copyright" and I get about 10.000 hits, since each document and practically all text contain that word in the footer. Or would you only update the first ten? But they are already the first ten hits, so why update them at all? And why is it relevant that a document was hit by a search at all. If I search for other things, the same document might not be relevant at all.</p>
<p>Maybe it would be interesting to count how often an article/document is clicked from the search. But still, it might be only interesting at the moment. So, somebody searches in the winter for ski and receives a ton of hits would receive ski results in summer too?</p>
<p>There has to be a better strategy for handling this.</p>
<p>I also don't understand what you update strategy should be.</p>
<p>Let's say, I search for "Copyright" and I get about 10.000 hits, since each document and practically all text contain that word in the footer. Or would you only update the first ten? But they are already the first ten hits, so why update them at all? And why is it relevant that a document was hit by a search at all. If I search for other things, the same document might not be relevant at all.</p>
<p>Maybe it would be interesting to count how often an article/document is clicked from the search. But still, it might be only interesting at the moment. So, somebody searches in the winter for ski and receives a ton of hits would receive ski results in summer too?</p>
<p>There has to be a better strategy for handling this.</p>
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™