Querying Blocks and internal content of MediaData (Pdf files)

Vote:
 

Hey Guys,

I need some help with an EPiServer FIND query.

I have some media content types (PdfFile, DocFile etc...) That are being indexed by FIND.  I can see these in the index so that is working as expected.  The media files are referenced on blocks, which are also added to the index.  When querying FIND I only want to return content results of my block type, so PdfFile will never be part of the results.  Since a PdfFile can be referenced on the block, I want FIND to be able to return search results (blocks) where the query matches the title of the block OR the internal content of the PdfFile where that file is set as the contentReference on the block.

i.e.

[AllowedTypes(typeof(PdfFile), typeof(DocFile))]
[Display(
    Name = "Media",
    Description = "Media",
    GroupName = SystemTabNames.Content,
    Order = 50)]
public virtual ContentReference Media { get; set; }

      
EXAMPLE:
So say I have 2 blocks (BlockA and BlockB) both with different PdfFiles (PdfA and PdfB) set as the content reference.  

BlockA - References PdfA
BlockB - References PdfB

PdfA - internal content contains the word "Dog"
PdfB - internal content contains the word "Cat"

If I search for the word "Dog" only BlockA should be returned in the content result from FIND since PdfA is set as the content reference on BlockA and PdfA contains the word "Dog".

I have this working, but I don't think it's the best implementation as I'm querying FIND twice.

First I query FIND for all PdfFiles and create a list of the contentId's.  I then use this list of contentId's as an "Include" to another FIND query.

// Get an instance of the search client
var client = SearchClient.Instance;

var pdfContentIds = new List();

// Get a list of PDF's content id's that match the search text
var pdfContentResult = client.Search().For(searchText).GetContentResult();
pdfContentIds = pdfContentResult.Items.ToList().Select(item => item.ContentLink.ID).ToList();

var query = client.Search();
query = query.For(searchText).InFields(x => x.Title);

// Include blocks that reference any of the pdf content ids in the content result
if (pdfContentIds.Any())
{
	query = query.Include(x => x.Media.ID.In(pdfContentIds));
}

return query.GetContentResult();

Does anyone know if there is a better way to implemented this?
Could some sort of nested query work?

Thanks, Mark

#176964
Mar 31, 2017 4:53
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.