3

This is the first time I'm working with RavenDB. I am not in a position to ask the creators of the code why they made certain decisions, so I am turning to internet strangers and hoping for some kindness.

There are a number of search queries throughout the code and they all look like:

query.AndAlso().Search("Content", $"*searchExpression*");

The query is a RavenDB IDocumentQuery. The property Content is not defined on the document that is being searched for.

I think that this performs a search on all the properties in the document. Unfortunately I cannot find any documentation on this, so I am not sure how this impacts performance, security and injection attacks. I'm not even sure if this is actively discouraged.

Can somebody shine some light on this? Even if it's just the direction of my google searches as "ravendb search on content" does not return a lot of useful articles.

Edit: To clarify, I understand the syntax of the Search method. It takes the property to search and then an expression that is being searched for. But I have a document

public class Person {
  public string FirstName {get; set;}
  public string LastName {get; set;}
  public DateTime DateOfBirth {get; set;}
}

The Search remains the same as above. There is no "Content" property in this Person document. Yet it does search on something, because I can see the list of documents get filtered. I just don't know what it's filtering on.

Ken Bonny
  • 729
  • 1
  • 9
  • 29

1 Answers1

3

The first parameter to the .Search method is the property selector.
An expression marking the document field in which the terms should be looked for.

See method definition:

https://github.com/ravendb/ravendb/blob/904c34a939c35c6375726a57e6eb475086355fa7/src/Raven.Client/Documents/Session/IDocumentQueryBase.cs#L187

You can see a usage example in these tests:

https://github.com/ravendb/ravendb/blob/904c34a939c35c6375726a57e6eb475086355fa7/test/SlowTests/Issues/RavenDB_5669.cs#L17

https://github.com/ravendb/ravendb/blob/904c34a939c35c6375726a57e6eb475086355fa7/test/SlowTests/Issues/RavenDB_5669.cs#L45

Danielle
  • 3,324
  • 2
  • 18
  • 31
  • That part is clear. The problem is that the document does not have a "Content" property, so what is being searched? – Ken Bonny Dec 08 '20 at 07:55
  • so if you don't have a ```Content``` field in your document, then why are you searching on this field? – Danielle Dec 08 '20 at 10:20
  • I didn't write the code. I inherited it. The last developer left and gave me a 2 week high level intro into the system. It did not include why somebody wrote this. I don't understand how it's filtering anything. Sometimes it works, sometimes it doesn't, sometimes it gives way too many results, sometimes way too little. I'm kind of hoping somebody can explain what the magic "Content" looks at, because it does do something. I just don't know what exactly. – Ken Bonny Dec 08 '20 at 10:36
  • Also, this isn't used once, this is used throughout the application in almost all search boxes. This baffles me and I don't even know if it's a good practice or if I should put it on the list to refactor. – Ken Bonny Dec 08 '20 at 10:55
  • 1
    @KenBonny, The `Content` is probably defined in the index that is used by the query. How does the index look like? – Grisha Kotler Dec 08 '20 at 19:18
  • @GrishaKotler That is where it is... Content is an array of objects populated with all the properties that can be searched. Why would somebody do that instead of writing a comprehensive search function. I'm going out on a limb here and guess that this is not really best practice. – Ken Bonny Dec 09 '20 at 12:17
  • @GrishaKotler If you add an answer, I'll give you credit for finding it. I'm really grateful to Danielle for helping, but you found the solution. – Ken Bonny Dec 09 '20 at 12:22