Elasticsearch NEST突出显示附件内容

时间:2017-10-31 08:55:10

标签: elasticsearch nest elasticsearch-plugin elasticsearch-net

在Elasticsearch 5.6.3中,我安装了 ingest-attachment 插件。我正在尝试在附件的内容中将术语向量属性设置为 WithPositionOffsets 我应该在哪里设置此属性以在我的搜索中看到高亮度的结果?

文件POCO如下:

public class Document
{
    public int Id { get; set; }
    public string Path { get; set; }
    public string Content { get; set; }
    public Attachment Attachment { get; set; }
}

这是带映射的CreateIndex函数。

var indexResponse = client.CreateIndex(documentsIndex, c => c
                                      .Settings(s => s
                                        .Analysis(a => a
                                          .Analyzers(ad => ad
                                            .Custom("windows_path_hierarchy_analyzer", ca => ca
                                              .Tokenizer("windows_path_hierarchy_tokenizer")
                                            )
                                          )
                                          .Tokenizers(t => t
                                            .PathHierarchy("windows_path_hierarchy_tokenizer", ph => ph
                                              .Delimiter('\\')
                                            )
                                          )
                                        )
                                      )
                                      .Mappings(m => m
                                        .Map<Document>(mp => mp
                                          .AutoMap()
                                          .AllField(all => all
                                            .Enabled(false)
                                          )
                                          .Properties(ps => ps
                                            .Text(s => s
                                              .Name(n => n.Path)
                                              .Analyzer("windows_path_hierarchy_analyzer")
                                            )
                                            .Attachment(at => at.
                                            Name(n => n.Attachment.Content)
                                            .FileField(ff => ff
                                                .Name("Content")
                                                .TermVector(TermVectorOption.WithPositionsOffsets)
                                                .Store()))
                                            //.Text(s => s
                                            //  .Name(n => n.Attachment.Content)
                                            //  .TermVector(TermVectorOption.WithPositionsOffsets)
                                            //  .Store(true)
                                            //)
                                            .Object<Attachment>(a => a
                                              .Name(n => n.Attachment)
                                              .AutoMap()
                                            )
                                          )
                                        )
                                      )
                                    );

进入.Mappings部分,我使用FileField来设置termvector和store属性。但结果如下:

{
"documents": {
    "mappings": {
        "document": {
            "_all": {
                "enabled": false
            },
            "properties": {
                "attachment": {
                    "properties": {
                        "author": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "content": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "content_length": {
                            "type": "long"
                        },
                        "content_type": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "date": {
                            "type": "date"
                        },
                        "detect_language": {
                            "type": "boolean"
                        },
                        "indexed_chars": {
                            "type": "long"
                        },
                        "keywords": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "language": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "name": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "title": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        }
                    }
                },
                "content": {
                    "type": "attachment",
                    "fields": {
                        "content": {
                            "type": "text",
                            "store": true,
                            "term_vector": "with_positions_offsets"
                        },
                        "author": {
                            "type": "text"
                        },
                        "title": {
                            "type": "text"
                        },
                        "name": {
                            "type": "text"
                        },
                        "date": {
                            "type": "date"
                        },
                        "keywords": {
                            "type": "text"
                        },
                        "content_type": {
                            "type": "text"
                        },
                        "content_length": {
                            "type": "integer"
                        },
                        "language": {
                            "type": "text"
                        }
                    }
                },
                "id": {
                    "type": "integer"
                },
                "path": {
                    "type": "text",
                    "analyzer": "windows_path_hierarchy_analyzer"
                }
            }
        }
    }
}

}

所以我无法在查询结果中看到突出显示属性。我该怎么做?

1 个答案:

答案 0 :(得分:1)

要使用ingest-attachment插件,请不要将Attachment属性映射为attachment数据类型;此映射适用于mapper-attachment插件,该插件在5.x中已弃用并在6.x中删除。

I wrote a blog post about using ingest-attachment with .NET,其中包含Attachment的示例映射;实质上,将其映射为object数据类型,并将Content属性映射为text数据类型并应用了.TermVector(TermVectorOption.WithPositionsOffsets)。在索引带有附件的任何文档之前,您需要在索引中创建此映射。