How to solve the problem of abnormal search caused by Chinese characters when using ews java api 2.0 to access Exchange2019 through fuzzy search of folder emails

海锋 冉 0 Reputation points
2025-11-20T02:15:41.8233333+00:00

When using ews java api 2.0, use the following method 1 for remote mail search on the server (Exchange server 2019)

// 1. Create a fuzzy search filter for the Body field
SearchFilter bodyFilter = new SearchFilter.ContainsSubstring(
        EmailMessageSchema.Body,    
        keyword,               
        ContainmentMode.Substring,  
        ComparisonMode.IgnoreCaseAndNonSpacingCharacters   
);
// Call findItems to perform a fuzzy matching query
FindItemsResults<Item> findResults = service.findItems(new FolderId(folderId), bodyFilter, view);

Method 1 is to specify a fuzzy search for the body of the email

Or use the following method 2 to search

// Directly use the fuzzy search keyword as the second parameter of the api and call findItems for fuzzy matching queries
FindItemsResults<Item> findResults = service.findItems(new FolderId(folderId), keyword, view);

Method 2 involves directly passing parameters using keyword parameters. findItems will automatically deblur and match multiple items, including the body of the email.

Both of the above two methods have a common problem: if the keyword contains Chinese, the result may not be accurate.

For example:

If the body of an email A contains the Chinese character "安排一下", no matter which of the above two methods is used

When the keyword is "安排", email A can always be searched.

  1. When the keyword is "安排一下", email A cannot be searched.
  2. When the keyword is "安", email A can always be searched.
  3. When the keyword is "排", email A cannot be searched.

May I ask how to solve the above problem and ensure the normal retrieval of the main text characters?

I sincerely hope to solve the above-mentioned problems of mine. I would be extremely grateful!

Exchange | Exchange Server | Development
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Q&A Assist
    2025-11-20T02:16:02.11+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    The issue you're experiencing with abnormal search results when using Chinese characters in the EWS Java API could be related to how the search filters handle character encoding and substring matching in different languages. Here are some suggestions to potentially improve the accuracy of your searches:

    1. Use Full Text Search: If possible, consider using full-text indexing features of Exchange. This can enhance the search capabilities for various languages, including Chinese.
    2. Adjust Comparison Mode: You might want to experiment with different ComparisonMode settings. While ComparisonMode.IgnoreCaseAndNonSpacingCharacters is useful, it may not be sufficient for Chinese characters. If available, try using a mode that better accommodates the specific character set.
    3. Search with Variations: When performing searches, consider implementing a mechanism to search for variations of the keyword. For example, if the keyword is "安排一下", you could also search for "安排", "一下", or even parts of the phrase to ensure broader matching.
    4. Check Encoding: Ensure that the encoding used in your application is compatible with the Exchange server. Sometimes, issues arise from mismatched character encodings.
    5. Debugging and Logging: Implement logging to capture the exact queries being sent to the Exchange server. This can help identify if the issue lies in the query formation or the server's handling of the request.
    6. Consult Documentation: Review the EWS Managed API documentation for any specific notes on handling non-Latin characters or any known issues with fuzzy searches involving Chinese characters.
    7. Reach Out to Microsoft Support: If the problem persists, consider reaching out to Microsoft support for assistance, as they may have insights or updates regarding the handling of Chinese characters in EWS queries.

    These steps may help improve the accuracy of your searches when using Chinese characters with the EWS Java API.


    References:

    0 comments No comments

  2. Jade-T 8,530 Reputation points Microsoft External Staff Moderator
    2025-11-20T05:08:40.0633333+00:00

    Hi @海锋 冉

    Thank you for posting your question in the Q&A Forum and providing the detailed breakdown of your scenario.

    The behavior you’re observing with Chinese keywords can vary depending on how the Exchange search index processes multi-byte characters, which may lead to differences between short and long substring queries. 

    You might consider testing with an AQS (Advanced Query Syntax) query, as some users have noted that it can return broader matching results in multilingual cases. AQS queries are supported in EWS through the QueryString parameter. 

    Example: 

    String keyword = "排"; 
    String queryString = "body:" + keyword + "*"; 
      
    FindItemsResults<Item> results = service.findItems( 
        new FolderId(folderId), 
        queryString, 
        view 
    ); 
    

    For longer phrases, you can also try quoted AQS expressions: 

    String queryString = "body:\"安排一下\"";  
    

    These formats may help you evaluate whether AQS provides results closer to what you expect in your environment. 

    It may also be helpful to confirm that the keyword string is consistently handled as UTF-8 on the client side, as character encoding can influence how multi-byte text is interpreted during searches. 

    For longer-term development planning, some developers explore Microsoft Graph, which offers a different search approach and broader support for modern workloads. This would involve a separate API model rather than adjustments to your existing EWS code. 

    You can refer to the following documentation for additional details: 

    We hope these options help you achieve more accurate search results in your environment. 


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".  

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. 


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.