Skip to content

Commit

Permalink
fix: throw error if key_field is indexed (#1520)
Browse files Browse the repository at this point in the history
  • Loading branch information
rebasedming authored Aug 13, 2024
1 parent 19d3520 commit 6b82968
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
15 changes: 15 additions & 0 deletions docs/search/full-text/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,21 @@ CALL paradedb.create_bm25(
and filtering.
</ParamField>

## Choosing a Key Field

The `key_field` option is used to uniquely identify documents within an index and cannot be tokenized. For instance,
the following configuration is not allowed:

```sql
-- This will throw an error
CALL paradedb.create_bm25(
index_name => 'search_idx',
table_name => 'mock_items',
key_field => 'description',
text_fields => paradedb.field('description')
);
```

## Multiple Fields

The `||` operator can be used to index multiple fields.
Expand Down
7 changes: 7 additions & 0 deletions pg_search/src/bootstrap/create_bm25.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ fn create_bm25_impl(
Ok(obj) => {
if let Value::Object(map) = obj {
for key in map.keys() {
if key == key_field {
bail!(
"key_field {} cannot be included in text_fields, numeric_fields, boolean_fields, json_fields, or datetime_fields",
spi::quote_identifier(key.clone())
);
}

column_names.insert(spi::quote_identifier(key.clone()));
}
}
Expand Down
4 changes: 0 additions & 4 deletions pg_search/src/postgres/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ pub extern "C" fn ambuild(
let index_relation = unsafe { PgRelation::from_pg(indexrel) };
let index_oid = index_relation.oid();

info!("ambuild {:?}", index_oid);

let rdopts: PgBox<SearchIndexCreateOptions> = if !index_relation.rd_options.is_null() {
unsafe { PgBox::from_pg(index_relation.rd_options as *mut SearchIndexCreateOptions) }
} else {
Expand Down Expand Up @@ -211,8 +209,6 @@ pub extern "C" fn ambuild(
let writer_client = WriterGlobal::client();
let directory = WriterDirectory::from_index_oid(index_oid.as_u32());

info!("create index {:?}", directory);

SearchIndex::create_index(
&writer_client,
directory,
Expand Down
12 changes: 12 additions & 0 deletions pg_search/tests/index_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ fn invalid_create_bm25(mut conn: PgConnection) {
Ok(_) => panic!("should fail with invalid field"),
Err(err) => assert!(err.to_string().contains("not exist"), "{}", fmt_err(err)),
};

match "CALL paradedb.create_bm25(
index_name => 'index_config',
table_name => 'index_config',
key_field => 'id',
numeric_fields => paradedb.field('id')
)"
.execute_result(&mut conn)
{
Ok(_) => panic!("should fail with invalid field"),
Err(err) => assert_eq!(err.to_string(), "error returned from database: key_field id cannot be included in text_fields, numeric_fields, boolean_fields, json_fields, or datetime_fields")
};
}

#[rstest]
Expand Down

0 comments on commit 6b82968

Please sign in to comment.