Elasticsearch 2.x - Return 带状疱疹列表

Elasticsearch 2.x - Return a list of shingles

我有一个字段 "searchtext",我提供了一个子字段 "shingle",并且我使用 shingles 过滤器为该搜索文本字段编制索引。

我需要获取为该字段创建的带状疱疹列表,以便我可以对该字段执行一些操作。当我检索 "searchtext.shingle" 字段时,它只包含原始文本。

这是否意味着我设置的带状疱疹分析器无法正常工作,或者我需要以不同的方式取回带状疱疹列表?

您可以使用 _termvectors endpoint 检索 "shingled" 字段的所有条款,如下所示:

curl -XGET 'http://localhost:9200/your_index/your_type/1/_termvectors?pretty=true' -d '{
  "fields" : ["searchtext.shingle"],
  "offsets" : true,
  "payloads" : true,
  "positions" : true,
  "term_statistics" : true,
  "field_statistics" : true
}'

除了 Val 的答案之外,您还可以使用 Analyze API 测试分析器的工作方式。例如,让我们构建一个自定义分析器,然后测试它为给定输入生成的标记

var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var defaultIndex = "analyzer-test";
var connectionSettings = new ConnectionSettings(pool)
        .DefaultIndex(defaultIndex);

var client = new ElasticClient(connectionSettings);

if (client.IndexExists(defaultIndex).Exists)
    client.DeleteIndex(defaultIndex);

client.CreateIndex(defaultIndex, c => c
    .Settings(s => s
        .Analysis(a => a
            .TokenFilters(tf => tf
                .Shingle("my_shingle", sh => sh
                    .MaxShingleSize(3)
                    .OutputUnigrams()
                )
            )
            .Analyzers(an => an
                .Custom("my_shingle_analyzer", sa => sa
                    .Tokenizer("standard")
                    .Filters("lowercase", "my_shingle")
                )
            )
        )
    )
);

现在测试一下

var analysisResponse = client.Analyze(a => a
    .Index(defaultIndex)
    .Analyzer("my_shingle_analyzer")
    .Text("This is the text I want to analyze")
);

foreach (var token in analysisResponse.Tokens)
{
    Console.WriteLine($"{token.Token}");
}

发出以下令牌

this
this is
this is the
is
is the
is the text
the
the text
the text i
text
text i
text i want
i
i want
i want to
want
want to
want to analyze
to
to analyze
analyze