---
title: "Using MoreLikeThis"
url: "https://docs.ravendb.net/6.2/client-api/session/querying/how-to-use-morelikethis"
version: "6.2"
language: "php"
---

# Using MoreLikeThis

> Client language: PHP. Also available for [C#](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-use-morelikethis.md), [Java](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-use-morelikethis.java.md), [Python](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-use-morelikethis.python.md), [Node.js](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-use-morelikethis.nodejs.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

`moreLikeThis` is available through query extension methods and will return similar documents according to the provided criteria and options.

## Syntax

```php
/**
* Usage:
*   - moreLikeThis(MoreLikeThisBase $moreLikeThis);
*   - moreLikeThis(function(MoreLikeThisBuilder($builder) {...});
*
* @param MoreLikeThisBase|Closure|null $moreLikeThisOrBuilder
* @return DocumentQueryInterface
*/
public function moreLikeThis(null|MoreLikeThisBase|Closure $moreLikeThisOrBuilder): DocumentQueryInterface;
```

| Parameters | | |
| - | - | - |
| **$moreLikeThisOrBuilder** | `null`<br />`MoreLikeThisBase`<br />`Closure` | Defines the type of MoreLikeThis that should be executed |

### Builder

```php
/**
* Usage:
*   - usingDocument();
*   - usingDocument(string $documentJson);
*   - usingDocument(function(MoreLikeThisBuilder $build) {...});
*
* @param string|Closure|null $documentJsonOrBuilder
* @return MoreLikeThisOperationsInterface
*/
function usingDocument(null|string|Closure $documentJsonOrBuilder): MoreLikeThisOperationsInterface;

function usingDocumentWithJson(?string $documentJson): MoreLikeThisOperationsInterface; // same as calling usingDocument(string $documentJson)

function usingDocumentWithBuilder(?Closure $builder): MoreLikeThisOperationsInterface; // same as calling usingDocument(function($builder) {...});

function withOptions(MoreLikeThisOptions $options): MoreLikeThisOperationsInterface;
```

| Parameters | | |
| - | - | - |
| **$documentJsonOrBuilder** | `null`<br />`string`<br />`Closure` | Builder or Inline JSON document to be used as a base for the operation |
| **$documentJson** | ?string | Inline JSON document to be used as a base for the operation |
| **$builder** | `?Closure` | Builder with fluent API that constructs the `MoreLikeThisOperationsInterface` instance |
| **$options** | `MoreLikeThisOptions` | Available operation options (see below) |

### Options

```php
public const DEFAULT_MAXIMUM_NUMBER_OF_TOKENS_PARSED = 5000;
public const DEFAULT_MINIMUM_TERM_FREQUENCY = 2;
public const DEFAULT_MINIMUM_DOCUMENT_FREQUENCY = 5;
public const DEFAULT_MAXIMUM_DOCUMENT_FREQUENCY = PhpClient::INT_MAX_VALUE;
public const DEFAULT_BOOST = false;
public const DEFAULT_BOOST_FACTOR = 1;
public const DEFAULT_MINIMUM_WORD_LENGTH = 0;
public const DEFAULT_MAXIMUM_WORD_LENGTH = 0;
public const DEFAULT_MAXIMUM_QUERY_TERMS = 25;

private ?int $minimumTermFrequency = null;
private ?int $maximumQueryTerms = null;
private ?int $maximumNumberOfTokensParsed = null;
private ?int $minimumWordLength = null;
private ?int $maximumWordLength = null;
private ?int $minimumDocumentFrequency = null;
private ?int $maximumDocumentFrequency = null;
private ?int $maximumDocumentFrequencyPercentage = null;
private ?bool $boost = null;
private ?float $boostFactor = null;
private ?string $stopWordsDocumentId = null;
private ?array $fields = null;
```

| Options | | |
| - | - | - |
| **$minimumTermFrequency** | `?int` | Ignores terms with less than this frequency in the source doc |
| **$maximumQueryTerms** | `?int` | Returns a query with no more than this many terms |
| **$maximumNumberOfTokensParsed** | `?int` | The maximum number of tokens to parse in each example doc field that is not stored with TermVector support |
| **$minimumWordLength** | `?int` | Ignores words less than this length or, if 0, then this has no effect |
| **$maximumWordLength** | `?int` | Ignores words greater than this length or if 0 then this has no effect |
| **$minimumDocumentFrequency** | `?int` | Ignores words which do not occur in at least this many documents |
| **$maximumDocumentFrequency** | `?int` | Ignores words which occur in more than this many documents |
| **$maximumDocumentFrequencyPercentage** | `int?` | Ignores words which occur in more than this percentage of documents |
| **$boost** | `?bool` | Boost terms in query based on score |
| **$boostFactor** | `?float` | Boost factor when boosting based on score |
| **$stopWordsDocumentId** | `?string` | Document ID containing custom stop words |
| **$fields** | `?array` | Fields to compare |

## Example I

### Sync

```php
// Search for similar articles to 'articles/1'
// using 'Articles/MoreLikeThis' index and search only field 'body'
$options = new MoreLikeThisOptions();
$options->setFields([ "body" ]);

/** @var array<Article> $articles */
$articles = $session
  ->query(Article::class, Query::index("Articles/MoreLikeThis"))
  ->moreLikeThis(function($builder) use ($options) {
      $builder
          ->usingDocument(function ($x) {
              $x->whereEquals("id()", "articles/1");
          })
          ->withOptions($options);
  })
  ->toList();
```

### RQL

```sql
from index 'Articles/MoreLikeThis' 
where morelikethis(id() = 'articles/1', '{ "Fields" : [ "Body" ] }')
```

## Example II

### Sync

```php
// Search for similar articles to 'articles/1'
// using 'Articles/MoreLikeThis' index and search only field 'body'
// where article category is 'IT'
$options = new MoreLikeThisOptions();
$options->setFields([ "body" ]);
/** @var array<Article> $articles */
$articles = $session
  ->query(Article::class, Query::index("Articles/MoreLikeThis"))
  ->moreLikeThis(function($builder) use ($options) {
      $builder
          ->usingDocument(function ($x) {
              $x->whereEquals("id()", "articles/1");
          })
          ->withOptions($options);
  })
  ->whereEquals("category", "IT")
  ->toList();
```

### RQL

```sql
from index 'Articles/MoreLikeThis' 
where morelikethis(id() = 'articles/1', '{ "Fields" : [ "Body" ] }') and Category == 'IT'
```

## See also

- [How to Query](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.php.md)
- [MoreLikeThis](https://docs.ravendb.net/6.2/indexes/querying/morelikethis.php.md)
