三、ElasticSearch-新建index實(shí)現(xiàn)同義詞分詞搜索

一、新建index
1.建立index(設(shè)置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.設(shè)置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.復(fù)制原index內(nèi)數(shù)據(jù)到新index

POST _reindex //將commodity內(nèi)的文檔復(fù)制到commodity_v2中,默認(rèn)同樣字段名稱匹配

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

4.查詢index內(nèi)現(xiàn)有mapping

GET /commodity_v2/_mapping?pretty

5.驗(yàn)證是否同義詞生效

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

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

GET /_cat/indices?v

2.search

a.查詢兩字段(兩字段為or的關(guān)系)

GET /commodity_v2/_search
{
   "query": {
        "multi_match": {
            "query":  "空調(diào)",
            "fields": [ "commodityName", "keywords" ]
        }
    }
}

b.查詢兩字段,設(shè)置權(quán)重

POST /commodity_v2/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "commodityName": {
              "query": "空調(diào)",
              "boost": 3
            }
          }
        },
        {
          "match": {
            "keywords": "空調(diào)"
          }
        }
      ]
    }
  }
}

c.(A or B)and C

POST /commodity_v2/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "match": {
                  "commodityName": {
                    "query": "空調(diào)",
                    "boost": 3
                  }
                }
              },
              {
                "match": {
                  "keywords": "空調(diào)"
                }
              }
            ]
          }
        },
        {
          "match_phrase": {
            "tagArray": "TG171018131900000"
          }
        }
      ]
    }
  }
}

3.設(shè)置別名

1.移除舊的別名關(guān)聯(lián),建立新的

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

2.查詢別名

GET /*/_alias/my_index  //查某個(gè)別名映射的所有index
GET /my_index_v1/_alias/* //查詢某個(gè)index擁有的別名
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容