key | Key used to retrieve the value. |
value | String value to store in the index. |
documentIndex | Document where the indexed value was found. |
saveKeyword | Indicates if we store this key in the keyword registry of the index. See SearchIndexer.GetKeywords. |
exact | If true, index stores an exact match entry for this word. |
score | Relevance score of the word. |
name | Key used to retrieve the value. |
minVariations | Minimum number of variations to compute for the value. Cannot be higher than the length of the word. |
maxVariations | Maximum number of variations to compute for the value. Cannot be higher than the length of the word. |
Adds a property value to the index. A property is specified with a key and a string value. The value will be stored with multiple variations.
using System.Linq; using UnityEditor; using UnityEditor.Search; using UnityEngine; static class Example_SearchIndexer_AddProperty { [MenuItem("Examples/SearchIndexer/AddProperty")] public static void Run() { var si = new SearchIndexer(); si.Start(); // Add a property with exact:true, meaning that you can either use is: or is= to search for results // These items are given a high score, so they will not be displayed first in the result list. si.AddProperty("is", "broken", score: 20, si.AddDocument("Bocument 1"), exact: true); si.AddProperty("is", "broken", score: 30, si.AddDocument("Bocument 4"), exact: true); // Use exact:false, so color=red won't match any result, just color:red, same for color:yel si.AddProperty("color", "red", si.AddDocument("RGB 55"), exact: false); si.AddProperty("color", "reddish", si.AddDocument("RGB 45"), exact: false); si.AddProperty("color", "yellow", si.AddDocument("RGB 66"), exact: false); // Use this version of AddProperty if you want to minimize how many index variations are computed. // In the example, if you want is:secret to match, but not is:secr si.AddProperty("is", "secret", minVariations: "secret".Length, maxVariations: "secret".Length, score: -99, si.AddDocument("Top Secret"), exact: true); si.Finish(() => { SearchDocuments(si, "Broken documents (Invalid query)", "is=broke", 0); SearchDocuments(si, "Broken documents", "is=broken", 2); SearchDocuments(si, "Color documents", "color=red", 0); SearchDocuments(si, "Color documents", "color:red", 2); SearchDocuments(si, "Color documents", "color:yel", 1); SearchDocuments(si, "Top documents", "is:secr", 0); SearchDocuments(si, "Top documents", "is:secret", 1); SearchDocuments(si, "Top documents", "is=secret", 1); }); } private static void SearchDocuments(SearchIndexer si, string label, string query, int expectedCount) { var results = si.Search(query).ToList(); Debug.Assert(results.Count == expectedCount, $"Invalid {label} with {query}, expected {expectedCount} results but got {results.Count}"); if (results.Count > 0) Debug.Log($"{label} ({query}): {string.Join(", ", results.Select(r => $"{r.id} [{r.score}]"))}"); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.