liveBook · Manning

Semantic function

As explained in AI-Powered Search

A semantic function is a domain-specific computational operation that interprets or transforms queries according to meaning rather than literal terms.

A search for “top kimchi near Charlotte” asks for more than literal term matches. “Top” calls for a ranking decision, while “near” relates the results to a place. A semantic function is a domain-specific operation applied during query parsing and enrichment to interpret such surrounding terms and change the query accordingly. section 7.4.2

The query pipeline handles semantic functions in three steps:

  • Extract known surface forms into a typed query tree. An entity can carry a canonical form, popularity value, and function call. If a surface form has several meanings, the tree builder initially chooses the most popular entity. section 7.4.1
  • Walk the tree and run each semantic function at its position. A function inspects nearby nodes, replaces or consumes nodes when its conditions hold, and reports whether it succeeded. An unsuccessful call is marked as an invalid semantic function. section 7.4.2
  • Transform remaining nodes into search-engine syntax. The demonstrated functions put Solr syntax directly into transformed nodes; a separate engine-neutral representation is possible but not used here. section 7.4.4

Interpreting “top kimchi near Charlotte”

The tree contains a popularity function for “top,” a keyword for “kimchi,” a location-distance function for “near,” and a city entity with Charlotte’s coordinates. The popularity function substitutes a Solr scoring expression based on stars_rating. The location function checks that the next node is a city, consumes it, and substitutes a geographic filter with a 50 km distance. “Kimchi” can be enriched separately with related terms and a category from a semantic knowledge graph. The resulting nodes combine ranking, content matching, and location filtering instead of treating every word as a keyword. section 7.4.1 section 7.4.2

Listing 7.10 A semantic function that accounts for location
def location_distance(query, position):
  if len(query["query_tree"]) -1 > position:  #1
    next_entity = query["query_tree"][position + 1] #1
    if next_entity["type"] == "city": #2

      query["query_tree"].pop(position + 1)  #3
      query["query_tree"][position] = {  #4
        "type": "transformed",  #4
        "syntax": "solr",  #4
        "query": create_geo_filter(  #4
          next_entity['location_coordinates'],  #4
          "location_coordinates", 50)}  #4
      return True
  return False  #5

def create_geo_filter(coordinates, field, distance_KM):
  return f'+{!geofilt d={distance_KM} sfield="{field}" pt="{coordinates}"}'

Inspect the location function to see its condition, consumption of the city node, and construction of the Solr geographic filter. section 7.4.2

Semantic functions are useful when connecting words express intent that keyword matching misses. Their rules remain domain-specific: a location rule succeeds only when the following entity is a city, and the shown output embeds search-engine-specific syntax. Other enrichment, such as related-term expansion, operates alongside these functions rather than replacing them. section 7.4.2 section 7.4.4

How AI-Powered Search covers it

How deep it goes
Works through entity tagging, typed trees, conditional Python functions, failure handling, and Solr output; defers a generic engine-neutral representation.
Examples
Uses “top kimchi near Charlotte” to produce a stars-rating scoring expression, related-term expansion, and a 50 km geographic filter.
What it assumes
Builds on extracted entities and a typed query tree, then shows how to implement the functions that process them.
What you can do after
Build it from scratch as conditional Python query-tree transformations that emit Solr ranking and filtering clauses.
How it connects
Connects entity extraction and query parsing to geographic filtering, relevance scoring, semantic knowledge graphs, and search-engine query transformation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI-Powered Search. The section links point to the parts of the book it draws on.