Enable Personal Content Indexing

With personal content indexing, your app provides search results in the Google app for content tied to a user's account. Users only see their own personal content on their devices.

For example, if a user searches for "favorite chicken recipe," the In Apps results tab includes the note they added to a chicken recipe in the recipe app.

If your app doesn't include any personal content, you can skip this step and go straight to Log User Actions.

Before you start, make sure you support links to your app content and have added the App Indexing library to your app.

Establish the index and add objects

Create a class that extends the IntentService.

Then, to include items in the app's personal content index, create Indexable objects in the same class:

  • Set the unique URL and title for each indexable object through the setUrl() and setName() setters, respectively. These properties are required.
  • To create your indexable objects more conveniently, use existing builders whenever possible (e.g., for messages, documents, and spreadsheets). See a list of common builders.
  • For content types that don't have custom builders, use the generic Indexable.Builder().
  • Add more information through predefined data types and properties from Schema.org.
public class AppIndexingService extends IntentService {

    public AppIndexingService() {
        super("AppIndexingService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        ArrayList<Indexable> indexableNotes = new ArrayList<>();

        for (Recipe recipe : getAllRecipes()) {
            Note note = recipe.getNote();
            if (note != null) {
                Indexable noteToIndex = Indexables.noteDigitalDocumentBuilder()
                        .setName(recipe.getTitle() + " Note")
                        .setText(note.getText())
                        .setUrl(recipe.getNoteUrl())
                        .build();

                indexableNotes.add(noteToIndex);
            }
        }

        if (indexableNotes.size() > 0) {
            Indexable[] notesArr = new Indexable[indexableNotes.size()];
            notesArr = indexableNotes.toArray(notesArr);

            // batch insert indexable notes into index
            FirebaseAppIndex.getInstance().update(notesArr);
        }
    }

    // ...
}

What should be in the personal content index?

Define Indexable objects for the following types of content:

  • User-specific content, like messages, photos, or documents.
  • Content that's important to users, such as favorites or content that they may want to access more frequently. For example, documents or songs that they've bookmarked or marked for offline use.
  • Content generated in your app, not just accessed by it. For example, contacts that are created by users directly in your app, and stored by your app, not contacts from the phone's directory.

Generate and refresh the index

Once you've extended the IntentService and added indexable objects, allow Google Play services to call AppIndexingService to add the user's existing personal content to and update the on-device index in the following three situations:

  • When the app is installed on a device.
  • If an existing version of the app is updated to a version that supports personal content indexing.
  • Periodic calls over time to accurately reflect any changes that happen to the indexed content, ensuring a seamless user experience.

Additionally, if the on-device index is lost for any reason (e.g., if the index is corrupted), this call to update the index will repopulate it.

In AndroidManifest.xml, add the AppIndexingService and an <intent-filter> tag.

        <service android:name=".client.AppIndexingService"
            android:exported="true"
            android:permission="com.google.android.gms.permission.APPINDEXING">
            <intent-filter>
                <action android:name="com.google.firebase.appindexing.UPDATE_INDEX" />
            </intent-filter>
        </service>
  

Update the index

When users add, update, or remove their personal content, the on-device index should reflect those changes. Add the following code to update content in the index:

Add & update
private void indexNote() {
        Note note = mRecipe.getNote();
        Indexable noteToIndex = Indexables.noteDigitalDocumentBuilder()
                .setName(mRecipe.getTitle())
                .setText(note.getText())
                .setUrl(mRecipe.getNoteUrl())
                .build();

        Task<Void> task = FirebaseAppIndex.getInstance().update(noteToIndex);
         ...
    }
    
Remove

To identify items that should be removed, use the URL of the item. Add the following line to your app’s delete or remove function for that item.

                    // Deletes or removes the corresponding notes from index.
                    String noteUrl = mRecipe.getNoteUrl();
                    FirebaseAppIndex.getInstance().remove(noteUrl);
         

Delete the index when users log out

Delete the on-device index when users log out of your app, on the logout event. Include the call to generate and refresh the index using AppIndexingService to repopulate the on-device index when users log in again.

To delete the on-device index when a user logs out of your app, add the following code to your app:

...
import com.google.firebase.appindexing.FirebaseAppIndex;

public class LogOutActivity extends Activity {
    ...
    private void onLogOutButtonClicked() {
        ...
        FirebaseAppIndex.getInstance().removeAll();
    }
}

Next: Log User Actions

Send feedback about...

Need help? Visit our support page.