meilisearch/milli/src/update
meili-bors[bot] d4f10800f2
Merge #3834
3834: Define searchable fields at runtime r=Kerollmops a=ManyTheFish

## Summary
This feature allows the end-user to search in one or multiple attributes using the search parameter `attributesToSearchOn`:

```json
{
  "q": "Captain Marvel",
  "attributesToSearchOn": ["title"]
}
```

This feature act like a filter, forcing Meilisearch to only return the documents containing the requested words in the attributes-to-search-on. Note that, with the matching strategy `last`, Meilisearch will only ensure that the first word is in the attributes-to-search-on, but, the retrieved documents will be ordered taking into account the word contained in the attributes-to-search-on. 

## Trying the prototype

A dedicated docker image has been released for this feature:

#### last prototype version:

```bash
docker pull getmeili/meilisearch:prototype-define-searchable-fields-at-search-time-1
```

#### others prototype versions:

```bash
docker pull getmeili/meilisearch:prototype-define-searchable-fields-at-search-time-0
```

## Technical Detail

The attributes-to-search-on list is given to the search context, then, the search context uses the `fid_word_docids`database using only the allowed field ids instead of the global `word_docids` database. This is the same for the prefix databases.
The database cache is updated with the merged values, meaning that the union of the field-id-database values is only made if the requested key is missing from the cache.

### Relevancy limits

Almost all ranking rules behave as expected when ordering the documents.
Only `proximity` could miss-order documents if all the searched words are in the restricted attribute but a better proximity is found in an ignored attribute in a document that should be ranked lower. I put below a failing test showing it:
```rust
#[actix_rt::test]
async fn proximity_ranking_rule_order() {
    let server = Server::new().await;
    let index = index_with_documents(
        &server,
        &json!([
        {
            "title": "Captain super mega cool. A Marvel story",
            // Perfect distance between words in an ignored attribute
            "desc": "Captain Marvel",
            "id": "1",
        },
        {
            "title": "Captain America from Marvel",
            "desc": "a Shazam ersatz",
            "id": "2",
        }]),
    )
    .await;

    // Document 2 should appear before document 1.
    index
        .search(json!({"q": "Captain Marvel", "attributesToSearchOn": ["title"], "attributesToRetrieve": ["id"]}), |response, code| {
            assert_eq!(code, 200, "{}", response);
            assert_eq!(
                response["hits"],
                json!([
                    {"id": "2"},
                    {"id": "1"},
                ])
            );
        })
        .await;
}
```

Fixing this would force us to create a `fid_word_pair_proximity_docids` and a `fid_word_prefix_pair_proximity_docids` databases which may multiply the keys of `word_pair_proximity_docids` and `word_prefix_pair_proximity_docids` by the number of attributes in the searchable_attributes list. If we think we should fix this test, I'll suggest doing it in another PR.

## Related

Fixes #3772

Co-authored-by: Tamo <tamo@meilisearch.com>
Co-authored-by: ManyTheFish <many@meilisearch.com>
2023-06-28 08:19:23 +00:00
..
facet Use the writemap flag to reduce the memory usage 2023-05-15 10:15:33 +02:00
index_documents Change the TODO message 2023-06-27 12:32:43 +02:00
prefix_word_pairs Avoid a prefix-related worst-case scenario in the proximity criterion 2022-12-22 12:08:00 +01:00
snapshots/delete_documents.rs Tests: rename snapshots 2022-12-19 10:07:17 +01:00
available_documents_ids.rs Fix cargo clippy errors 2022-10-27 01:04:23 +09:00
clear_documents.rs Move back to the hnsw crate 2023-06-27 12:32:39 +02:00
delete_documents.rs Implement an ugly deletion of values in the HNSW 2023-06-27 12:32:39 +02:00
facets.rs Merge remote-tracking branch 'origin/main' into facet-levels-refactor 2022-10-26 15:13:34 +02:00
indexer_config.rs Skip computing index budget in tests 2023-02-23 11:23:39 +01:00
mod.rs Restrict field ids in search context 2023-06-26 14:55:57 +02:00
settings.rs feat: check to see if the PK changed before erroring out 2023-03-26 12:18:39 -04:00
update_step.rs Implement documents format 2021-09-21 16:58:33 +02:00
word_prefix_docids.rs Fixing piles of clippy errors. 2022-10-13 22:02:54 +02:00
words_prefix_integer_docids.rs Fix indexing of word_prefix_fid_docids 2023-04-29 10:56:48 +02:00
words_prefixes_fst.rs Fix clippy errors 2022-11-04 09:27:46 +09:00