es创建索引(mapping和setting)

news/2024/7/7 5:47:06 标签: elasticsearch, c#, 数据库

1、首先定义一个索引,如下

PUT /person_news
{
  "settings": {
    "index": {
      "number_of_shards": "3",
      "number_of_replicas": "0",
      "max_result_window": "2000000000"
    }
  },
  "mappings": {
    "properties": {
      "companyName": {
        "type": "text",
	"analyzer": "ik_max_word",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "newsSource": {
        "type": "keyword"
      },
      "newsContent": {
        "type": "text",
	"analyzer": "ik_max_word"
      },
      "newsTitle": {
        "type": "text",
	"analyzer": "ik_max_word",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "labels": {
        "type": "keyword"
      },
      "personInfo": {
        "type": "nested",
        "properties": {
          "personName": {
            "type": "keyword"
          },
          "age": {
            "type": "integer"
          }
        }
      },
      "hotPoint": {
        "type": "long"
      }
    }
  }
}

person_news 这个索引是新闻和人相关的索引,companyName公司名称,定义了text类型,分词器采用的是ik分词,同时定义子字段类型为keyword,表示不分词(可以用来聚合和精准匹配);
newsSource 新闻来源,不分词;
newsContent 新闻内容,分词;
newsTitle 新闻标题,分词,同时建立子字段为keyword类型(同上companyName);
labels 标签,不分词(这里我准备给这个字段存储的是一个数组类型,就是一个新闻有多个标签,详见下文插入文档);
personInfo 新闻中的人物对象信息,采用的是nested结构,是一个数组对象,对象里面有personName和age字段;
hotPoint 新闻的热点值,通常通过此字段给新闻排序;
2、插入数据

PUT person_news/_doc/1
{
  "companyName": "中国恒大有限责任公司",
  "newsSource": "新华社",
  "newsContent": "今日中国证监会对中国恒大董事长许家印罚款4000万,并对其做出终身不能入市的处罚规定,其公司其他高管夏海钧也被做出相应处罚",
  "newsTitle": "恒大许家印被罚",
  "labels": [
    "恒大",
    "许家印"
  ],
  "personInfo": [
    {
      "personName": "许家印",
      "age": 60
    },
    {
      "personName": "夏海钧",
      "age": 59
    }
  ],
  "hotPoint": 1
}
PUT person_news/_doc/2
{
  "companyName": "阿里巴巴有限责任公司",
  "newsSource": "新华社",
  "newsContent": "今日阿里公司集团董事长张勇卸任,由蔡崇信接任",
  "newsTitle": "阿里张勇卸任",
  "labels": [
    "阿里",
    "蔡崇信",
    "张勇"
  ],
  "personInfo": [
    {
      "personName": "张勇",
      "age": 60
    },
    {
      "personName": "蔡崇信",
      "age": 54
    }
  ],
  "hotPoint": 2
}

PUT person_news/_doc/3
{
  "companyName": "中国恒大有限责任公司",
  "newsSource": "路透社",
  "newsContent": "中国恒大董事长传闻跳楼,恒大资产负债高达几万亿,传闻阿里张勇将对恒大进行投资,进军房地产,具体消息恒大高管夏海钧予以否认",
  "newsTitle": "恒大董事长许家印",
  "labels": [
    "恒大",
    "张勇"
  ],
  "personInfo": [
    {
      "personName": "张勇",
      "age": 60
    },
    {
      "personName": "夏海钧",
      "age": 59
    }
  ],
  "hotPoint": 3
}

3、可以通过kibana的DSL语句,查看文本采用某个分词器的效果(采用的是ik_max_word最大粒度分词)

GET /person_news/_analyze
{
  "analyzer": "ik_max_word",
  "text": "中国恒大有限责任公司"
}

结果如下:

{
  "tokens" : [
    {
      "token" : "中国",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "恒",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "CN_CHAR",
      "position" : 1
    },
    {
      "token" : "大有",
      "start_offset" : 3,
      "end_offset" : 5,
      "type" : "CN_WORD",
      "position" : 2
    },
    {
      "token" : "有限责任",
      "start_offset" : 4,
      "end_offset" : 8,
      "type" : "CN_WORD",
      "position" : 3
    },
    {
      "token" : "有限",
      "start_offset" : 4,
      "end_offset" : 6,
      "type" : "CN_WORD",
      "position" : 4
    },
    {
      "token" : "责任",
      "start_offset" : 6,
      "end_offset" : 8,
      "type" : "CN_WORD",
      "position" : 5
    },
    {
      "token" : "公司",
      "start_offset" : 8,
      "end_offset" : 10,
      "type" : "CN_WORD",
      "position" : 6
    }
  ]
}

采用ik_smart智能分词

{
  "tokens" : [
    {
      "token" : "中国",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "恒",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "CN_CHAR",
      "position" : 1
    },
    {
      "token" : "大",
      "start_offset" : 3,
      "end_offset" : 4,
      "type" : "CN_CHAR",
      "position" : 2
    },
    {
      "token" : "有限责任",
      "start_offset" : 4,
      "end_offset" : 8,
      "type" : "CN_WORD",
      "position" : 3
    },
    {
      "token" : "公司",
      "start_offset" : 8,
      "end_offset" : 10,
      "type" : "CN_WORD",
      "position" : 4
    }
  ]
}

使用es自带的默认分词器,分词效果如下(会把每个中文分成一个个的汉字)

GET /person_news/_analyze
{
  "analyzer": "standard",
  "text": "中国恒大有限责任公司"
}
{
  "tokens" : [
    {
      "token" : "中",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "<IDEOGRAPHIC>",
      "position" : 0
    },
    {
      "token" : "国",
      "start_offset" : 1,
      "end_offset" : 2,
      "type" : "<IDEOGRAPHIC>",
      "position" : 1
    },
    {
      "token" : "恒",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "<IDEOGRAPHIC>",
      "position" : 2
    },
    {
      "token" : "大",
      "start_offset" : 3,
      "end_offset" : 4,
      "type" : "<IDEOGRAPHIC>",
      "position" : 3
    },
    {
      "token" : "有",
      "start_offset" : 4,
      "end_offset" : 5,
      "type" : "<IDEOGRAPHIC>",
      "position" : 4
    },
    {
      "token" : "限",
      "start_offset" : 5,
      "end_offset" : 6,
      "type" : "<IDEOGRAPHIC>",
      "position" : 5
    },
    {
      "token" : "责",
      "start_offset" : 6,
      "end_offset" : 7,
      "type" : "<IDEOGRAPHIC>",
      "position" : 6
    },
    {
      "token" : "任",
      "start_offset" : 7,
      "end_offset" : 8,
      "type" : "<IDEOGRAPHIC>",
      "position" : 7
    },
    {
      "token" : "公",
      "start_offset" : 8,
      "end_offset" : 9,
      "type" : "<IDEOGRAPHIC>",
      "position" : 8
    },
    {
      "token" : "司",
      "start_offset" : 9,
      "end_offset" : 10,
      "type" : "<IDEOGRAPHIC>",
      "position" : 9
    }
  ]
}


http://www.niftyadmin.cn/n/5462608.html

相关文章

pytorch-分类-检测-分割的dataset和dataloader创建

1.前言 在PyTorch中&#xff0c;Dataset和DataLoader是两个重要的工具&#xff0c;用于构建输入数据的管道。 &#xff08;1&#xff09;Dataset是一个抽象类&#xff0c;表示数据集&#xff0c;需要实现__len__和__getitem__方法。 &#xff08;2&#xff09;DataLoader是一…

Java线程:活锁

1、什么是活锁 活锁是指&#xff0c;线程没有发生阻塞&#xff0c;但依然执行不下去的情况。 2、活锁的例子 如果两个线程互相改变对方的结束条件&#xff0c;就可能导致双方谁也无法结束。 比如这个程序&#xff1a; public class TestLiveLock {static volatile int cou…

LEETCODE-DAY39

title: LEETCODE-DAY39 date: 2024-03-30 17:24:46 tags: 今日内容&#xff1a;62.不同路径、63. 不同路径 II T1 class Solution:def uniquePaths(self, m: int, n: int) -> int:dp[[0 for _ in range(n1)]for _ in range(m1)]dp[1][1]1for i in range(1,m1):for j in r…

车载电子电器架构 —— 诊断数据库开发

我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 屏蔽力是信息过载时代一个人的特殊竞争力,任何消耗你的人和事,多看一眼都是你的不对。非必要不费力证明自己,无利益不试图说服别人,是精神上的节…

JAVAEE——多线程进阶,锁策略

文章目录 锁策略乐观锁和悲观锁乐观锁悲观锁两者的比较 读写锁重量级锁和轻量级锁重量级锁轻量级锁 自旋锁公平锁和非公平锁公平锁非公平锁 可重入锁和不可重入锁可重入锁不可重入锁 锁策略 乐观锁和悲观锁 乐观锁 什么是乐观锁呢&#xff1f;我们可以认为乐观锁比较自信&am…

使用canvas内置api完成图片的缩放平移和导出和添加提示

最近挺忙的&#xff0c;几乎没有时间去更新博客&#xff0c;今天正好在学习新东西&#xff0c;正好和大家分享一下。 最近要做一个使用canvas完成图片平移&#xff0c;缩放&#xff0c;添加标注的需求&#xff0c;完成的效果大概如下&#xff1a; 使用canvas内置api完成图片的缩…

价值1万元的定制版跑分源码 微信支付宝跑分源码,微信支付宝跑分源码

价值1万元的定制版跑分源码 微信支付宝跑分源码&#xff0c;微信支付宝跑分源码|开代理|自动抢单接单。 此类“跑分”操作究竟是如何运作的呢?以一项“为游戏平台提供微信充值接口”的项目说明来举例:用户成功注册并进一步完善用户信息后&#xff0c;就可以抢单&#xff0c;抢…

6.5物联网RK3399项目开发实录-驱动开发之LCD显示屏使用(wulianjishu666)

90款行业常用传感器单片机程序及资料【stm32,stc89c52,arduino适用】 链接&#xff1a;https://pan.baidu.com/s/1M3u8lcznKuXfN8NRoLYtTA?pwdc53f LCD使用 简介 AIO-3399J开发板外置了两个LCD屏接口&#xff0c;一个是EDP&#xff0c;一个是LVDS&#xff0c;接口对应板…