elasticsearch创建multi-fields字段及修改非multi-fields字段为multi-fields字段及multi-field字段的不同的analyzer进行分析和搜索

本文使用的elasticsearch是5.2.1,官方的multi-fields例子参见 https://www.elastic.co/guide/en/elasticsearch/reference/5.3/multi-fields.html

创建一个index及type和mapping 及setting 和analyzer和search_analyzer,注意此时已经是multi-fields了,如下:

PUT /test/
{
    "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        },
        "my_analyzer2": {
          "type":      "pattern",
          "pattern":   "\\W+|_", 
          "lowercase": true
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "pattern",
          "pattern": "\\w+|\"(.+?)\"",
          "group":0
        }
      }
    }
  },
  "mappings": {
    "tech": {
      "properties": {
        "body": {
          "type": "text",
          "analyzer":"standard",
          "search_analyzer": "standard",
          "fields": {
          "syh": {
            "type": "text",
            "analyzer":"my_analyzer",
            "search_analyzer": "my_analyzer"
          },
          "xhx": {
            "type": "text",
            "analyzer":"my_analyzer2",
            "search_analyzer": "my_analyzer2"
          }
        }
      }
    }
  }
}
}

修改刚才创建的type的mapping为multi-fields

PUT /test/tech/_mapping
{
  "tech": {
    "properties": {
      "body": {
        "type": "text",
        "analyzer":"standard",
        "search_analyzer": "standard",
        "fields": {
          "syh": {
            "type": "text",
            "analyzer":"my_analyzer",
            "search_analyzer": "my_analyzer"
          },
          "xhx": {
            "type": "text",
            "analyzer":"my_analyzer2",
            "search_analyzer": "my_analyzer2"
          }
        }
      }
    }
  }
}

修改刚才创建的索引的setting,本例是修改analyzer,注意修改setting必须先关闭索引,然后再打开索引

POST  /test/_close
PUT /test/_settings
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        },
        "my_analyzer2": {
          "type":      "pattern",
          "pattern":   "\\W+|_", 
          "lowercase": true
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "pattern",
          "pattern": "\\w+|\"(.+?)\"",
          "group":0
        }
      }
    }
  }
}
POST  /test/_open

针对刚才索引test的类型tech的不同的字段做analyzer,同样的文档其分词效果是不一样的。

查看body.syh字段的带双引号分词效果

POST /test/_analyze
{
  "field": "body.syh",
  "text": "description \"this device in beijing office \" \"what are you going to do\" email_address"
}

查看body.xhx字段的带下划线的分词效果

POST /test/_analyze
{
  "field": "body.xhx",
  "text": "description \"this device in beijing office \" \"what are you going to do\" email_address"
}

查看body字段的standard的分词效果

POST /test/_analyze
{
  "field": "body",
  "text": "description \"this device in beijing office \" \"what are you going to do\" email_address"
}

使用bulk插入一条数据

POST _bulk/?refresh=true
{ "index" : { "_index" : "test", "_type" : "tech" } }
{ "body": "description \"this device in beijing office\" \"what are you going to do\" email_address"}

查询body字段的standard搜索

POST /test/_search?pretty
{
  "query": {
    "term": {
      "body": "description"
    }
  }
}

查询body.xhx字段的my_analyzer2搜索

POST /test/_search?pretty
{
  "query": {
    "term": {
      "body.syh": "\"this device in beijing office\""
    }
  }
}

查询body.xhx字段的my_analyzer搜索

POST /test/_search?pretty
{
  "query": {
    "term": {
      "body.xhx": "address"
    }
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值