三、ElasticSearch-新建index实现同义词分词搜索

一、新建index
1.建立index(设置analyzer)

PUT /commodity_v2
{
  "index": {
    "analysis": {
      "analyzer": {
        "by_smart": {
          "type": "custom",
          "tokenizer": "ik_smart",
          "filter": ["by_tfr","by_sfr"],
          "char_filter": ["by_cfr"]
        },
        "by_max_word": {
          "type": "custom",
          "tokenizer": "ik_max_word",
          "filter": ["by_tfr","by_sfr"],
          "char_filter": ["by_cfr"]
        }
      },
      "filter": {
        "by_tfr": {
          "type": "stop",
          "stopwords": [" "]
        },
        "by_sfr": {
          "type": "synonym",
          "synonyms_path": "analysis/synonyms.txt"
        }
      },
      "char_filter": {
        "by_cfr": {
          "type": "mapping",
          "mappings": ["| => |"]
        }
      }
    }
  }
}

2.设置mapping

PUT /commodity_v2/_mapping/search
{
    "properties": {
    "channel": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "commodityId": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "commodityName": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      },
      "index": "analyzed",
      "analyzer": "by_max_word",
      "search_analyzer": "by_smart"
    },
    "commodityType": {
      "type": "long"
    },
    "endTime": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "inventory": {
      "type": "long"
    },
    "keywords": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      },
      "index": "analyzed",
      "analyzer": "by_max_word",
      "search_analyzer": "by_smart"
    },
    "price": {
      "type": "long"
    },
    "publishTime": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "salesVolume": {
      "type": "long"
    },
    "startTime": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "tagArray": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    }
  }
}

3.复制原index内数据到新index

POST _reindex //将commodity内的文档复制到commodity_v2中,默认同样字段名称匹配

{
  "source": {
    "index": "commodity"
  },
  "dest": {
    "index": "commodity_v2"
  }
}

4.查询index内现有mapping

GET /commodity_v2/_mapping?pretty

5.验证是否同义词生效

POST /commodity_v2/_search
{
    "query" : {
      "match_phrase" : {
        "commodityName" : "接近无脂"
      }
    }
}

二、其他查询
1.查询所有index

GET /_cat/indices?v

2.search

a.查询两字段(两字段为or的关系)

GET /commodity_v2/_search
{
   "query": {
        "multi_match": {
            "query":  "空调",
            "fields": [ "commodityName", "keywords" ]
        }
    }
}

b.查询两字段,设置权重

POST /commodity_v2/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "commodityName": {
              "query": "空调",
              "boost": 3
            }
          }
        },
        {
          "match": {
            "keywords": "空调"
          }
        }
      ]
    }
  }
}

c.(A or B)and C

POST /commodity_v2/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "match": {
                  "commodityName": {
                    "query": "空调",
                    "boost": 3
                  }
                }
              },
              {
                "match": {
                  "keywords": "空调"
                }
              }
            ]
          }
        },
        {
          "match_phrase": {
            "tagArray": "TG171018131900000"
          }
        }
      ]
    }
  }
}

3.设置别名

1.移除旧的别名关联,建立新的

POST /_aliases
{
    "actions": [
        { "remove": { "index": "my_index_v1", "alias": "my_index" }},
        { "add":    { "index": "my_index_v2", "alias": "my_index" }}
    ]
}

2.查询别名

GET /*/_alias/my_index  //查某个别名映射的所有index
GET /my_index_v1/_alias/* //查询某个index拥有的别名

你可能感兴趣的:(三、ElasticSearch-新建index实现同义词分词搜索)