社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Elasticsearch

如何在elasticsearch中添加文档时引用多个字段

mehdi delmaghani • 5 年前 • 866 次点击  

我创建了这个mappaing的类“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"
          }

        }
      }
    }

文档类:

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

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/56366
 
866 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Russ Cam
Reply   •   1 楼
Russ Cam    6 年前

可以使用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)
);

这个 .Index() Document 键入。要更新的文档是用部分文档建模的,因为使用 将导致为值类型发送默认值,如 DocumentDate DocumentType_Id

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

不可能这样做,因为这不是 multi-fields 工作。对于多个字段,可以通过多种不同的方式分析单个文档字段输入,以满足不同的搜索需求。在你的例子中, Description 财产价值将通过4种不同的方式进行分析:

  1. 用标准分析仪 text 映射
  2. 由英语分析器和 .en 多场映射
  3. 通过Nofa波斯语分析器 .fa 多场映射
  4. .fr 多场映射

"description" 字段,当您检索 _source 对于文档(如果 存储,默认情况下为)。

如果要将这些字段建模为文档上的单独字段,可以引入 具有必需属性的类型

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