Am using ews-java-api to connect exchange mail box and search the text "ABCD" which is the subject of the email
i have used the code similar to the one in https://www.tabnine.com/code/java/methods/microsoft.exchange.webservices.data.search.ItemView/setPropertySet
When it searches first time it is able to retrieve the exact no of results. in my case it is 18. After sometime there was another email triggered in the inbox and rerunning the same code has just brought same count of 18 emails where as the actual count in the inbox is 19
can you please help
connectViaExchange();
ItemView view = new ItemView(100);
view.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Ascending);
view.setPropertySet(new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, ItemSchema.DateTimeReceived));
FindItemsResults<Item> results = service.findItems(WellKnownFolderName.Inbox, new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.ContainsSubstring(ItemSchema.Body, emailSearchString)), view);
int count = results.getTotalCount();
``` System.out.println("Total No of Emails based on the search performed is: "+count);
There are two ways of searching for items in Exchange using EWS a folder restriction which is what your doing above and using the a ContentIndex query which involves using a AQS query string eg https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-perform-an-aqs-search-by-using-ews-in-exchange.
With the restriction method when you use finditems and a searchfilter this creates a temporary restriction https://bill-long.com/2015/08/31/a-history-of-cached-restrictions-in-exchange/ that the Exchange server populates asynchronously in the background like the way searchfolders work. The problem with this type of search is when you have a folder with a large number of items and you making an expensive query like ContainsSubstring on the body is that it can take a long time for the server to update the temporary restriction which is what findItems uses to return the results. The Content index's which is what is searched when you use an AQS query will be updated much quicker and be a lot less of an expensive query so will work much better when you have folders with very large item counts.
If you really want to use a search filter and your doing the same query over and over on a Mailbox you can create a search folder which will generally update much quicker then a temporary restriction and the server doesn't need to rebuild and rescan all the items.