# JoinSpec

Artifact: [androidx.appsearch:appsearch](https://developer.android.com/jetpack/androidx/releases/appsearch) [View Source](https://cs.android.com/search?q=file:androidx/appsearch/app/JoinSpec.java+class:androidx.appsearch.app.JoinSpec) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appsearch#1.1.0)

*** ** * ** ***


```
public final class JoinSpec
```

<br />

*** ** * ** ***

This class represents the specifications for the joining operation in search.

Joins are only possible for matching on the qualified id of an outer document and a property value within a subquery document. In the subquery documents, these values may be referred to with a property path such as "email.recipient.id" or "entityId" or a property expression. One such property expression is "this.qualifiedId()", which refers to the document's combined package, database, namespace, and id.

Note that in order for perform the join, the property referred to by `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec#getChildPropertyExpression()` has to be a property with `https://developer.android.com/reference/androidx/appsearch/app/AppSearchSchema.StringPropertyConfig#getJoinableValueType()` set to `https://developer.android.com/reference/androidx/appsearch/app/AppSearchSchema.StringPropertyConfig#JOINABLE_VALUE_TYPE_QUALIFIED_ID()`. Otherwise no documents will be joined to any `https://developer.android.com/reference/androidx/appsearch/app/SearchResult`.

Take these outer query and subquery results for example:

```
Outer result {
  id: id1
  score: 5
}
Subquery result 1 {
  id: id2
  score: 2
  entityId: pkg$db/ns#id1
  notes: This is some doc
}
Subquery result 2 {
  id: id3
  score: 3
  entityId: pkg$db/ns#id2
  notes: This is another doc
}
```

In this example, subquery result 1 contains a property "entityId" whose value is "pkg$db/ns#id1", referring to the outer result. If you call `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec.Builder` with "entityId", we will retrieve the value of the property "entityId" from the child document, which is "pkg$db#ns/id1". Let's say the qualified id of the outer result is "pkg$db#ns/id1". This would mean the subquery result 1 document will be matched to that parent document. This is done by adding a `https://developer.android.com/reference/androidx/appsearch/app/SearchResult` containing the child document to the top-level parent `https://developer.android.com/reference/androidx/appsearch/app/SearchResult#getJoinedResults()`.

If `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec#getChildPropertyExpression()` is "notes", we will check the values of the notes property in the subquery results. In subquery result 1, this values is "This is some doc", which does not equal the qualified id of the outer query result. As such, subquery result 1 will not be joined to the outer query result.

It's possible to define an advanced ranking strategy in the nested `https://developer.android.com/reference/androidx/appsearch/app/SearchSpec` and also use `https://developer.android.com/reference/androidx/appsearch/app/SearchSpec#RANKING_STRATEGY_JOIN_AGGREGATE_SCORE()` in the outer `https://developer.android.com/reference/androidx/appsearch/app/SearchSpec`. In this case, the parents will be ranked based on an aggregation, such as the sum, of the signals calculated by scoring the joined documents with the advanced ranking strategy.

In terms of scoring, if `https://developer.android.com/reference/androidx/appsearch/app/SearchSpec#RANKING_STRATEGY_JOIN_AGGREGATE_SCORE()` is set in `https://developer.android.com/reference/androidx/appsearch/app/SearchSpec#getRankingStrategy()`, the scores of the outer SearchResults can be influenced by the ranking signals of the subquery results. For example, if the `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec#getAggregationScoringStrategy()` is set to:

- `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec#AGGREGATION_SCORING_MIN_RANKING_SIGNAL()`, the ranking signal of the outer `https://developer.android.com/reference/androidx/appsearch/app/SearchResult` will be set to the minimum of the ranking signals of the subquery results. In this case, it will be the minimum of 2 and 3, which is 2.
- `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec#AGGREGATION_SCORING_MAX_RANKING_SIGNAL()`, the ranking signal of the outer `https://developer.android.com/reference/androidx/appsearch/app/SearchResult` will be 3.
- `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec#AGGREGATION_SCORING_AVG_RANKING_SIGNAL()`, the ranking signal of the outer `https://developer.android.com/reference/androidx/appsearch/app/SearchResult` will be 2.5.
- `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec#AGGREGATION_SCORING_RESULT_COUNT()`, the ranking signal of the outer `https://developer.android.com/reference/androidx/appsearch/app/SearchResult` will be 2 as there are two joined results.
- `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec#AGGREGATION_SCORING_SUM_RANKING_SIGNAL()`, the ranking signal of the outer `https://developer.android.com/reference/androidx/appsearch/app/SearchResult` will be 5, the sum of 2 and 3.
- `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec#AGGREGATION_SCORING_OUTER_RESULT_RANKING_SIGNAL()`, the ranking signal of the outer `https://developer.android.com/reference/androidx/appsearch/app/SearchResult` will stay as it is.

Referring to "this.childrenRankingSignals()" in the ranking signal of the outer query will return the signals calculated by scoring the joined documents using the scoring strategy in the nested `https://developer.android.com/reference/androidx/appsearch/app/SearchSpec`, as in `https://developer.android.com/reference/androidx/appsearch/app/SearchResult#getRankingSignal()`.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/appsearch/app/JoinSpec.Builder` Builder for `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec`. |

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec#AGGREGATION_SCORING_AVG_RANKING_SIGNAL() = 3` Score the aggregation of joined documents using the average ranking signal. |
| `static final int` | `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec#AGGREGATION_SCORING_MAX_RANKING_SIGNAL() = 4` Score the aggregation of joined documents using the largest ranking signal. |
| `static final int` | `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec#AGGREGATION_SCORING_MIN_RANKING_SIGNAL() = 2` Score the aggregation of joined documents using the smallest ranking signal. |
| `static final int` | `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec#AGGREGATION_SCORING_OUTER_RESULT_RANKING_SIGNAL() = 0` Do not score the aggregation of joined documents. |
| `static final int` | `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec#AGGREGATION_SCORING_RESULT_COUNT() = 1` Score the aggregation of joined documents by counting the number of results. |
| `static final int` | `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec#AGGREGATION_SCORING_SUM_RANKING_SIGNAL() = 5` Score the aggregation of joined documents using the sum of ranking signal. |

| ### Public methods |
|---|---|
| `int` | `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec#getAggregationScoringStrategy()()` Gets the joined document list scoring strategy. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec#getChildPropertyExpression()()` The property expression that is used to get values from child documents, returned from the nested search. |
| `int` | `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec#getMaxJoinedResultCount()()` Returns the max amount of `https://developer.android.com/reference/androidx/appsearch/app/SearchResult` objects to return with the parent document, with a default of 10 SearchResults. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec#getNestedQuery()()` Returns the query to run on the joined documents. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/appsearch/app/SearchSpec` | `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec#getNestedSearchSpec()()` Returns the search spec used to retrieve the joined documents. |

## Constants

### AGGREGATION_SCORING_AVG_RANKING_SIGNAL

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appsearch#1.1.0)

```
public static final int AGGREGATION_SCORING_AVG_RANKING_SIGNAL = 3
```

Score the aggregation of joined documents using the average ranking signal.

### AGGREGATION_SCORING_MAX_RANKING_SIGNAL

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appsearch#1.1.0)

```
public static final int AGGREGATION_SCORING_MAX_RANKING_SIGNAL = 4
```

Score the aggregation of joined documents using the largest ranking signal.

### AGGREGATION_SCORING_MIN_RANKING_SIGNAL

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appsearch#1.1.0)

```
public static final int AGGREGATION_SCORING_MIN_RANKING_SIGNAL = 2
```

Score the aggregation of joined documents using the smallest ranking signal.

### AGGREGATION_SCORING_OUTER_RESULT_RANKING_SIGNAL

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appsearch#1.1.0)

```
public static final int AGGREGATION_SCORING_OUTER_RESULT_RANKING_SIGNAL = 0
```

Do not score the aggregation of joined documents. This is for the case where we want to perform a join, but keep the parent ranking signal.

### AGGREGATION_SCORING_RESULT_COUNT

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appsearch#1.1.0)

```
public static final int AGGREGATION_SCORING_RESULT_COUNT = 1
```

Score the aggregation of joined documents by counting the number of results.

### AGGREGATION_SCORING_SUM_RANKING_SIGNAL

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appsearch#1.1.0)

```
public static final int AGGREGATION_SCORING_SUM_RANKING_SIGNAL = 5
```

Score the aggregation of joined documents using the sum of ranking signal.

## Public methods

### getAggregationScoringStrategy

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appsearch#1.1.0)

```
public int getAggregationScoringStrategy()
```

Gets the joined document list scoring strategy.

The default scoring strategy is `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec#AGGREGATION_SCORING_OUTER_RESULT_RANKING_SIGNAL()`, which specifies that the score of the outer parent document will be used.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/appsearch/app/SearchSpec#RANKING_STRATEGY_JOIN_AGGREGATE_SCORE()` |   |

### getChildPropertyExpression

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appsearch#1.1.0)

```
public @NonNull String getChildPropertyExpression()
```

The property expression that is used to get values from child documents, returned from the nested search. These values are then used to match them to parent documents. These are analogous to foreign keys.

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | the property expression to match in the child documents. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec.Builder` |   |

### getMaxJoinedResultCount

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appsearch#1.1.0)

```
public int getMaxJoinedResultCount()
```

Returns the max amount of `https://developer.android.com/reference/androidx/appsearch/app/SearchResult` objects to return with the parent document, with a default of 10 SearchResults.

### getNestedQuery

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appsearch#1.1.0)

```
public @NonNull String getNestedQuery()
```

Returns the query to run on the joined documents.

### getNestedSearchSpec

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appsearch#1.1.0)

```
public @NonNull SearchSpec getNestedSearchSpec()
```

Returns the search spec used to retrieve the joined documents.

If `https://developer.android.com/reference/androidx/appsearch/app/JoinSpec.Builder#setNestedSearch(java.lang.String,androidx.appsearch.app.SearchSpec)` is never called, this will return a `https://developer.android.com/reference/androidx/appsearch/app/SearchSpec` with all default values. This will match every document, as the nested search query will be "" and no schema will be filtered out.