繁体   English   中英

将文档添加到Elasticsearch时如何引用多个字段

[英]How to reference multi fields when adding a document to elasticsearch

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

您可以使用Update 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 =“这是一些描述”;

无法执行此操作,因为这不是多字段的工作方式。 使用多字段,可以以多种不同方式分析单个文档字段输入,以满足不同的搜索需求。 在您的示例中,将通过4种不同的方式来分析Description属性值:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM