Searching non-primitive types within a struct

260 views Asked by At

I need to search within a an array nested in another array. Let's say I have the following document

schema foos {

    document foos {

        struct foo {
            field bars type array<string> {}
        }

        field baz type string {
            indexing: summary
        }

        field foos type array<foo> {
            indexing: summary
            struct-field bars { indexing: attribute } // breaks due to non-primitive typing
        }
    }
}

I need to be able to search within the bars field or at least access that field within a rank-profile whilst also being able to search based on baz. My first thought at a solution would be to structure it as follows:

schema foos {

    document foos {

        field id type string {
            indexing: summary
        }

        field baz type string {
            indexing: summary | attribute
        }

        field foos type array<reference<foo>> {
            indexing: summary
        }
    }
}
schema foo {
    document foo {

        field foos_ref type reference<foos> {
            indexing: attribute
        }

        field bars type array<string> {
            indexing: summary | index
        }
    }
    
    import field foos_ref.baz as baz {}
}

This would allow me to search within the foo cluster then get the corresponding foos reference, but the overall goal is to provide the user a list of foos documents which would require multiple searches from the list of returned foo documents resulting in slow searches overall.

If there is a recommended way to handle situations like these, any help would be appreciated. Thank you.

1

There are 1 answers

0
geirst On BEST ANSWER

First, note that a reference field used for parent child relationships can only be single value and not an array (https://docs.vespa.ai/documentation/reference/schema-reference.html#type:reference). The reference field is specified in the child type to reference the parent. The schemas can be defined as follows, where foos is the parent type and foo is the child type:

schema foos {
  document foos {
    field id type string {
      indexing: summary | attribute
    }
    field baz type string {
      indexing: summary | attribute
    }
  }
}

schema foo {
  document foo {
    field foos_ref type reference<foos> {
      indexing: attribute
    }
    field bars type array<string> {
      indexing: summary | index
    }
  }
  import field foos_ref.baz as foos_baz {}
  import field foos_ref.id as foos_id {}
}

Now you can search for foo documents using the fields bars and foos_baz in the query. Use grouping (https://docs.vespa.ai/documentation/grouping.html) on the foos_id field to structure the result around the foos documents instead. This is handled in a single query request.