Configuring Search Fields and Weighting for ElasticPress

One of the advantages that ElasticPress has over the built-in WordPress search is the ability to fine-tune which fields or attributes are searchable and configure how important each field is to the relevance score of the document.

Why Relevance Score Matters

Every time someone searches your site, Elasticsearch assigns a relevance “score” to each of the results it returns. The score represents how well each returned result matches what the user is most likely looking for based on the search query the user entered—a higher score indicates a more-relevant result.

This score is important because (unless you have set up custom search results for the specific search search terms entered) the returned search results are ordered by the score, with the highest-scored result on top. By fine-tuning how the score is calculated, you can help ensure the most-relevant content is presented to the user.

Field Weighting

ElasticPress has the ability to search based on many different fields in all the posts, pages, documents, and products on your site. But you may not wish to treat all of these fields equally. That’s where weighting comes in! 

To access the Search Fields & Weighting settings, click on the ElasticPress link on the sidebar of your WordPress Admin menu, then click Search Fields & Weighting.

In the Search Fields & Weighting menu, you’ll find a list of all the different content types ElasticPress is indexing and a list of the different fields available for each. 

(NOTE: In addition to the fields listed on this settings page, you can choose whether results should be weighted based on their published date. For information on how to enable this feature, see Configuring ElasticPress via the Plugin Dashboard.)

To exclude any field from search entirely, uncheck the Searchable box for that field and click the Save Changes button on the bottom of the page. This will prevent search terms in users queries from being matched against that field.

You can also determine how much weight or importance a particular field is given in search results. For example, if you run a recipes website and a user searches for tomato, you may want to give preference to the recipe for Tomato and Bacon Pie (which has the search term right in the title) over, say, the recipe for Grilled Cheese, which just has tomatoes as an optional add-on.

By increasing the value of the Weight slider for the Title field and then clicking the Save Changes button, you can tell ElasticPress that you want searches where the user’s entry matches the title of a post to have a greater effect on the relevance score than one where the search matches on just the content or tags.

A weight of 2 indicates that the field is twice as important as a field with weight of 1. This won’t double the relevance score of a document, as the weight is just one factor in determining relevance, and the scores are normalized before they are returned, so think of the weight as defining the “relative importance” of a field as it related to the other fields.

So looking at our recipe site example, when fields are weighted equally, the Grilled Cheese recipe has a relevance score of 37.1279:

JSON
"_score": 37.1279,
"_source": {
    "comment_count": 0,
    "post_title": "CLASSIC GRILLED CHEESE SANDWICH",
    "post_author": {
        "raw": "andrew.small",
        "id": 3,
        "login": "andrew.small",
        "display_name": "Andrew Small"
    }

and the Tomato Pie has a score of 35.1757:

JSON
"_score": 35.175743,
"_source": {
    "comment_count": 0,
    "post_title": "COUNTRY TOMATO AND BACON PIE",
    "post_author": {
        "raw": "andrew.small",
        "id": 3,
        "login": "andrew.small",
        "display_name": "Andrew Small"
    },

(NOTE: The actual raw numerical scores themselves are irrelevant—the score is only used to sort search results by relevance, and therefore only has meaning in relation to other scores.)

This would result in the Grilled Cheese showing up first, which is not what we want. To fix this, let’s give the Title field a weight of 2 instead.

Now if we search for tomato, the score of the Grilled Cheese recipe doesn’t change, because we only increased the weight of a field that doesn’t, for this post, contain the search term. But the relevance score for the Tomato and Bacon Pie jumps to 50.1464, pushing it ahead of the Grilled Cheese and making it the top result:

JSON
"_score": 50.146423,
"_source": {
    "comment_count": 0,
    "post_title": "COUNTRY TOMATO AND BACON PIE",
    "post_author": {
        "raw": "andrew.small",
        "id": 3,
        "login": "andrew.small",
        "display_name": "Andrew Small"
    },

To fine-tune your search results, continue to fiddle with your field weights until you are seeing the results you want. Remember to scroll to the bottom of the page and hit the Save Changes button for your changes to take effect. You can view the raw search results and relevance scores by using the ElasticPress Debugging Add-On plugin, as discussed in this guide article.