First written in October 2016 while I was at SEEK, and refreshed in 2020 while I was Head of Data at Go1 to reflect changes in the tools. First published in the Practical AI Coalition publication on Medium. Lightly edited in 2026. The source code and data used in this article are on GitHub.
Good search is about making sure users find what they need. That can be something as simple as a restaurant’s contact details to make a booking, or something more life-defining, such as exploring new jobs. Search engineering and search quality are as much an intellectual pursuit as a technological one.
People often think about building a search system purely from a technical perspective. However, that only accounts for half of what it takes to design, build and maintain a user-centred search system. In this article, we look at search quality in practice. We go through the steps of setting up a search system, not ready for production, to demonstrate the iterative, empirical nature of search quality.
Search is an empirical field. Different users go after different things in a search system or product. Tracking, measurement, evaluation and monitoring are a big part of setting up and maintaining a search system that understands the things that matter to its users, and uses that understanding to serve them better. Not tracking, and not using data to inform search improvements, is one of the most common pitfalls in building and maintaining search engines.
These search quality matters are covered in steps 7 to 10, in the second half of this article. Before that, steps 1 to 6 cover the basic engineering needed to get a search system up and running. For this exercise, we rely on Elasticsearch, MySQL and the Apache web server, with PHP, Perl and Python as the glue, to build a basic search system for job ads crawled from a free-to-post website.

Step 1: Prepare the content
The first step is to get the content ready for Elasticsearch to index. I assembled a web crawler quickly in Perl using the LWP and JSON modules. It works in two stages: scraping and downloading the HTML content, then extracting the pieces of information of interest and storing them in a database. For this exercise, I used the free job posting site postjobfree.com, after reading its terms of use to make sure it did not prohibit crawlers from downloading job ads, and that they could be used for a non-commercial demonstration. The crawler downloaded 7,005 job postings between 4 and 5 October 2016. Using the extractor, we parse the HTML and pick up five main attributes: doctitle, doctext, doccompany, doclocation and docdate. These values are stored in a MySQL table, with a unique identifier generated by hashing the full HTML file. Crawling and extracting text from HTML pages can just as easily be done in Python. At this stage, we have content for the next step.
Step 2: Set up an Elasticsearch instance
Next, we download Elasticsearch and unpack the files into a folder. The version used in this exercise is 7.8.0, released in June 2020. As I was on a Windows machine, I used elasticsearch.bat in the bin folder to get Elasticsearch running. If you need an interface to query and interact with Elasticsearch, consider API development and testing tools such as Postman or Insomnia.
There are two main parts to the JSON object for creating an index: the settings, and the mappings of properties. There used to be a top-level key called type that wrapped the properties, but it was removed in Elasticsearch 7.0. Since Elasticsearch runs locally for this exercise, it sits at http://127.0.0.1, and the default port for the endpoints is 9200. We create an index called jobs using the PUT method, as shown below, and define five properties covering the title, text, date, company and location of the job ads. The settings section is where we outline the specifics of the index, such as the number of shards and replicas, which are not discussed here.
PUT /jobs
{
"settings": {
...
},
"mappings": {
"properties": {
"docdate": { "type": "date", "format": "yyyy-MM-dd" },
"doctitle": { "type": "text" },
"doctext": { "type": "text" },
"doccompany": { "type": "keyword" },
"doclocation": { "type": "keyword" }
}
}
}
Previously, there was only a string type, and if you did not want a string broken down and analysed for full-text search, you set index to not_analyzed. Now that distinction is made at the type level, between text and keyword. For fields of type text, we can apply a set of rules called an analyser to process the text. An analyser performs basic morphological and lexical transformations, such as tokenising and stemming, to improve searchability, and a wide range of languages are supported. For properties of type date, you can define the format that suits your data. So we set doctitle and doctext to type text, and for the fields that need to be matched in their entirety, doccompany and doclocation, we set the type to keyword.
Step 3: Write a feeder to index documents
At this stage, we have an Elasticsearch instance with an empty index, and a database of content. Next, I built a feeder in PHP to send the content to the index. Documents are indexed with the POST method to the endpoint http://127.0.0.1:9200/jobs/_doc. Each document needs a unique identifier, which can be provided explicitly or generated if not. Elasticsearch creates some meta-fields by default, and one of them is _id, which is populated with the identifier if one is provided. So there is no need for a separate field just for identifiers. Below is an example call to the endpoint for the index jobs, with the explicit identifier fb4b50b8fdf075db3ce859038d88fe7d:
POST /jobs/_doc/fb4b50b8fdf075db3ce859038d88fe7d
{
"docdate": "2016-10-04",
"doctext": "Summit Management is currently seeking a highly...",
"doclocation": "New Britain, Connecticut, United States",
"doccompany": "Summit Management Corp.",
"doctitle": "Firearm Industry Sales Representative"
}
The PHP feeder reads from the MySQL table of structured job content, organises the values into JSON as above, and sends them to Elasticsearch. I also built in the ability to recognise documents that have already been indexed, so the next run of the feeder does not push the same content again. This allows documents to be added to the index incrementally.
Step 4: Write a search endpoint for keyword search
We now have an Elasticsearch index with several thousand searchable job ads. Next, we create a PHP back end called search.php that receives search parameters from the search interface (which does not exist yet) and constructs the JSON query for Elasticsearch. We start with two parameters, keywords and location, and explore different ways of constructing the JSON query for keyword search. The query is sent to the endpoint http://127.0.0.1:9200/jobs/_search:
GET /jobs/_search
{
"from": 0,
"size": 5,
"query": {
"match": {
"doctitle": "Product Manager"
}
}
}
Our first iteration is the most basic keyword search, with the two-word search term “Product Manager” coming from the interface. We begin with the match query, as above, targeting the doctitle field. This returns documents that match any of the query words. In other words, the default Boolean operator OR is applied between the words, which can be rewritten as below. In our corpus of 7,005 documents, both queries return 554 results. We set size to 5 to limit the number of results per batch.
GET /jobs/_search
{
"from": 0,
"size": 5,
"query": {
"match": {
"doctitle": {
"query": "product manager",
"operator": "or"
}
}
}
}
The same query can also be expressed with a bool query and the should clause. There are four types of clause in a bool query: must, should, filter and must_not, and they are fairly self-explanatory. Note that when a bool query contains no must or filter clause, at least one should clause has to match. In other cases, should clauses are optional and only contribute to the score of each match for ranking.
GET /jobs/_search
{
"from": 0,
"size": 5,
"query": {
"bool": {
"should": [
{ "term": { "doctitle": "product" } },
{ "term": { "doctitle": "manager" } }
]
}
}
}
We know this approach is sub-optimal, especially in vertical search. It tends to return too many results, many of them irrelevant. Instead of matching any word in the search term, we need to return only documents that contain all the words (both of them, in the case of “Product Manager”). We revise the JSON query to use the must clause instead of should, as below. Alternatively, we could set the operator from OR to AND in the match version. The result count drops to 17.
GET /jobs/_search
{
"from": 0,
"size": 5,
"query": {
"bool": {
"must": [
{ "term": { "doctitle": "product" } },
{ "term": { "doctitle": "manager" } }
]
}
}
}
To find more potentially relevant results, we broaden the search to other fields. Sometimes the words we want are not in the title of a document, so we also need to look in other fields, especially the text. This time, we combine the bool and match queries, as below. The query looks for documents that contain both words (in any order or distance apart, for now) in either the doctext or doctitle field. Running it in Insomnia, the retrieval grows from 17, when we searched only the title, to 290.
GET /jobs/_search
{
"from": 0,
"size": 5,
"query": {
"bool": {
"should": [
{ "match": { "doctext": { "query": "product manager", "operator": "and" } } },
{ "match": { "doctitle": { "query": "product manager", "operator": "and" } } }
]
}
}
}
What if we want to add another mandatory field? Say we want documents that contain both “Product” and “Manager” and are also in a certain location. We cannot simply append a must clause after the should clause above because, as mentioned, the presence of a must clause makes the matches on doctitle and doctext optional. Instead, we wrap another bool query around the one above, grouping it with a new match query on the doclocation field. With “New York” as the location and the same “Product Manager” keywords, the result count falls from 290 to 12. In other words, only 12 job ads with the exact location “New York” contain both words.
GET /jobs/_search
{
"from": 0,
"size": 5,
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{ "match": { "doctext": { "query": "product manager", "operator": "and" } } },
{ "match": { "doctitle": { "query": "product manager", "operator": "and" } } }
]
}
},
{ "match": { "doclocation": { "query": "New York" } } }
]
}
}
}
The inner bool query that matches “Product Manager” against doctitle and doctext can also be written as a multi_match query, as below. Setting the type to most_fields means that when the words match multiple fields, the scores across those fields are added together.
GET /jobs/_search
{
"from": 0,
"size": 5,
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "product manager",
"fields": ["doctitle", "doctext"],
"type": "most_fields",
"operator": "and"
}
},
{ "match": { "doclocation": { "query": "New York" } } }
]
}
}
}
Especially in vertical search, we often want to see how the results are distributed across certain dimensions. These dimensions are informative to users and can be offered as facets for filtering. In our corpus, say we want to use the doccompany field as a facet. To do this, we add an aggregations (aggs) object to the query to return the unique values in that field, with a count of documents next to each. We name the aggregation doccompanies:
GET /jobs/_search
{
"from": 0,
"size": 5,
"query": {
...
},
"aggs": {
"doccompanies": {
"terms": { "field": "doccompany" }
}
}
}
The results now include an aggregation of the doccompany field, as in the response below. The interface can use these key and count pairs to show filtering options to users. This is where the analyser explained earlier matters: if we had let doccompany be analysed, by setting its type to text, the buckets would come back as groupings of individual words in the company names, rather than the complete names.
{
...
"aggregations": {
"doccompanies": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{ "key": "Gap Inc.", "doc_count": 3 },
{ "key": "HSBC", "doc_count": 2 },
{ "key": "Nigel Frank", "doc_count": 2 },
{ "key": "Goldman Sachs & Co.", "doc_count": 1 },
{ "key": "Oracle", "doc_count": 1 },
{ "key": "RedHat Inc", "doc_count": 1 },
{ "key": "Scholastic", "doc_count": 1 },
{ "key": "The Forum Group", "doc_count": 1 }
]
}
}
}
Step 5: Create a search page that uses the endpoint
We now have a basic endpoint that receives keywords and a location, constructs the corresponding JSON query and accepts the response from Elasticsearch. At this stage, the response contains the individual hits and an aggregation on the doccompany field:
{
"took": 7,
"timed_out": false,
"hits": {
"total": { "value": 12, "relation": "eq" },
"max_score": 9.481624,
"hits": [
{
"_index": "jobs",
"_id": "b25d9d2887f77d0c051d4166537673a7",
"_score": 9.481624,
"_source": {
"docdate": "2016-10-05",
"doctext": "...",
"doclocation": "New York",
"doccompany": "Gap Inc.",
"doctitle": "Assistant Manager, Merchandising Gap..."
}
},
...
]
},
"aggregations": {
"doccompanies": {
"buckets": [
{ "key": "Gap Inc.", "doc_count": 3 },
...
]
}
}
}
Next, I created a basic search page called index.php, with two input boxes in a collapsible left panel. It uses jQuery AJAX methods to call search.php for results. You could easily build this as a React app today; the code for this article was written in 2016, so please excuse its age. What we code with, and how, is beside the point of this article. The page also queries Elasticsearch for the total document count, which it shows in the header.
When a search is submitted, the keywords and location are sent to search.php, our search endpoint. We extend the endpoint to parse the JSON response from Elasticsearch: it loops through the hits, keeping the fields we want to display, and parses the key and count pairs in the aggregation, which are returned to the page and shown as a companies facet. Because each page shows only five results, we also handle pagination, passing a page number between the page, the endpoint and Elasticsearch, and using the from and size attributes in the JSON query. For the “Product Manager” in “New York” search, the page shows the result count falling from 7,005 to 12 jobs.
Step 6: Add filtering and sorting
So far, we only use keywords and location to decide what is retrieved, and to score results (with the out-of-the-box configuration) by keyword matching. In some cases, search criteria are used only to slice and dice the retrieved results, and do not contribute to scoring or ranking. The difference between retrieval and ranking is discussed in detail in Precise retrieval for tuning ranking.
In this exercise, we use the companies facet to filter the result set. To do this, we extend the parameters passed from the page to the search endpoint, and revise the JSON query by adding a filter context after the must clause inside the bool query. If the user selects “Gap Inc.”, it becomes the third search criterion, alongside “Product Manager” and “New York”:
GET /jobs/_search
{
"from": 0,
"size": 5,
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "Product Manager",
"fields": ["doctitle", "doctext"],
"type": "most_fields",
"operator": "and"
}
},
{ "match": { "doclocation": { "query": "New York" } } }
],
"filter": [
{ "term": { "doccompany": "Gap Inc." } }
]
}
},
"aggs": {
"doccompanies": {
"terms": { "field": "doccompany" }
}
}
}
We also extend the search page so users can click on the companies facet in the left panel, as well as on the company name shown against each job ad, to run a new search filtered by that company. Selecting “Gap Inc.” brings the number of results down from 12 to 3.
By default, results are sorted by a score computed with the out-of-the-box configuration for keyword matching. To make this explicit, we can add a sort parameter that sorts on the _score meta-field, which is keyword relevance and the default. Users may also want to re-sort the results, for example to see newer job ads first. To sort by our only date field, we use docdate:
GET /jobs/_search
{
"from": 0,
"size": 5,
"query": {
...
},
"aggs": {
...
},
"sort": {
"docdate": { "order": "desc" }
}
}
On the search page, we add an option to sort by relevance or by date. We now have five search parameters implemented from the page all the way to the Elasticsearch query: keywords, location, company, page number and sort order.
Step 7: Track searches and results
Many people would consider search done at this point, but this could not be further from the truth. Looking at the results so far, there are several quality concerns:
- The results do not look very relevant. Typically, there are two reasons: either there are no job ads matching the search criteria, or the keyword matching and sorting can be improved.
- How do we know whether users find these results relevant or useful?
- Do we know whether users are seeing most of the results they are meant to see?
To answer these questions and more, we need proper tracking. Getting the right tracking need not be difficult, and there are JavaScript-based tools that can help. For this exercise, I quickly put together a custom tracker. Because tracking is integral to search quality, and the things we need to track can be very fine-grained, we need to be careful about adopting off-the-shelf solutions. In my experience, many free tools are designed for SEO and product analytics, so they are better suited to aggregated metrics on visits, users, page views and sessions. The different types of data, and how they are used for data science or analytics, need to be front of mind when choosing technology or tools, as discussed in Four hurdles to creating value from data.
Broadly, there are four types of data to track: the search criteria, the results shown to users, unique identifiers for the user and the search, and the interactions with the results. Some of this can be tracked in the back end by the search endpoint, while some has to come from the page. The search criteria, the results shown and the identifiers are all recorded (and some generated) in search.php when a search runs. Interactions are front-end events, so they can only come from the page: whenever someone clicks on a result, an event fires and triggers a request to a separate endpoint that logs it. I created another endpoint, logger.php, which is called asynchronously and accepts the document identifier, the position and type of the interaction, and a timestamp. These events are stored in another MySQL table. Like most things set up for this exercise, they are not fit for production.
Step 8: Introduce basic metrics to gauge success
With the right tracking in place, we can look at how well the search results perform when we present them to users. Search is about helping users satisfy information needs, and whether we are doing that varies from user to user.
Until users tell us explicitly what they are after, figuring out what success looks like for them is mostly guesswork. With properly tracked data and metrics, however, we can make more educated guesses about success, improve our understanding of user needs, and keep improving search empirically.
For the following examples, we keep using “Product Manager” as the search term. Assume we have three users, each with a different information need that should show in how they interact with the results:
- User 1 is looking for product management opportunities in online tech, in any location. They search for “Product Manager” with no location or company criteria, in the default relevance order, and get 290 results.
- User 2 wants a new technology product management role in New York. They search for “Product Manager” in “New York”, in the default relevance order, and see 12 job ads.
- User 3 has been searching for product management roles every day. They search for “Product Manager” with no other criteria, re-sorted by date so they see the most recently posted first.
We use the most recent JSON query from step 6 for these searches, and treat it as the base algorithm. With tracking in place, we can see which job ads were presented to each user and which were clicked. Note that at this stage we are naively using clicks on results as the indicator of success, which can be flawed. In job search, a more appropriate signal would be a job application, or if clicks are used, they should be supplemented with other signals, such as the time users spend reading a job ad.
Let us assume User 1 visited only the first two pages, so 10 results were presented (impressions), and clicked on the jobs in positions 4 (by GoDaddy.com) and 8 (by Amazon). User 2 clicked on nothing, which is not surprising, as none of the jobs were relevant despite being in New York. User 3 did not click on anything either, since none of the jobs were specifically about product management. Both Users 2 and 3 paged through to page 3, hoping to find something relevant, without success.
We naively use three measures to demonstrate the empirical nature of search improvement. The first, impression depth, is not really a success metric. It quantifies how deep users go in inspecting the results. If impression depth is consistently low, users generally do not go far down the results, which should make us question the value of returning so many, especially if many of those in the long tail are irrelevant. The second is click-through rate, though not in the online advertising sense: it captures how many of the results a user saw in a search were clicked. A low click-through rate can mean the results are not very relevant, but it can also indicate other things, which is why we need to be wary of using clicks directly in measurement. For Users 2 and 3, the click-through rate is 0%: none of the results they were shown attracted any interest. For User 1, a click-through rate of 20% means, on average, one in five jobs was clicked. The average of 6.7% is very poor.
The third metric is precision at lowest click. It treats the position of the lowest click as the last result the user inspected, and captures how many of the results between first position and that lowest position were clicked (or were “precise”). For the base algorithm, this metric was only slightly better than the click-through rate, at 8.3%, and still not good.
Step 9: Improve the precision of retrieval
With tracking and a basic form of measurement in place, we are better placed to turn our attention to the quality of the results. This is the point where understanding and improving search quality can truly begin, and building a search engine graduates from a purely technical pursuit.
Informed by the metrics, we can look at why the results performed so poorly for Users 2 and 3. Reproducing their results from the tracked data, we quickly find that both result sets contain far too many irrelevant results. Why were those documents returned? To answer this, we look back at the bool query we constructed to retrieve documents by keyword matches on doctext and doctitle.

The first job ad User 3 saw did not come back because the two words were in its title. Its text had one occurrence of the word “product” and several of “manager”, and the closest pair was far apart, resulting in an out-of-context match. When users type “Product Manager”, they clearly do not see it as a bag of words, but our search logic did, and that produced irrelevant results. This was discussed in detail in Faceted search needs precise retrieval.
The quickest fix is to restrict how far apart the matched words can be. In Elasticsearch, we change the type of the multi_match query to phrase and use slop instead of the AND operator, setting slop to 1:
GET /jobs/_search
{
"from": 0,
"size": 5,
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "Product Manager",
"fields": ["doctitle", "doctext"],
"type": "phrase",
"slop": 1
}
}
],
"filter": [
...
]
}
},
"aggs": {
...
},
"sort": {
...
}
}
We applied the change in the search endpoint and repeated the same searches. This time, instead of 12 product management jobs in New York, User 2 got none. For Users 1 and 3, who searched for “Product Manager” with no location, the results dropped from 290 to 19. Overall, many of the irrelevant results previously shown are no longer retrieved, especially for Users 2 and 3. For User 1, the top results before and after the change look much the same, even though the retrieval fell from 290 to 19, because the job ads at the top were fairly relevant anyway. When results are sorted by relevance, the irrelevant ones are often pushed down the ranking and stay out of sight. Depending on the content and the vertical, we need to find the right balance between being overly restrictive, and missing relevant results, and being too relaxed, and bringing back irrelevant ones.
- User 1 still finds the jobs in positions 7 and 8 relevant and clicks on them, as before.
- User 2 now has no results, instead of the 12 irrelevant job ads seen previously.
- User 3 sees the same 19 results as User 1, but sorted by date, and this time all the top results are relevant. User 3 clicks on the job ads in positions 2 and 5.
To quantify the impact of the change, we update the metrics. Impression depth increased, meaning more of the returned results were seen, because the change cut the retrieval set considerably. Having more results seen is not enough; we also need them to be clicked. The click-through rate rose from the base algorithm’s 6.7% to 20.0%, and precision at lowest click improved from 8.3% to 21.7%. Both metrics suggest we are doing something right for users with this change.
Step 10: Improve recall with synonym expansion
In this last step, we look at another improvement, informed by the metrics. Different people sometimes refer to the same things in different ways, and a search engine does not know this by default. As a result, it may miss documents that are relevant to a query just because they are expressed differently. Query or synonym expansion can bridge that gap.
In this exercise, we assume “Product Manager” and “Product Lead” refer to the same job title. Synonyms like these can be curated by domain experts or learned from logs, and the aim is to use them in Elasticsearch to improve the recall of the retrieval. In other words, we want to reduce the chance of the search engine, and so users, missing relevant job ads. When users search for “Product Manager”, they also get results containing “Lead” where “Manager” is absent.
To do this, we change the analyser and filter components of the index. We create a new filter of type synonym, called jobsynonyms, that points to the file containing the synonyms, synonym.csv, placed in the config folder of the Elasticsearch installation. We also create a new analyser, synonym_analyzer, that applies the jobsynonyms filter during analysis to expand synonyms. The index has to be closed before the settings change is applied and reopened afterwards.
POST /jobs/_close
PUT /jobs/_settings
{
"settings": {
"index": {
"analysis": {
"analyzer": {
"synonym_analyzer": {
"tokenizer": "whitespace",
"filter": ["lowercase", "jobsynonyms"]
}
},
"filter": {
"jobsynonyms": {
"type": "synonym",
"synonyms_path": "synonym.csv",
"updateable": true
}
}
}
}
}
}
POST /jobs/_open
To use synonym_analyzer on the fields we want to expand synonyms for at search time, we change the mappings of those fields by sending the query below to the _mapping endpoint. We apply the analyser only to doctitle, to limit the effect of the expansion.
PUT /jobs/_mapping
{
"properties": {
"doctitle": {
"type": "text",
"analyzer": "standard",
"search_analyzer": "synonym_analyzer"
}
}
}
After applying these changes, we repeated the same searches for Users 1, 2 and 3. User 2, who had no results after the word-distance restriction, now sees one job ad. The results for Users 1 and 3 are much the same as before, which is expected: there are enough jobs matching the original “Product Manager” query for them to dominate the top.
- User 1’s results increased from 19 to 21, but the experience was much the same, and the job ads in positions 7 and 8 are still the relevant ones.
- User 2 now has one result, a product lead role, instead of none.
- User 3 saw two more results, but the top results by date, including the ones clicked before, stayed in the same positions.
Synonym expansion, and maintaining the synonyms behind it, is an ongoing process. As new synonyms are added, we want them to take effect immediately. Because we set updateable to true when creating the synonym filter, we can reload the search analysers with the query below:
POST /jobs/_reload_search_analyzers
Before we finish, let us look at the effect of synonym expansion on user interactions and the metrics. Across the board, one or two more job ads were returned, depending on whether the “New York” location was applied. For Users 1 and 3, the result set grew from 19 to 21. Synonym-expanded results are often weighted lower than original keyword matches, which can be configured. Since the results Users 1 and 3 found relevant before the expansion stayed in their original positions, their search quality was not affected, and their click-through rate and precision at lowest click stayed the same. For User 2, because of the location filter, only one job was returned through synonym expansion rather than two. Assuming User 2 finds that product lead job ad relevant, we have potentially improved recall.
Through synonym expansion, we improved search quality for User 2. Overall, the click-through rate improved from 20.0% to 53.3%, and precision at lowest click from 21.7% to 55.0%.

Conclusion
Building and maintaining a search system or product that is set up for success takes more than the right technology. In this article, we set up a basic search engine for job ads using Elasticsearch to illustrate search quality in practice. We tracked the essential data to calculate basic metrics and to better understand user needs from their searches and their interactions with the results. We started with a base algorithm and made two improvements, each informed by metrics, and each moved the metrics further in the right direction.
We relied heavily on clicks in this article, purely to demonstrate the importance of setting up the right foundations for improving search. As I have said many times, be careful when using clicks. The technology aside, I hope this article sheds some light on what it takes to build a strong search engine and product. Beyond the improvements explored here, there are many other ways to optimise the search experience for users.