How to search for pages by title using the Search v2 API?

Hi,
I’m trying to build a REST resource which returns pages with the title that the user searches. I suppose the best way is to use the Search V2 API (because I’m not aware of any other API to search for pages by title), but it doesn’t return pages, event if my query is the exact title of the page. It does return pages if I use AllQuery.getInstance() instead.


    @AnonymousAllowed
    @GET
    @Path("/someapipath")
    @Produces(MediaType.APPLICATION_JSON)
    public Response searchForPage(@QueryParam("spaceKey") String spaceKey, @QueryParam("q") String query) {
        try {
            SearchQuery searchV2Query = new TermQuery(SearchFieldNames.TITLE, query);
            SearchSort sort = new ModifiedSort(SearchSort.Order.DESCENDING); // latest modified content first
            SearchFilter securityFilter = SiteSearchPermissionsSearchFilter.getInstance(); // Only pages the current user can see
            ContentSearch search = new ContentSearch(searchV2Query, sort, securityFilter, 0, 20);
    
            SearchResults results = searchManager.search(search);
            List<SearchResult> allItems = results.getAll();
            // HERE: allItems is always empty !

Is “new TermQuery("title", "My Page Title")” the right query to use?

My guess is that you miss the type of results you expect, e.g page, attachment etc. You should add it and then try the below (my custom search code)

Set<ContentTypeEnum> contentTypes = new HashSet<ContentTypeEnum>();
contentTypes.add(ContentTypeEnum.PAGE);
ContentTypeQuery contentTypesQuery = new ContentTypeQuery(contentTypes);
TextFieldQuery titleQuery = new TextFieldQuery("title", queryString,BooleanOperator.AND);
TextFieldQuery bodyContentQuery = new TextFieldQuery("contentBody", queryString, BooleanOperator.AND); //this is just an example for your future reference
BooleanQueryFactory titleFactory = new BooleanQueryFactory();
titleFactory.addMust(titleQuery);
titleFactory.addMust(contentTypesQuery);
//use titleFactory.toBooleanQuery() as SearchQuery

Correct answer! Not for the type-of-result criteria.
I was wondering why adding a criteria would generate more results. Of course it isn’t the case. In your example, you use the right query:

  • :negative_squared_cross_mark: My mistake: new TermQuery("title", "My Page Title")
  • :white_check_mark: Correct: new TextFieldQuery("title", queryString, AND).

I see your point with adding :smiley: Makes more sense now that i think again :slight_smile:

Actually, it answers the question, but doesn’t answer my need. TextFieldQuery requires exact matches. I’ve tried TextFieldPrefixQuery but it never returns any match. Would you be able to help too? How to search for *partial* titles using the Confluence Search v2 API?

Why dont you just use search operators such as wildcard, fuzzy etc?

I’ve tried with the wildcard, it works at the end of the word, but not at the beginning of the word. It does “startsWith” but not “contains”. Anyway, maybe I’ll just settle for “startsWith”, it’s almost good enough.