1
votes

I've added a search index to a Sitecore 6.6 website. To test I've created two items which match the index params i.e. use the single template being indexed.

This template has a DropLink field called "Story Data Item" and what I want to do is return all items where that field's value matches a supplied Guid.

The following code returns all indexed documents and when I run it I get both items returned which demonstrates they are indexed. Furthermore, the first item meets this condition: items.First().Fields["Story Data Item"].Value == "{3F810A26-5D57-49CA-8D16-8BE4FDA404E2}"

Index indx = SearchManager.GetIndex("hos");
IEnumerable<Item> items = new List<Item>();
Sitecore.Search.SearchResultCollection results;
using (IndexSearchContext searchContext = indx.CreateSearchContext())
{
    SearchHits hits = searchContext.Search(new Lucene.Net.Search.MatchAllDocsQuery(), int.MaxValue);
    results = hits.FetchResults(0, hits.Length);
}
items = results.Select(result => result.GetObject<Item>()).Where(item => item != null).ToArray();

If I now change the query to the following, which as far as I understand it should search the relevant field for that same Guid, I get no results. This I don't understand. I've followed all advice about ensuring the case is lowered and the Guid is converted to ShortID. Can anyone tell me what I'm doing wrong?

Index indx = SearchManager.GetIndex("hos");
IEnumerable<Item> items = new List<Item>();
Sitecore.Search.SearchResultCollection results;
using (IndexSearchContext searchContext = indx.CreateSearchContext())
{
    FieldQuery fldQuery = new FieldQuery("Story Data Item".ToLowerInvariant(), ShortID.Encode(new Guid("{3F810A26-5D57-49CA-8D16-8BE4FDA404E2}")).ToLowerInvariant());
    SearchHits hits = searchContext.Search(fldQuery, int.MaxValue);
    results = hits.FetchResults(0, hits.Length);
}
items = results.Select(result => result.GetObject<Item>()).Where(item => item != null).ToArray();

The index is configured as follows:

      <index id="hos" type="Sitecore.Search.Index, Sitecore.Kernel">
        <param desc="name">$(id)</param>
        <param desc="folder">__hos</param>
        <Analyzer ref="search/analyzer" />
        <locations hint="list:AddCrawler">
          <master type="Sitecore.Search.Crawlers.DatabaseCrawler, Sitecore.Kernel">
            <Database>master</Database>
            <Tags>master hos</Tags>
            <Root>/sitecore/content/Home/my_root</Root>
            <include hint="list:IncludeTemplate">
              <story>{AB3023C3-331D-4DD0-86B2-F8F48E1287EC}</story>
            </include>
            <Boost>2.0</Boost>
          </master>
          <master type="Sitecore.Search.Crawlers.DatabaseCrawler, Sitecore.Kernel">
            <Database>web</Database>
            <Tags>web hos</Tags>
            <Root>/sitecore/content/Home/my_root</Root>
            <include hint="list:IncludeTemplate">
              <story>{AB3023C3-331D-4DD0-86B2-F8F48E1287EC}</story>
            </include>
            <Boost>2.0</Boost>
          </master>
        </locations>
      </index>

I have also followed the advice here - droplink and treelist values in sitecore search - regarding using BooleanQuery but no difference.

Also, Luke confirms the index holds the data I need:

enter image description here

All help greatly appreciated.

1

1 Answers

3
votes

Just change FieldQuery to TermQuery and your code will work:

Index indx = SearchManager.GetIndex("hos");
IEnumerable<Item> items = new List<Item>();
Sitecore.Search.SearchResultCollection results;
using (IndexSearchContext searchContext = indx.CreateSearchContext())
{
    TermQuery termQuery = new TermQuery(new Term("story data item", "{3F810A26-5D57-49CA-8D16-8BE4FDA404E2}".ToLower()));
    SearchHits hits = searchContext.Search(termQuery, int.MaxValue);
    results = hits.FetchResults(0, hits.Length);
}
items = results.Select(result => result.GetObject<Item>()).Where(item => item != null).ToArray();