Skip to content

Commit

Permalink
Merge pull request #1685 from Danielle9897/RDoc-2485-proximitySearch
Browse files Browse the repository at this point in the history
RDoc-2485 Client API > Session > Querying > Text Search > Proximity search
  • Loading branch information
ppekrol authored Aug 7, 2023
2 parents 0742196 + ba91a9a commit eb29e64
Show file tree
Hide file tree
Showing 4 changed files with 369 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,105 @@
# Proximity Search

To find words within a specific distance away use `Proximity` method.
This method is available only from [DocumentQuery](../../../../client-api/session/querying/document-query/what-is-document-query) level and can only be used right after `Search` method.
---

## Syntax
{NOTE: }

{CODE proximity_1@ClientApi\Session\Querying\TextSearch\ProximitySearch.cs /}
* A __proximity search__ retrieves documents containing search terms that are located within a specified distance from each other.
The distance is measured by the number of intermediate terms.

| Parameters | | |
| ------------- | ------------- | ----- |
| **proximity** | `int` | Number of words within. |
* Proximity search is available only via [DocumentQuery](../../../../client-api/session/querying/document-query/what-is-document-query) or RQL.
* Use the `Proximity` method when making a __full-text search__ using the [Search](../../../../client-api/session/querying/text-search/full-text-search) method.

## Example
* In this page:
* [Why use proximity search](../../../../client-api/session/querying/text-search/proximity-search#why-use-proximity-search)
* [How proximity works](../../../../client-api/session/querying/text-search/proximity-search#how-proximity-works)
* [Proximity search examples](../../../../client-api/session/querying/text-search/proximity-search#proximity-search-examples)
* [Syntax](../../../../client-api/session/querying/text-search/proximity-search#syntax)

{NOTE/}

---

{PANEL: Why use proximity search}

* A basic linguistic assumption is that the proximity of the words implies a relationship between them.

* Proximity search helps match phrases while avoiding scattered or spread-out terms in the text.

* By limiting the search to only include matches where the terms are within the specified maximum proximity,
the search results can be more relevant than those with scattered terms.

{PANEL/}

{PANEL: How proximity works}

* When searching with some specified distance between search terms: `term1` and `term2`:

* Documents that will be returned will contain text where `term1` and `term2` are separated by
the maximum number of terms specified or less.

* The search terms can be separated by fewer terms, but not more than the specified distance.

* Only the terms generated by the [search analyzer](../../../../indexes/using-analyzers#ravendb) are considered towards the count of the maximum distance.
Words or tokens that are Not part of the generated terms are Not included in the proximity calculation.

* Note:

* Search criteria should contain least 2 search terms.

* Search terms must be simple string terms without wildcards.

{PANEL/}

{PANEL: Proximity search examples}

{NOTE: }

__Proximity search (0 distance)__

{CODE-TABS}
{CODE-TAB:csharp:DocumentQuery proximity_1@ClientApi\Session\Querying\TextSearch\ProximitySearch.cs /}
{CODE-TAB:csharp:DocumentQuery_async proximity_2@ClientApi\Session\Querying\TextSearch\ProximitySearch.cs /}
{CODE-TAB-BLOCK:sql:RQL}
from "Employees"
where proximity(search(Notes, "fluent french"), 0)
{CODE-TAB-BLOCK/}
{CODE-TABS/}

{NOTE/}

{NOTE: }

__Proximity search (distance > 0)__

{CODE-TABS}
{CODE-TAB:csharp:Sync proximity_2@ClientApi\Session\Querying\TextSearch\ProximitySearch.cs /}
{CODE-TAB:csharp:Async proximity_3@ClientApi\Session\Querying\TextSearch\ProximitySearch.cs /}
{CODE-TAB:csharp:DocumentQuery proximity_3@ClientApi\Session\Querying\TextSearch\ProximitySearch.cs /}
{CODE-TAB:csharp:DocumentQuery_async proximity_4@ClientApi\Session\Querying\TextSearch\ProximitySearch.cs /}
{CODE-TAB-BLOCK:sql:RQL}
from Foxes
where proximity(search(Name, 'quick fox'), 2)
from "Employees"
where proximity(search(Notes, "fluent french"), 5)
{CODE-TAB-BLOCK/}
{CODE-TABS/}

{NOTE/}

{PANEL/}

{PANEL: Syntax}

{CODE syntax@ClientApi\Session\Querying\TextSearch\ProximitySearch.cs /}

| Parameter | Type | Description |
|---------------|-------|------------------------------------------------------------------------------------------|
| __proximity__ | `int` | The maximum number of terms between the search terms.<br>Can be greater or equal to `0`. |

{PANEL/}

## Related Articles

### Session

- [Query overview](../../../../client-api/session/querying/how-to-query)
- [Full-text search](../../../../client-api/session/querying/text-search/full-text-search)
- [Query overview](../../../../client-api/session/querying/how-to-query)
- [What is a DocumentQuery](../../../../client-api/session/querying/document-query/what-is-document-query)
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Proximity Search

---

{NOTE: }

* A __proximity search__ retrieves documents containing search terms that are located within a specified distance from each other.
The distance is measured by the number of intermediate terms.

* Use the `proximity` method when making a __full-text search__ using the [search](../../../../client-api/session/querying/text-search/full-text-search) method.

* In this page:
* [Why use proximity search](../../../../client-api/session/querying/text-search/proximity-search#why-use-proximity-search)
* [How proximity works](../../../../client-api/session/querying/text-search/proximity-search#how-proximity-works)
* [Proximity search examples](../../../../client-api/session/querying/text-search/proximity-search#proximity-search-examples)
* [Syntax](../../../../client-api/session/querying/text-search/proximity-search#syntax)

{NOTE/}

---

{PANEL: Why use proximity search}

* A basic linguistic assumption is that the proximity of the words implies a relationship between them.

* Proximity search helps match phrases while avoiding scattered or spread-out terms in the text.

* By limiting the search to only include matches where the terms are within the specified maximum proximity,
the search results can be more relevant than those with scattered terms.

{PANEL/}

{PANEL: How proximity works}

* When searching with some specified distance between search terms: `term1` and `term2`:

* Documents that will be returned will contain text where `term1` and `term2` are separated by
the maximum number of terms specified or less.

* The search terms can be separated by fewer terms, but not more than the specified distance.

* Only the terms generated by the [search analyzer](../../../../indexes/using-analyzers#ravendb) are considered towards the count of the maximum distance.
Words or tokens that are Not part of the generated terms are Not included in the proximity calculation.

* Note:

* Search criteria should contain least 2 search terms.

* Search terms must be simple string terms without wildcards.

{PANEL/}

{PANEL: Proximity search examples}

{NOTE: }

__Proximity search (0 distance)__

{CODE-TABS}
{CODE-TAB:nodejs:Query proximity_1@ClientApi\Session\Querying\TextSearch\proximitySearch.js /}
{CODE-TAB-BLOCK:sql:RQL}
from "Employees"
where proximity(search(Notes, "fluent french"), 0)
{CODE-TAB-BLOCK/}
{CODE-TABS/}

{NOTE/}

{NOTE: }

__Proximity search (distance > 0)__

{CODE-TABS}
{CODE-TAB:nodejs:Query proximity_2@ClientApi\Session\Querying\TextSearch\proximitySearch.js /}
{CODE-TAB-BLOCK:sql:RQL}
from "Employees"
where proximity(search(Notes, "fluent french"), 5)
{CODE-TAB-BLOCK/}
{CODE-TABS/}

{NOTE/}

{PANEL/}

{PANEL: Syntax}

{CODE:nodejs syntax@ClientApi\Session\Querying\TextSearch\proximitySearch.js /}

| Parameter | Type | Description |
|---------------|----------|--------------------------------------------------------------------------------------------|
| __proximity__ | `number` | The maximum number of terms between the search terms.<br>Can be greater or equal to `0`. |

{PANEL/}

## Related Articles

### Session

- [Full-text search](../../../../client-api/session/querying/text-search/full-text-search)
- [Query overview](../../../../client-api/session/querying/how-to-query)
- [What is a DocumentQuery](../../../../client-api/session/querying/document-query/what-is-document-query)
Original file line number Diff line number Diff line change
@@ -1,41 +1,130 @@
using Raven.Client.Documents.Session;
using System.Collections.Generic;
using System.Threading.Tasks;
using Raven.Client.Documents;
using Raven.Client.Documents.Session;
using Raven.Documentation.Samples.Orders;

namespace Raven.Documentation.Samples.ClientApi.Session.Querying.TextSearch
{
public class ProximitySearch
{
private interface IFoo<T>
public async Task Examples()
{
#region proximity_1
IDocumentQuery<T> Proximity(int proximity);
#endregion
using (var store = new DocumentStore())
{
using (var session = store.OpenSession())
{
#region proximity_1
List<Employee> employees = session.Advanced
.DocumentQuery<Employee>()
// Make a full-text search with search terms
.Search(x => x.Notes,"fluent french")
// Call 'Proximity' with 0 distance
.Proximity(0)
.ToList();

// Running the above query on the Northwind sample data returns the following Employee documents:
// * employees/2-A
// * employees/5-A
// * employees/9-A

// Each resulting document has the text 'fluent in French' in its 'Notes' field.
//
// The word "in" is not taken into account as it is Not part of the terms list generated
// by the analyzer. (Search is case-insensitive in this case).
//
// Note:
// A document containing text with the search terms appearing with no words in between them
// (e.g. "fluent french") would have also been returned.
#endregion
}

using (var asyncSession = store.OpenAsyncSession())
{
#region proximity_2
List<Employee> employees = await asyncSession.Advanced
.AsyncDocumentQuery<Employee>()
// Make a full-text search with search terms
.Search(x => x.Notes,"fluent french")
// Call 'Proximity' with 0 distance
.Proximity(0)
.ToListAsync();

// Running the above query on the Northwind sample data returns the following Employee documents:
// * employees/2-A
// * employees/5-A
// * employees/9-A

// Each resulting document has the text 'fluent in French' in its 'Notes' field.
//
// The word "in" is not taken into account as it is Not part of the terms list generated
// by the analyzer. (Search is case-insensitive in this case).
//
// Note:
// A document containing text with the search terms appearing with no words in between them
// (e.g. "fluent french") would have also been returned.
#endregion
}

using (var session = store.OpenSession())
{
#region proximity_3
List<Employee> employees = session.Advanced
.DocumentQuery<Employee>()
// Make a full-text search with search terms
.Search(x => x.Notes,"fluent french")
// Call 'Proximity' with distance 5
.Proximity(4)
.ToList();

// Running the above query on the Northwind sample data returns the following Employee documents:
// * employees/2-A
// * employees/5-A
// * employees/6-A
// * employees/9-A

// This time document 'employees/6-A' was added to the previous results since it contains the phrase:
// "fluent in Japanese and can read and write French"
// where the search terms are separated by a count of 4 terms.
//
// "in" & "and" are not taken into account as they are not part of the terms list generated
// by the analyzer.(Search is case-insensitive in this case).
#endregion
}

using (var asyncSession = store.OpenAsyncSession())
{
#region proximity_4
List<Employee> employees = await asyncSession.Advanced
.AsyncDocumentQuery<Employee>()
// Make a full-text search with search terms
.Search(x => x.Notes,"fluent french")
// Call 'Proximity' with distance 5
.Proximity(4)
.ToListAsync();

// Running the above query on the Northwind sample data returns the following Employee documents:
// * employees/2-A
// * employees/5-A
// * employees/6-A
// * employees/9-A

// This time document 'employees/6-A' was added to the previous results since it contains the phrase:
// "fluent in Japanese and can read and write French"
// where the search terms are separated by a count of 4 terms.
//
// "in" & "and" are not taken into account as they are not part of the terms list generated
// by the analyzer.(Search is case-insensitive in this case).
#endregion
}
}
}

public void Sample1(IDocumentSession session)
private interface IFoo
{
#region proximity_2
session
.Advanced
.DocumentQuery<Fox>()
.Search(x => x.Name, "quick fox").Proximity(2)
.ToList();
#endregion
}

public void Sample2(IAsyncDocumentSession session)
{
#region proximity_3
session
.Advanced
.AsyncDocumentQuery<Fox>()
.Search(x => x.Name, "quick fox").Proximity(2)
.ToListAsync();
#region syntax
IDocumentQuery<T> Proximity(int proximity);
#endregion
}

public class Fox
{
public string Name { get; set; }
}
}
}
Loading

0 comments on commit eb29e64

Please sign in to comment.