6

I want to extract Json property from Raven DB database. I am extracting it from Ravendb studio from index as below. In below query, parameter Body is of type json and I want to extract its one field (let's say field1) from it. How can it be done?

from index 'Selectdata' as message 
order by message.ProcessedAt desc 
select {
    UniqueMessageId: message.UniqueMessageId,
    MessageId: message.MessageMetadata.MessageId,
    MessageType: message.Headers["EnclosedMessageTypes"],
    TimeSent: message.Headers["TimeSent"],
    ProcessingStarted: message.Headers["ProcessingStarted"],
    ProcessingEnded: message.Headers["ProcessingEnded"],
    ProcessingEndpoint: message.Headers["ProcessingEndpoint"],
    ProcessedAt : message.ProcessedAt,
    Body:message.MessageMetadata.Body //json body
}
Rajaram Shelar
  • 7,537
  • 24
  • 66
  • 107
  • Why don't you index that field (field1) in a static index, so that you can query on it ? – Danielle Jul 27 '20 at 07:03
  • @Danielle - How can I make index on property of document parameter (Body)? – Rajaram Shelar Jul 27 '20 at 12:03
  • Can you extract this field (the one you are interested in) from the json body and place it as a property in your entity class? If yes, then you can index it. – Danielle Jul 27 '20 at 16:09
  • 1
    @Danielle - No, as I am not able to parse to Json body. – Rajaram Shelar Jul 28 '20 at 05:03
  • 1
    Try using ```Search()``` which will perform full-text search on the 'Body' field property. See https://ravendb.net/docs/article-page/5.0/Csharp/client-api/session/querying/how-to-use-search. Note: First enable the full-text-search capability on the 'Body' field in the index definition – Danielle Jul 28 '20 at 07:58

1 Answers1

3

Use Full-Text-Search on the 'Body' field.
See code examples in the RavenDB Demo:

Full Text Search with Static Index - Single Field
https://demo.ravendb.net/demos/csharp/text-search/fts-with-static-index-single-field

Full Text Search with Static Index - Multiple Fields
https://demo.ravendb.net/demos/csharp/text-search/fts-with-static-index-multiple-fields

And also can use the Search() method on 'Body' field property. See https://ravendb.net/docs/article-page/5.0/Csharp/client-api/session/querying/how-to-use-search

Danielle
  • 3,324
  • 2
  • 18
  • 31
  • 1
    Can we query on two indexes with some joining condition? I have to select field in json Body as well as other properties of other index. – Rajaram Shelar Jul 30 '20 at 07:19
  • 1
    Maybe the way to go for that is to define one index of type: **'Multi-Map-Index'**. You define one index that indexes data from more than one collection. So you can index a property from one collection and from another collection in one index, and then of course you can query this index. See: https://ravendb.net/docs/article-page/5.0/csharp/studio/database/indexes/create-multi-map-index and https://ravendb.net/docs/article-page/5.0/csharp/indexes/multi-map-indexes – Danielle Jul 30 '20 at 18:22