Skip to content

Commit

Permalink
Allow to search key sentences etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
yjcyxky committed Sep 11, 2024
1 parent dabf3c6 commit 9176714
Show file tree
Hide file tree
Showing 8 changed files with 311 additions and 275 deletions.
423 changes: 178 additions & 245 deletions src/api/route.rs

Large diffs are not rendered by default.

59 changes: 49 additions & 10 deletions src/model/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,7 @@ impl KnowledgeCuration {
fingerprint: Option<&str>,
project_id: i32,
organization_id: i32,
query: Option<ComposeQuery>,
page: Option<u64>,
page_size: Option<u64>,
order_by: Option<&str>,
Expand Down Expand Up @@ -949,9 +950,18 @@ impl KnowledgeCuration {
format!("curator IS NOT NULL")
};

let mut query_str = match query {
Some(query) => query.to_string(),
None => "".to_string(),
};

if query_str.is_empty() {
query_str = "1=1".to_string();
}

let where_str = format!(
"{} AND {} AND {} AND {}",
curator_qstr, project_id_qstr, organization_id_qstr, fingerprint_qstr
"{} AND {} AND {} AND {} AND ({})",
curator_qstr, project_id_qstr, organization_id_qstr, fingerprint_qstr, query_str
);

let page = match page {
Expand Down Expand Up @@ -1194,6 +1204,7 @@ impl EntityMetadataCuration {
curator: &str,
project_id: i32,
organization_id: i32,
query: Option<ComposeQuery>,
page: Option<u64>,
page_size: Option<u64>,
order_by: Option<&str>,
Expand Down Expand Up @@ -1222,9 +1233,18 @@ impl EntityMetadataCuration {
format!("curator IS NOT NULL")
};

let mut query_str = match query {
Some(query) => query.to_string(),
None => "".to_string(),
};

if query_str.is_empty() {
query_str = "1=1".to_string();
}

let where_str = format!(
"{} AND {} AND {} AND {}",
curator_qstr, project_id_qstr, organization_id_qstr, fingerprint_qstr
"{} AND {} AND {} AND {} AND ({})",
curator_qstr, project_id_qstr, organization_id_qstr, fingerprint_qstr, query_str
);

let page = match page {
Expand Down Expand Up @@ -1536,6 +1556,7 @@ impl KeySentenceCuration {
curator: &str,
project_id: i32,
organization_id: i32,
query: &Option<ComposeQuery>,
page: Option<u64>,
page_size: Option<u64>,
order_by: Option<&str>,
Expand Down Expand Up @@ -1564,9 +1585,18 @@ impl KeySentenceCuration {
format!("curator IS NOT NULL")
};

let mut query_str = match query {
Some(query) => query.to_string(),
None => "".to_string(),
};

if query_str.is_empty() {
query_str = "1=1".to_string();
}

let where_str = format!(
"{} AND {} AND {} AND {}",
curator_qstr, project_id_qstr, organization_id_qstr, fingerprint_qstr
"{} AND {} AND {} AND {} AND ({})",
curator_qstr, project_id_qstr, organization_id_qstr, fingerprint_qstr, query_str
);

let page = match page {
Expand Down Expand Up @@ -1970,6 +2000,7 @@ impl EntityCuration {
fingerprint: &str,
project_id: i32,
organization_id: i32,
query: &Option<ComposeQuery>,
page: Option<u64>,
page_size: Option<u64>,
order_by: Option<&str>,
Expand Down Expand Up @@ -1998,9 +2029,18 @@ impl EntityCuration {
format!("curator IS NOT NULL")
};

let mut query_str = match query {
Some(query) => query.to_string(),
None => "".to_string(),
};

if query_str.is_empty() {
query_str = "1=1".to_string();
};

let where_str = format!(
"{} AND {} AND {} AND {}",
curator_qstr, project_id_qstr, organization_id_qstr, fingerprint_qstr
"{} AND {} AND {} AND {} AND ({})",
curator_qstr, project_id_qstr, organization_id_qstr, fingerprint_qstr, query_str
);

let page = match page {
Expand Down Expand Up @@ -2613,8 +2653,7 @@ impl RelationCount {
query: &Option<ComposeQuery>,
) -> Result<Vec<RelationCount>, anyhow::Error> {
let mut query_str = match query {
Some(ComposeQuery::QueryItem(item)) => item.format(),
Some(ComposeQuery::ComposeQueryItem(item)) => item.format(),
Some(query) => query.to_string(),
None => "".to_string(),
};

Expand Down
1 change: 1 addition & 0 deletions src/model/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,7 @@ impl Graph {
None, // TODO: Do we need to filter the knowledges by website fingerprint?
project_id,
organization_id,
None,
page,
page_size,
order_by,
Expand Down
30 changes: 28 additions & 2 deletions src/query_builder/sql_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ pub struct QueryItem {
impl QueryItem {
pub fn new(field: String, value: Value, operator: String) -> Self {
let allowed_operators = vec![
"=", "!=", "like", "not like", "ilike", "in", "not in", "<>", "<", ">", "<=", ">=", "is", "is not"
"=", "!=", "like", "not like", "ilike", "in", "not in", "<>", "<", ">", "<=", ">=",
"is", "is not",
];
if !allowed_operators.contains(&operator.as_str()) {
panic!("Invalid operator: {}", operator);
Expand Down Expand Up @@ -163,6 +164,27 @@ pub enum ComposeQuery {
ComposeQueryItem(ComposeQueryItem),
}

impl ComposeQuery {
pub fn to_string(&self) -> String {
let mut query_str = match self {
ComposeQuery::QueryItem(item) => item.format(),
ComposeQuery::ComposeQueryItem(item) => item.format(),
};

query_str
}

pub fn from_str(query_str: &str) -> Result<Option<Self>, serde_json::Error> {
let query = if query_str == "" {
None
} else {
Some(serde_json::from_str(&query_str)?)
};

Ok(query)
}
}

impl ComposeQueryItem {
pub fn new(operator: &str) -> Self {
Self {
Expand Down Expand Up @@ -292,7 +314,11 @@ pub fn make_order_clause(fields: Vec<String>) -> String {
pub fn make_order_clause_by_pairs(pairs: Vec<(String, String)>, topk: usize) -> String {
let mut topk_pairs = Vec::new();
if topk != 0 {
let k = if pairs.len() < topk { pairs.len() } else { topk };
let k = if pairs.len() < topk {
pairs.len()
} else {
topk
};
topk_pairs = pairs[0..k].to_vec();
} else {
topk_pairs = pairs;
Expand Down
37 changes: 35 additions & 2 deletions studio/src/pages/KnowledgeCuration/KeySentenceTable.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useState, forwardRef, useImperativeHandle } from 'react';
import { Table, Row, Tag, Space, message, Popover, Button } from 'antd';
import { Table, Row, Tag, Space, message, Popover, Button, Input } from 'antd';
import type { ColumnsType } from 'antd/es/table';
import { KeySentence, KeySentenceTableData } from './typings';
import { deleteKeySentenceCuration, fetchKeySentenceCurationByOwner } from '@/services/swagger/KnowledgeGraph';
Expand All @@ -20,6 +20,7 @@ const KeySentenceTable: React.FC<KeySentenceTableProps> = forwardRef((props, ref
const [page, setPage] = useState<number>(props.page || 1);
const [pageSize, setPageSize] = useState<number>(props.pageSize || 30);
const [refreshKey, setRefreshKey] = useState<number>(0);
const [searchText, setSearchText] = useState<string>('');

useImperativeHandle(ref, () => ({
downloadTable() {
Expand Down Expand Up @@ -156,11 +157,33 @@ const KeySentenceTable: React.FC<KeySentenceTableProps> = forwardRef((props, ref
},
];

const makeSearchQuery = (searchText: string) => {
if (!searchText) {
return undefined;
}

const composedQuery = {
operator: 'or',
items: [{
operator: 'ilike',
field: 'key_sentence',
value: `%${searchText}%`,
}, {
operator: 'ilike',
field: 'description',
value: `%${searchText}%`,
}],
}

return JSON.stringify(composedQuery);
};

useEffect(() => {
setLoading(true);
fetchKeySentenceCurationByOwner({
page: page,
page_size: pageSize,
query_str: makeSearchQuery(searchText)
})
.then((response) => {
setData({
Expand All @@ -179,14 +202,24 @@ const KeySentenceTable: React.FC<KeySentenceTableProps> = forwardRef((props, ref
setData({} as KeySentenceTableData);
setLoading(false);
});
}, [page, pageSize, refreshKey]);
}, [page, pageSize, refreshKey, searchText]);

const getRowKey = (record: KeySentence) => {
return record.id || `${JSON.stringify(record)}`;
};

return (
<Row className="key-sentence-table-container">
<Input.Search
placeholder="Search by Keywords [AI Search Functionality Coming Soon]"
allowClear
enterButton="Search"
size="middle"
onSearch={(value) => {
setSearchText(value);
}}
style={{ marginBottom: '10px' }}
/>
<Table
className={props.className + ' key-sentence-table'}
style={props.style}
Expand Down
2 changes: 1 addition & 1 deletion studio/src/pages/KnowledgeCuration/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
flex-direction: column;
height: 100%;
width: calc(100% - 40px);
padding: 20px;
padding: 0 20px;
margin: 10px auto;
background-color: #fff;
}
30 changes: 15 additions & 15 deletions studio/src/services/swagger/KnowledgeGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,21 @@ export async function postEntityCuration(
});
}

/** Call `/api/v1/entity-curations` with payload to delete a entity curation. DELETE /api/v1/entity-curations */
export async function deleteEntityCurationRecord(
// 叠加生成的Param类型 (非body参数swagger默认没有生成对象)
params: swagger.deleteEntityCurationRecordParams,
options?: { [key: string]: any },
) {
return request<any>('/api/v1/entity-curations', {
method: 'DELETE',
params: {
...params,
},
...(options || {}),
});
}

/** Call `/api/v1/entity-curations-by-owner` with query params to fetch entity curations by owner. GET /api/v1/entity-curations-by-owner */
export async function fetchEntityCurationByOwner(
// 叠加生成的Param类型 (非body参数swagger默认没有生成对象)
Expand Down Expand Up @@ -290,21 +305,6 @@ export async function deleteEntityCuration(
});
}

/** Call `/api/v1/entity-curations` with payload to delete a entity curation. DELETE /api/v1/entity-curations */
export async function deleteEntityCurationRecord(
// 叠加生成的Param类型 (非body参数swagger默认没有生成对象)
params: swagger.deleteEntityCurationRecordParams,
options?: { [key: string]: any },
) {
return request<any>('/api/v1/entity-curations', {
method: 'DELETE',
params: {
...params,
},
...(options || {}),
});
}

/** Call `/api/v1/entity-metadata` with query params to fetch all entity metadata. GET /api/v1/entity-metadata */
export async function fetchEntityMetadata(options?: { [key: string]: any }) {
return request<swagger.EntityMetadata[]>('/api/v1/entity-metadata', {
Expand Down
4 changes: 4 additions & 0 deletions studio/src/services/swagger/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ declare namespace swagger {
fingerprint?: string;
project_id?: string;
organization_id?: string;
query_str?: string;
page?: number;
page_size?: number;
};
Expand Down Expand Up @@ -369,6 +370,7 @@ declare namespace swagger {
organization_id?: string;
page?: number;
page_size?: number;
query_str?: string;
};

type fetchEntityCurationParams = {
Expand All @@ -381,6 +383,7 @@ declare namespace swagger {
fingerprint?: string;
project_id?: string;
organization_id?: string;
query_str?: string;
page?: number;
page_size?: number;
};
Expand All @@ -398,6 +401,7 @@ declare namespace swagger {
organization_id?: string;
page?: number;
page_size?: number;
query_str?: string;
};

type fetchKeySentenceCurationParams = {
Expand Down

0 comments on commit 9176714

Please sign in to comment.