版本:Unity 6(6000.0)
语言中文(简体)
  • C#

SearchIndexer.AddProperty

建议更改

成功!

感谢帮助我们改进 Unity 文档的质量。尽管我们无法接受所有提交,但我们确实会阅读用户提出的每项建议更改,并在适用的情况下进行更新。

关闭

提交失败

由于某种原因,您建议的更改无法提交。请在几分钟后<a>重试</a>。感谢您花时间帮助我们提高 Unity 文档的质量。

关闭

取消

声明

public void AddProperty(string key, string value, int documentIndex, bool saveKeyword, bool exact);

声明

public void AddProperty(string key, string value, int score, int documentIndex, bool saveKeyword, bool exact);

声明

public void AddProperty(string name, string value, int minVariations, int maxVariations, int score, int documentIndex, bool saveKeyword, bool exact);

参数

key 用于获取值的密钥。
value 要存储在索引中的字符串值。
documentIndex 找到已编制索引的值的文档。
saveKeyword 表示是否将此密钥存储在该索引的关键字注册表中。请参阅 SearchIndexer.GetKeywords.
exact 如果为真,该索引将为此词存储一个完全匹配的条目。
score 该词的相关性得分。
name 用于获取值的密钥。
minVariations 为值计算的最小变化数。不能高于单词的长度。
maxVariations 为值计算的最大变化数。不能高于单词的长度。

说明

向索引添加属性值。属性使用密钥和字符串值指定。该值将存储有多种变化形式。

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}]"))}");
    }
}