【问题标题】:How to reference multi fields when adding a document to elasticsearch向elasticsearch添加文档时如何引用多个字段
【发布时间】:2019-06-14 11:50:17
【问题描述】:

我在我的 Elasticsearch 服务器中创建了一个索引,并使用 .Net 客户端 NEST 连接到它。一些索引属性有多个字段,我只想填写正确的字段。

我为此映射创建了“文档”类。但我不知道如何访问属性字段。

这是我的映射(总结):

"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"
          }

        }
      }
    }

和文档类:

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);

【问题讨论】:

  • 我明白你的意思。一旦我能对你的问题有一个好的答案,我会在这里发布。我删除了我之前的答案。

标签: c# elasticsearch nest


【解决方案1】:

您可以使用更新 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 会导致发送值类型的默认值,例如 DocumentDateDocumentType_Id 属性。

doc.Description.en = "这是一些描述";

这是不可能的,因为这不是multi-fields 的工作方式。借助多字段,可以以多种不同方式分析单个文档字段输入,以满足不同的搜索需求。在您的示例中,Description 属性值将通过 4 种不同方式进行分析:

  1. 由标准分析器与基 text 映射
  2. 由带有.en 多字段映射的英语分析器
  3. 由 nofapersian 分析器使用.fa 多字段映射
  4. 由带有.fr 多字段映射的法国分析仪

分析的结果将被索引到倒排索引中,以便您对其进行搜索和查询,但是发送到 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"
        }
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    相关资源
    最近更新 更多