droplink and treelist values in sitecore search

2019-08-05 05:06发布

How to get droplink and treelist values in sitecore search . Below are my code and config file . But when i am searching based on droplink and treelist value its not coming in search result .

var fquery = new FullTextQuery(search); SearchHits searchHits = sc.Search(fquery, int.MaxValue); return searchHits.FetchResults(0, int.MaxValue).Select(r => r.GetObject()).ToList();

config file entry .

I am not sure if i have to parse them or something else . Looking forward for help.

标签: sitecore
1条回答
Anthone
2楼-- · 2019-08-05 05:26

You don't say which version of Sitecore you're using, but speaking as someone who works with v6.6:

ID-based fields, like TreeList store GUIDs in the Sitecore database. At index time, Sitecore parses these into ShortID format and forces it to lower case. So the Lucene index entry actually contains a lowercase GUID with no curly braces or hyphens.

Chances are, your text-based query is not going to contain text that will match this.

I tend to use a Term based BooleanQuery object to match ID-based fields. Something like:

BooleanQuery query = new BooleanQuery();
query.Add(new TermQuery(new Term("myfieldname", ShortID.Encode(myGuidToMatch).ToLowerInvariant())), BooleanClause.Occur.MUST);

Note that the field name you want to query should be in lower case, as Sitecore / Lucene generally works in lower case.

You may find the code and UI example in this blog post helpful to see an example of building queries against ID-based fields: http://jermdavis.wordpress.com/2014/06/09/faceted-search-in-sitecore-6-6/

If you want to be able to match the values contained in these fields from a free text type of search box, then you will have to pre-process the values from these ID-based fields before they are indexed.

Sitecore and Lucene allow for the idea of "computed fields" in your index - basically you can configure the indexing process to run bits of your own code in order to process data at index time, and to create new Lucene index fields from the results of your code.

This blog post of mine gives an example of a computed field: http://jermdavis.wordpress.com/2014/05/05/using-dms-profile-cards-as-search-metadata/ That example is not doing what you want - but it does talk about how you might configure one of these custom fields.

You'd probably want your custom field code to:

  • Get the raw value of the ID-based field
  • Load the item that this ID points to
  • Process that item to turn it into the pattern of text you want to be indexed
  • Return this text, to be saved into the computed field in Lucene

With that done, you should find that your index will contain the text associated with your ID field(s). And hence you should be able to match it with a text-based query.

-- EDITED TO ADD --

More detail on creating computed index items:

Sitecore 6.6 doesn't directly support computed fields in your Lucene indexes. To get them you can make use of the Advanced Database Crawler - which is part of the Sitecore SearchContrib project available on GitHub: https://github.com/sitecorian/SitecoreSearchContrib There are assorted blog posts about on getting started with this code.

[NB: In Sitecore 7.x I believe this behaviour has migrated into the core of Sitecore. However I think they changed the names of stuff. Details of that are available via google - things like Upgrading sitecore 6.6 index configuration to sitecore 7 (using ComputedFields) for example]

The code for a dynamic field to turn something ID-based into text might look like:

public class IndexIDField : BaseDynamicField
{
    public override string ResolveValue(Sitecore.Data.Items.Item item)
    {
        Field fld = item.Fields["FieldYouAreInterestedIn"];

        if (fld != null && !string.IsNullOrWhiteSpace(fld.Value))
        {
            string[] ids = fld.Value.Split('|');
            StringBuilder text = new StringBuilder();

            foreach (string id in ids)
            {
                Item relatedItem = item.Database.GetItem(id);
                if (relatedItem != null)
                {
                    text.Append(relatedItem.DisplayName);
                    text.Append(" ");
                }
            }

            return text.ToString();
        }

        return null;
    }
}

This is extracting the appropriate field from the context item that's being passed in. If it exists and is not empty, it splits it by "|" to get a list of all the IDs stored in this field. Then for each one it tries to load it. Note use of the appropriate database specified by the input item - Sitecore.Context.Database will point to Core at this point, and you won't find your items there. Finally, if we get back a valid item from the ID, we append its display name to our text to be indexed. You could use other fields than Display Name - depending on what makes sense in your solution.

With that code added to your solution you need to ensure it's called at index build time. The default config for the Advanced Database Crawler includes a config element for dynamic fields. (And again, SC7.x will have something similar but I don't know the names off the top of my head) You need to add your type to the configuration for dynamic fields. Snipping out all the extraneous bits from the default config:

<configuration xmlns:x="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <!-- snip -->
    <search>
      <!-- snip -->
      <crawlers>
        <demo type="scSearchContrib.Crawler.Crawlers.AdvancedDatabaseCrawler,scSearchContrib.Crawler">
          <!-- snip -->

          <dynamicFields hint="raw:AddDynamicFields">
            <!-- snip -->

            <dynamicField type="YourNamespace.IndexIDField,YourDLL" name="_myfieldname" storageType="NO" indexType="TOKENIZED" vectorType="NO" boost="1f" />

          </dynamicFields>

          <!-- snip -->
        </demo>
      </crawlers>
    </search>
  </sitecore>
</configuration>

That sets up a new field called "_myfieldname" with sensible options for indexing text.

Rebuild your search index, and you should find your free text queries will match the appropriate items. Testing this basic setup on an instance of SC6.6, I get hits with some test data I happened to have lying around. If I search my computed column for "blue" I get only rows which were tagged with a metadata item that had "blue" in its name:

What I see in Index Viewer

查看更多
登录 后发表回答