# Analyze Entity Co-Occurrence

## User Intent

"How do I find content where multiple entities appear together? Show me entity co-occurrence patterns."

## Operation

**SDK Methods**: `queryContents()` with multiple entity filters\
**GraphQL**: Multi-entity content queries\
**Use Case**: Relationship discovery through co-occurrence

## Prerequisites

* Content with extracted entities
* Multiple entities to analyze
* Understanding of entity relationships

***

## Complete Code Example (TypeScript)

```typescript
import { Graphlit } from 'graphlit-client';
import { ObservableTypes } from 'graphlit-client/dist/generated/graphql-types';

const graphlit = new Graphlit();

// Find entities
const kirk = await graphlit.queryObservables({
  search: "Kirk Marple",
  filter: { types: [ObservableTypes.Person] }
});

const maria = await graphlit.queryObservables({
  search: "Maria Garcia",
  filter: { types: [ObservableTypes.Person] }
});

// Find co-occurrence
const cooccurrence = await graphlit.queryContents({
  
    observations: [
      { observable: { id: kirk.observables.results[0].observable.id } },
      { observable: { id: maria.observables.results[0].observable.id } }
    ]
  });

console.log(`Kirk and Maria mentioned together in ${cooccurrence.contents.results.length} documents`);

// Build co-occurrence matrix
const people = await graphlit.queryObservables({
  filter: { types: [ObservableTypes.Person] }
});

const matrix = new Map<string, Map<string, number>>();

for (let i = 0; i < people.observables.results.length; i++) {
  for (let j = i + 1; j < people.observables.results.length; j++) {
    const personA = people.observables.results[i];
    const personB = people.observables.results[j];
    
    const together = await graphlit.queryContents({
      
        observations: [
          { observable: { id: personA.observable.id } },
          { observable: { id: personB.observable.id } }
        ]
      });
    
    const count = together.contents.results.length;
    if (count > 0) {
      if (!matrix.has(personA.observable.name)) {
        matrix.set(personA.observable.name, new Map());
      }
      matrix.get(personA.observable.name)!.set(personB.observable.name, count);
    }
  }
}

// Display top co-occurrences
console.log('\nTop co-occurrences:');
const flat: Array<{ a: string; b: string; count: number }> = [];
matrix.forEach((bMap, a) => {
  bMap.forEach((count, b) => {
    flat.push({ a, b, count });
  });
});

flat.sort((x, y) => y.count - x.count)
  .slice(0, 10)
  .forEach(({ a, b, count }) => {
    console.log(`  ${a} ↔ ${b}: ${count} times`);
  });
```

***

## Key Patterns

### 1. Pairwise Co-occurrence

Two specific entities:

```typescript
observations: [
  { observable: { id: entityA } },
  { observable: { id: entityB } }
]
```

### 2. Entity + Type

Specific entity with any of type:

```typescript
observations: [
  { observable: { id: personId } },
  { type: ObservableTypes.Organization }
]
```

### 3. Co-occurrence Strength

Frequency indicates relationship strength

***

## Use Cases

**Team Collaboration Analysis**: Who works with whom\
**Influence Mapping**: Entity co-mention networks\
**Topic Association**: Products + Organizations\
**Relationship Discovery**: Find hidden connections

***

## Developer Hints

* Co-occurrence implies relationship
* Frequency = strength
* Useful for network analysis
* Export for graph visualization
* Can be computationally expensive (cache results)

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.graphlit.dev/api-guides/use-cases/knowledge-graph/content-entity-co-occurrence.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
