Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions querybook/server/datasources/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,32 @@ def _data_doc_access_terms(user_id):
]


def _match_table_fields(fields):
def _match_table_word_fields(fields):
search_fields = []
for field in fields:
# 'table_name', 'description', and 'column' are fields used by Table search
if field == "table_name":
search_fields.append("full_name^15")
search_fields.append("full_name^2")
search_fields.append("full_name_ngram")
elif field == "description":
search_fields.append("description")
elif field == "column":
search_fields.append("columns")

return search_fields


def _match_table_phrase_queries(fields, keywords):
phrase_queries = []
for field in fields:
if field == "table_name":
phrase_queries.append(
{"match_phrase": {"full_name": {"query": keywords, "boost": 10}}}
)
elif field == "column":
phrase_queries.append({"match_phrase": {"columns": {"query": keywords}}})
return phrase_queries


def _match_data_doc_fields(fields):
search_fields = []
for field in fields:
Expand All @@ -170,27 +181,31 @@ def _match_data_doc_fields(fields):
def _construct_tables_query(
keywords, filters, fields, limit, offset, concise, sort_key=None, sort_order=None,
):

search_query = {}
if keywords:
search_fields = _match_table_fields(fields)
search_query["multi_match"] = {
"query": keywords,
"fields": search_fields,
"minimum_should_match": "100%",
"type": "phrase",
"operator": "and",
search_query = {
"bool": {
"must": {
"multi_match": {
"query": keywords,
"fields": _match_table_word_fields(fields),
# All words must appear in a field
"operator": "and",
},
},
"should": _match_table_phrase_queries(fields, keywords),
}
}
else:
search_query["match_all"] = {}
search_query = {"match_all": {}}

search_query = {
"function_score": {
"query": search_query,
"boost_mode": "multiply",
"script_score": {
"script": {
"source": "doc['importance_score'].value + (doc['golden'].value ? 2 : 0)"
"source": "1 + (doc['importance_score'].value + (doc['golden'].value ? 1 : 0))"
}
},
}
Expand Down
3 changes: 2 additions & 1 deletion querybook/server/logic/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ def get_tables_iter(batch_size=5000, session=None):
@with_session
def get_table_weight(table_id: int, session=None) -> int:
"""Calculate the weight of table. Used for ranking in auto completion
and sidebar table search.
and sidebar table search. It produces a number >= 0


Arguments:
table_id {int} -- Id of DataTable
Expand Down
3 changes: 1 addition & 2 deletions querybook/webapp/redux/dataTableSearch/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
const BATCH_LOAD_SIZE = 100;

function mapStateToSearch(state: IDataTableSearchState) {
let searchString = state.searchString;
const searchString = state.searchString;

const filters = Object.entries(state.searchFilters).filter(
([_, filterValue]) => filterValue != null
Expand All @@ -20,7 +20,6 @@ function mapStateToSearch(state: IDataTableSearchState) {
const matchSchemaName = searchString.match(/(\w+)\.(\w*)/);
if (matchSchemaName) {
filters.push(['schema', matchSchemaName[1]]);
searchString = searchString.replace(/(\w+)\.(\w*)/, '$2');
}

const searchParam = {
Expand Down