A query tree represents a parsed search query as typed nodes containing keywords, entities, and their metadata. Unlike a tagged query string, it gives a search pipeline a structure it can manipulate to interpret relationships between terms. In this implementation, the initial tree is an ordered list of JSON objects. section 7.4.1
To build the tree, extract entity tags from the query and index the returned entities by ID. For each tag, add any preceding untagged text as a keyword node, then add the matching entity with the highest popularity. Add any remaining text as a final keyword node. Entity nodes retain metadata such as canonical form, type, and semantic function. section 7.4.1
Parsing a restaurant query
For “top kimchi near charlotte,” the initial tree contains a popularity semantic-function node for “top,” a keyword node for “kimchi,” a location-distance semantic-function node for “near,” and a city node for Charlotte. The city node includes coordinates; the keyword node preserves its surface and canonical forms. section 7.4.1
Listing 7.8 Generating a typed query tree from a user query
def generate_query_tree(extracted_entities):
query = extracted_entities["query"]
entities = {entity["id"]: entity for entity #1
in extracted_entities["entities"]} #1
query_tree = []
last_end = 0
for tag in extracted_entities["tags"]:
best_entity = entities[tag["ids"][0]] #2
for entity_id in tag["ids"]: #2
if (entities[entity_id]["popularity"] > #2
best_entity["popularity"]): #2
best_entity = entities[entity_id] #2
next_text = query[last_end:tag["startOffset"]].strip()
if next_text:
query_tree.append({"type": "keyword", #3
"surface_form": next_text, #3
"canonical_form": next_text}) #3
query_tree.append(best_entity) #4
last_end = tag["endOffset"]
if last_end < len(query): #5
final_text = query[last_end:len(query)].strip() #5
if final_text: #5
query_tree.append({"type": "keyword", #5
"surface_form": final_text, #5
"canonical_form": final_text}) #5
return query_tree
parsed_query = generate_query_tree(query_entities)
display(parsed_query)
Follow the construction of keyword and entity nodes from extraction offsets and candidate popularity. section 7.4.1
Enrichment can change or replace nodes. For example, the popularity function replaces “top” with a rating-based Solr expression when another node follows. The location-distance function consumes a following city node and replaces “near” with a geographic filter. Keyword nodes can also be enriched with related terms and a category from a semantic knowledge graph. section 7.4.2
Finally, an adapter transforms enriched nodes into search-engine-specific syntax. Nodes already transformed by semantic functions are left in place; the resulting tree can be converted to a string and sent to the configured engine. Directly inserting engine syntax during enrichment simplifies the implementation but couples those functions to that engine; a generic intermediate representation would defer that choice. section 7.4.4
[{"type": "transformed",
"syntax": "solr",
"query": "+{!func v=\"mul(if(stars_rating,stars_rating,0),20)\"}"},
{"type": "transformed",
"syntax": "solr",
"query": "{!edismax v=\"kimchi^0.9193 korean^0.7069 banchan^0.6593
+doc_type:\\\"Korean\\\"\"}"},
{"type": "transformed",
"syntax": "solr",
"query": "+{!geofilt d=50 sfield=\"location_coordinates\"
pt=\"35.22709,-80.84313\"}"}]
Inspect the transformed Solr nodes for the restaurant query: a rating expression, an enriched keyword query, and a geographic filter. section 7.4.4
