Xpages: get count of values in multi-value field in view

北慕城南 提交于 2020-01-07 06:18:51

问题


I have 100 documents with a multi-value field. The field holds 5 possible values (Albert,Ben,Chris,Don,Ed) let's say. The field must contain 1 value, but can contain up to 5.

I need to compute the number of docs that contain each value, so

Albert 56
Ben    22
Chris  79 

etc.

This seemed easy. I constructed a view that contains the docs, the first column is the field, and I selected show multiple documents for multiple feeds.

In SSJS loop through my master list of values in the field, and for each one do a getDocumentByKey.

myArray = applicationScope.application;     
var dc:NotesDocumentCollection;

for (index = 0; index < myArray.length; ++index) {
      dc = view1.getAllDocumentsByKey(myArray[index]);  
   Print(dc.getCount())
}

This gets the first value correctly, but none after. If I just hard code a particular value, it works. But when I call the getAllDocumentsByKey a second time, it doesn't return the right value.

I think this would work fine in LS, but in SSJS I must clear or recycle or rest something, but I don't know what.


回答1:


Bryan,

Two things to try in this order:

  1. Your first line of myArray = applicationScope.application; doesn't look right. I suspect that you are not actually getting an array here. I think you are just getting the first value from your applicationScope. Add a print statement on the next line of print(myArray.length); If it is always equal to one, that is your problem. You are not using var and you should set the variable to some java type using a colon.
  2. Before the end of your for loop, try setting your collection to null. dc = null; This way you know for sure that you are getting a new collection in the next iteration.



回答2:


Are any of your multi-value field values ambiguous? getAllDocumentsByKey(Vector) does partial matches. Unless you ever want that, I would recommend always using the second parameter and setting it to true, same always for getAllEntriesByKey().

An alternative, which will definitely perform better, would be to add a total column to the view. There's a performance hit of that on the view indexing, but you can then use a ViewNavigator with getColumnValues() and getNextSibling(). getCount() is extremely poor performing in LS and will almost certainly be as poorly performing in SSJS/Java. See this blog post from a few years ago http://www.intec.co.uk/why-you-shouldnt-count-on-a-notesviewnavigator/




回答3:


If I understand that right: you want to count all values that are possible over an amount of documents to have a 5 value list with corresponsing 5 value counts, right? You could loop through all docs, loop through all values and add entries to a HashMap with the value as key and an int as value (which should be increased everytime). Afterall you have a Map with 5 values holding the sums of each keys.




回答4:


You will never get the right answer with getAllDocumentsByKey(). When document shows in one category, it will be missing from the collection of next category. That's the way it works.

Use ViewNavigator instead. Build it by category and simply count ViewEntries.



来源:https://stackoverflow.com/questions/33701061/xpages-get-count-of-values-in-multi-value-field-in-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!