Message Boards

Problem sorting documents by ddm field when searching

Carlos Barreiro, modified 3 Years ago.

Problem sorting documents by ddm field when searching

New Member Posts: 16 Join Date: 11/25/19 Recent Posts
Hello, i'm making a portlet that allows the user to search documents based on certain things, and the documents should appear sorted by a DDM field. I'm having problems implementing the last thing, so let me explain what i'm doing:In first place, I've created a new Metadata Set called "Positioning", this set has only one field "position", with the following attributes:

"dataType": "integer",
"fieldNamespace": "ddm",
"indexType": "keyword",
"localizable": false,
"name": "position",
"readOnly": false,
"repeatable": false,
"required": false,
"showLabel": true,
"type": "ddm-integer"


Then, I have some Document Types that use this Metadata Set, and so I can set the "position" field in these documents successfully.

In the portlet, I'm looking for the structureID of "Positioning", and using it in the SearchContext to specify that I want to sort first by the "position" field, and then for the document's title:

DynamicQuery queryForStructure =DDMStructureLocalServiceUtil.dynamicQuery().
        add(PropertyFactoryUtil.forName("name").like("%Positioning%"));
List<ddmstructure> structures = DDMStructureLocalServiceUtil
&nbsp; &nbsp; &nbsp; &nbsp; .dynamicQuery(queryForStructure, 0, 1);
long structureId = structures.get(0).getStructureId();

SearchContext searchContext = SearchContextFactory.getInstance(request);
searchContext.setCompanyId(themeDisplay.getCompanyId());
searchContext.setGroupIds(new long[] { themeDisplay.getScopeGroupId() });
searchContext.setEntryClassNames(new String [] {DLFileEntry.class.getName()});
searchContext.setAssetCategoryIds(categoryIds);
searchContext.setAssetTagNames(tagNames);
searchContext.setFolderIds(folderIds);
searchContext.setStart(start);
searchContext.setEnd(end);

Sort[] sorts = new Sort[] {SortFactoryUtil.create("ddm__keyword__" + structureId + "__position", Sort.INT_TYPE, true),new Sort(Field.TITLE, false)};
searchContext.setSorts(sorts);

Indexer<dlfileentry> indexer = IndexerRegistryUtil.getIndexer(DLFileEntry.class.getName());
Hits hits = indexer.search(searchContext);
Document[] documents = (Document[]) hits.getDocs();
</dlfileentry></ddmstructure>


The problem is that the first condition of the sorts array is being ignored/skipped, I've checked that the structureId is the id of the metadata set, and i've tried to reverse the second condition to assure that the sorting is being applied, and it works.

I guess it's some problem with the encoding of the ddm field name, but i've tried some variants (removing the keyword part and adding the locale to the end) and none of them worked emoticon If you can see something that i'm not seeing i'd be grateful emoticon

Regards and thanks in advance!
thumbnail
Jorge Díaz, modified 3 Years ago.

RE: Problem sorting documents by ddm field when searching

Liferay Master Posts: 753 Join Date: 1/9/14 Recent Posts
Have a look to this post:
There is explained to use "DDMIndexerUtil.encodeName" method to generate the field name.
Carlos Barreiro, modified 3 Years ago.

RE: Problem sorting documents by ddm field when searching

New Member Posts: 16 Join Date: 11/25/19 Recent Posts
Yes, i saw that post, but apparently these solutions are for 6.2, as I can't find a reference for a DDMIndexerUtil in 7.1 :/

I saw a DDMIndexerImpl, but I can't import its package (com.liferay.dynamic.data.mapping.util.impl), maybe there's another way to get its instance?

(To use the data mapping package, i'm adding this to build.gradle)
compileOnly group: "com.liferay", name:"com.liferay.dynamic.data.mapping.api", version: "4.10.1"Thanks and regards!
Carlos Barreiro, modified 3 Years ago.

RE: Problem sorting documents by ddm field when searching

New Member Posts: 16 Join Date: 11/25/19 Recent Posts
Sorry for the bump! But i'm still stuck with this problem emoticon Any suggestion is appreciated.
thumbnail
Krzysztof Gołębiowski, modified 3 Years ago.

RE: Problem sorting documents by ddm field when searching (Answer)

Liferay Master Posts: 549 Join Date: 6/25/11 Recent Posts
Hmm, as far as I remember Liferay creates more than one field in FT index for each structure field. There are localized fields for each language you use, and also localized + sortable (with "_sortable" prefix). I don't remember how it was exactly done, but I'd look into the index, try to find a group of fields that correspond to your Structure field, choose the one that is "sortable" and use it in your Sort.
Carlos Barreiro, modified 3 Years ago.

RE: Problem sorting documents by ddm field when searching

New Member Posts: 16 Join Date: 11/25/19 Recent Posts
Hello, that was the solution emoticon I searched and there was an index called "ddm__keyword__40937__position_Number_sortable", so my Sort array should be:

Sort[] sorts = new Sort[] {SortFactoryUtil.create("ddm__keyword__" + structureId + "__position_Number_sortable", Sort.INT_TYPE, true),new Sort(Field.TITLE, false)};

Now it is sorting the documents without problems emoticonThanks!
thumbnail
Krzysztof Gołębiowski, modified 3 Years ago.

RE: Problem sorting documents by ddm field when searching

Liferay Master Posts: 549 Join Date: 6/25/11 Recent Posts
Happy to hear that, you're welcome emoticon
Nestor cruz, modified 3 Years ago.

RE: Problem sorting documents by ddm field when searching

Junior Member Posts: 48 Join Date: 2/15/15 Recent Posts
Greetings Carlos

Try entering the names inside this property  "index.sortable.text.fields=firstName,jobTitle,lastName,name,screenName" of the file portal-ext.properties , so create the fields_sort in the search index