elasticsearch添加文档时如何引用多个字段
How to reference multi fields when adding a document to elasticsearch
我在我的 Elasticsearch 服务器中创建了一个索引,我正在使用 .Net 客户端 NEST 连接到它。一些索引属性有多个字段,我只想填写正确的字段。
我为此映射创建了 class 'document'。但我不知道如何访问 属性.
的字段
这是我的映射(总结):
"mappings": {
"document": {
"properties": {
"baseUniqueID": {
"type": "keyword"
},
"description": {
"type": "text",
"fields": {
"en": {
"type": "text",
"analyzer": "english"
},
"fa": {
"type": "text",
"analyzer": "nofapersian"
},
"fr": {
"type": "text",
"analyzer": "french"
}
}
},
"documentDate": {
"type": "date"
},
"documentType_Id": {
"type": "keyword"
},
"id": {
"type": "long"
}
}
}
}
和文档 class:
public class Document : BaseInt32KeyEntity
{
public string BaseUniqueID{ get; set; }
public int? Weight { get; set; }
public DateTime DocumentDate { get; set; }
public string Description { get; set; }
public int DocumentType_Id { get; set; }
}
}
如何创建一个 Document 对象来填充我想要的字段(在此示例中 description.en),然后使用 IndexDocument 将其添加到 Elasticsearch?像这样:
Document doc = new Document();
doc.Description.en = "This is some description";
ElasticClient.IndexDocument(doc);
您可以使用更新 API
更新单个字段
var client = new ElasticClient();
var documentId = 1;
var partial = new
{
Description = "This is some description"
};
var updateResponse = client.Update<Document, object>(documentId, u => u
.Index("your_index")
.Doc(partial)
);
仅当您尚未为 Document
类型设置索引约定时才需要 .Index()
。要更新的文档是用部分文档建模的,因为使用 Document
会导致发送值类型的默认值,例如 DocumentDate
和 DocumentType_Id
属性。
doc.Description.en = "This is some description";
这是不可能的,因为这不是 multi-fields 的工作方式。使用 multi-fields,可以以多种不同的方式分析单个文档字段输入以满足不同的搜索需求。在您的示例中,Description
属性 值将以 4 种不同的方式进行分析:
- 通过标准分析器与基础
text
映射
- 由带有
.en
multi-field 映射的英语分析器
- 由带有
.fa
multi-field 映射的 nofapersian 分析器
- 由法语分析器使用
.fr
multi-field 映射
分析的结果会被索引到倒排索引中,让你可以对其进行搜索和查询,但是发送到Elasticsearch的原始JSON文档只会包含一个"description"
字段,这是当您检索文档的 _source
时您将得到的结果(如果 _source
已存储,默认情况下它是)。
如果您想将它们建模为文档中的单独字段,您可以引入具有必要属性的 Description
类型
public class Description
{
public string Standard { get;set; }
public string English { get;set; }
public string NoFaPersian{ get;set; }
public string French{ get;set; }
}
然后将其索引为object
类型映射,为每个
配置分析器
public class Document
{
public string BaseUniqueID { get; set; }
public int? Weight { get; set; }
public DateTime DocumentDate { get; set; }
public Description Description { get; set; }
public int DocumentType_Id { get; set; }
}
var indexResponse = client.CreateIndex("your_index", c => c
.Mappings(m => m
.Map<Document>(mm => mm
.AutoMap()
.Properties(p => p
.Object<Description>(o => o
.Name(n => n.Description)
.AutoMap()
.Properties(pp => pp
.Text(t => t.Name(n => n.Standard).Analyzer("standard"))
.Text(t => t.Name(n => n.English).Analyzer("english"))
.Text(t => t.Name(n => n.NoFaPersian).Analyzer("nofapersian"))
.Text(t => t.Name(n => n.French).Analyzer("french"))
)
)
)
)
)
);
产生以下创建索引请求
PUT http://localhost:9200/your_index?pretty=true
{
"mappings": {
"document": {
"properties": {
"baseUniqueID": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"weight": {
"type": "integer"
},
"documentDate": {
"type": "date"
},
"description": {
"type": "object",
"properties": {
"standard": {
"type": "text",
"analyzer": "standard"
},
"english": {
"type": "text",
"analyzer": "english"
},
"noFaPersian": {
"type": "text",
"analyzer": "nofapersian"
},
"french": {
"type": "text",
"analyzer": "french"
}
}
},
"documentType_Id": {
"type": "integer"
}
}
}
}
}
我在我的 Elasticsearch 服务器中创建了一个索引,我正在使用 .Net 客户端 NEST 连接到它。一些索引属性有多个字段,我只想填写正确的字段。
我为此映射创建了 class 'document'。但我不知道如何访问 属性.
的字段这是我的映射(总结):
"mappings": {
"document": {
"properties": {
"baseUniqueID": {
"type": "keyword"
},
"description": {
"type": "text",
"fields": {
"en": {
"type": "text",
"analyzer": "english"
},
"fa": {
"type": "text",
"analyzer": "nofapersian"
},
"fr": {
"type": "text",
"analyzer": "french"
}
}
},
"documentDate": {
"type": "date"
},
"documentType_Id": {
"type": "keyword"
},
"id": {
"type": "long"
}
}
}
}
和文档 class:
public class Document : BaseInt32KeyEntity
{
public string BaseUniqueID{ get; set; }
public int? Weight { get; set; }
public DateTime DocumentDate { get; set; }
public string Description { get; set; }
public int DocumentType_Id { get; set; }
}
}
如何创建一个 Document 对象来填充我想要的字段(在此示例中 description.en),然后使用 IndexDocument 将其添加到 Elasticsearch?像这样:
Document doc = new Document();
doc.Description.en = "This is some description";
ElasticClient.IndexDocument(doc);
您可以使用更新 API
更新单个字段var client = new ElasticClient();
var documentId = 1;
var partial = new
{
Description = "This is some description"
};
var updateResponse = client.Update<Document, object>(documentId, u => u
.Index("your_index")
.Doc(partial)
);
仅当您尚未为 Document
类型设置索引约定时才需要 .Index()
。要更新的文档是用部分文档建模的,因为使用 Document
会导致发送值类型的默认值,例如 DocumentDate
和 DocumentType_Id
属性。
doc.Description.en = "This is some description";
这是不可能的,因为这不是 multi-fields 的工作方式。使用 multi-fields,可以以多种不同的方式分析单个文档字段输入以满足不同的搜索需求。在您的示例中,Description
属性 值将以 4 种不同的方式进行分析:
- 通过标准分析器与基础
text
映射 - 由带有
.en
multi-field 映射的英语分析器 - 由带有
.fa
multi-field 映射的 nofapersian 分析器 - 由法语分析器使用
.fr
multi-field 映射
分析的结果会被索引到倒排索引中,让你可以对其进行搜索和查询,但是发送到Elasticsearch的原始JSON文档只会包含一个"description"
字段,这是当您检索文档的 _source
时您将得到的结果(如果 _source
已存储,默认情况下它是)。
如果您想将它们建模为文档中的单独字段,您可以引入具有必要属性的 Description
类型
public class Description
{
public string Standard { get;set; }
public string English { get;set; }
public string NoFaPersian{ get;set; }
public string French{ get;set; }
}
然后将其索引为object
类型映射,为每个
public class Document
{
public string BaseUniqueID { get; set; }
public int? Weight { get; set; }
public DateTime DocumentDate { get; set; }
public Description Description { get; set; }
public int DocumentType_Id { get; set; }
}
var indexResponse = client.CreateIndex("your_index", c => c
.Mappings(m => m
.Map<Document>(mm => mm
.AutoMap()
.Properties(p => p
.Object<Description>(o => o
.Name(n => n.Description)
.AutoMap()
.Properties(pp => pp
.Text(t => t.Name(n => n.Standard).Analyzer("standard"))
.Text(t => t.Name(n => n.English).Analyzer("english"))
.Text(t => t.Name(n => n.NoFaPersian).Analyzer("nofapersian"))
.Text(t => t.Name(n => n.French).Analyzer("french"))
)
)
)
)
)
);
产生以下创建索引请求
PUT http://localhost:9200/your_index?pretty=true
{
"mappings": {
"document": {
"properties": {
"baseUniqueID": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"weight": {
"type": "integer"
},
"documentDate": {
"type": "date"
},
"description": {
"type": "object",
"properties": {
"standard": {
"type": "text",
"analyzer": "standard"
},
"english": {
"type": "text",
"analyzer": "english"
},
"noFaPersian": {
"type": "text",
"analyzer": "nofapersian"
},
"french": {
"type": "text",
"analyzer": "french"
}
}
},
"documentType_Id": {
"type": "integer"
}
}
}
}
}