Elasticsearch 移除 type 之后的新姿势

本文详细介绍了Elasticsearch 7.0版本中type概念的移除过程,包括新的索引操作、mapping操作变化,以及搜索、父子关系等关键功能的调整方式。同时,文中提供了具体的API调用示例,帮助读者理解如何适应这一重大变更。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

随着 7.0 版本的即将发布,type 的移除也是越来越近了,在 6.0 的时候,已经默认只能支持一个索引一个 type 了,7.0 版本新增了一个参数 include_type_name ,即让所有的 API 是 type 相关的,这个参数在 7.0 默认是 true,不过在 8.0 的时候,会默认改成 false,也就是不包含 type 信息了,这个是 type 用于移除的一个开关。

让我们看看最新的使用姿势吧,当 include_type_name 参数设置成 false 后:

  • 索引操作:PUT {index}/{type}/{id}需要修改成PUT {index}/_doc/{id}
  • Mapping 操作:PUT {index}/{type}/_mapping 则变成 PUT {index}/_mapping
  • 所有增删改查搜索操作返回结果里面的关键字 _type 都将被移除
  • 父子关系使用 join 字段来构建
  • #创建索引
    PUT twitter
    {
      "mappings": {
        "_doc": {
          "properties": {
            "type": { "type": "keyword" }, 
            "name": { "type": "text" },
            "user_name": { "type": "keyword" },
            "email": { "type": "keyword" },
            "content": { "type": "text" },
            "tweeted_at": { "type": "date" }
          }
        }
      }
    }
    
    #修改索引
    PUT twitter/_doc/user-kimchy
    {
      "type": "user", 
      "name": "Shay Banon",
      "user_name": "kimchy",
      "email": "shay@kimchy.com"
    }
    
    #搜索
    GET twitter/_search
    {
      "query": {
        "bool": {
          "must": {
            "match": {
              "user_name": "kimchy"
            }
          },
          "filter": {
            "match": {
              "type": "tweet" 
            }
          }
        }
      }
    }
    
    #重建索引
    POST _reindex
    {
      "source": {
        "index": "twitter"
      },
      "dest": {
        "index": "new_twitter"
      }
    }

    原文链接

<think>我们正在处理一个关于在Vue3中处理移动端点击事件的问题。由于移动端设备有触摸屏,我们需要考虑触摸事件(如`touchstart`、`touchend`)与鼠标事件(如`click`)的差异。在移动端,`click`事件会有大约300ms的延迟(为了区分单击和双击),这可能会影响用户体验。因此,我们通常需要处理触摸事件来获得更快的响应,或者使用一些库来消除延迟。 在Vue3中,我们可以使用事件修饰符来简化事件处理。但是,对于移动端,我们可能需要处理特定的触摸事件。 常见的做法是: 1. 使用`@click`,但注意延迟问题。如果对延迟不敏感,可以直接使用。 2. 使用`@touchstart`或`@touchend`来替代`@click`,但要注意触摸事件会阻止鼠标事件的触发(除非我们同时监听两者)。 3. 使用第三方库如`fastclick`(但可能已过时)或者使用现代浏览器已经支持的`touch-action: manipulation`(通过CSS)来消除300ms延迟。 在Vue3中,我们可以这样处理: ### 1. 直接使用@click并解决延迟问题 现代浏览器(iOS 10+,Android Chrome等)在设置了`<meta name="viewport" content="width=device-width">`后已经移除了300ms延迟。所以确保你的HTML中有这个meta标签。 另外,可以通过CSS属性`touch-action: manipulation`来消除延迟,例如: ```css a, button, .touch-element { touch-action: manipulation; } ``` ### 2. 使用@touchstart或@touchend ```vue <template> <button @touchstart="handleTouchStart">触摸我</button> </template> <script> export default { methods: { handleTouchStart() { // 处理触摸开始事件 } } } </script> ``` 但是,注意触摸事件和鼠标事件的区别:触摸事件会立即触发,而不会等待用户抬起手指(touchend)。此外,如果我们需要同时支持桌面和移动端,可能需要同时监听`@click`和`@touchstart`,但这样会导致事件被触发两次。为了避免这种情况,我们可以使用一个自定义处理函数来统一处理。 ### 3. 使用自定义指令处理点击事件(支持移动端和桌面端) 我们可以创建一个自定义指令,在移动端使用`touchstart`,在桌面端使用`click`,或者同时监听但避免重复触发。 以下是一个简单的自定义指令示例,它会在触摸开始(移动端)或点击(桌面端)时触发: ```vue <template> <button v-click>点击或触摸</button> </template> <script> export default { directives: { click: { mounted(el, binding) { // 判断是否为移动设备 const isMobile = 'ontouchstart' in window; const eventType = isMobile ? 'touchstart' : 'click'; el.addEventListener(eventType, binding.value); }, beforeUnmount(el, binding) { const isMobile = 'ontouchstart' in window; const eventType = isMobile ? 'touchstart' : 'click'; el.removeEventListener(eventType, binding.value); } } }, methods: { handleClick() { console.log('点击或触摸事件'); } } } </script> ``` 但是,上面的指令只绑定了一个事件(移动端用`touchstart`,桌面端用`click`)。然而,有些设备可能同时支持触摸和鼠标(如平板),为了避免重复触发,我们可能需要更复杂的逻辑。 ### 4. 使用第三方库 有一些Vue库可以帮助处理移动端事件,例如`v-touch`(Vue2的,Vue3可能不兼容)或者使用`@vueuse/gesture`(基于VueUse的姿势库)。 如果使用`@vueuse/gesture`,可以这样: 首先安装: ```bash npm install @vueuse/gesture ``` 然后在组件中使用: ```vue <template> <button v-bind="bind()">点击或触摸</button> </template> <script> import { useGesture } from '@vueuse/gesture' export default { setup() { const bind = useGesture({ onClick: (state) => { console.log('点击事件', state) }, onTap: (state) => { console.log('轻触事件(移动端)', state) }, }) return { bind } } } </script> ``` ### 5. 注意点 - 在移动端,使用`touchstart`事件可能会干扰滚动行为,因此需要谨慎使用。 - 如果使用触摸事件,注意事件对象(`event`)中的`touches`和`changedTouches`属性,它们包含触摸点的信息。 ### 总结 在Vue3中处理移动端点击事件,我们可以: - 确保视口设置正确,使用CSS的`touch-action: manipulation`来消除`click`事件的延迟。 - 直接使用`@click`,如果延迟不是问题。 - 使用`@touchstart`或`@touchend`来处理触摸事件,但要注意与桌面端的兼容性。 - 创建自定义指令来统一处理点击和触摸事件。 - 使用第三方库如`@vueuse/gesture`来处理更复杂的手势。 请根据实际需求选择合适的方法。 [^1]: Tooltips / popovers v-tooltip - Easy tooltips with Vue 2.x.vue-popper-component - Popper.js directive for Vue.js.vue-directive-tooltip - Simple, flexible tooltip directive (based on Popper.js) vue-popperjs - VueJS 2.x popover component based popper.jsvue-tooltipster - VueJS 2.x tooltip component based tooltipster.js. Support html content, hover and hover+click events. Overlay [^2]: angular: webview "pause", "resume", and "resize" events now trigger change detection (#18853) (544e550), closes #18831 [^3]: Mobile jQuery also gives developers the ability to render basic content (as built) on basic devices. And the more sophisticated platforms and browsers will be increasingly enriched using additional, externally linked JavaScript and CSS. §§相关问题§§ 1. 在Vue3中,如何消除移动端点击事件的300ms延迟? 2. Vue3中如何同时处理移动端和桌面端的点击事件? 3. 在Vue3中,如何使用第三方库(如`@vueuse/gesture`)来处理手势事件?</think>在 Vue3 框架中处理移动端点击事件时,需要考虑移动设备的特性(如触摸操作、手势识别、延迟处理等)。以下是完整的解决方案: ### 1. 基础点击事件处理 Vue3 使用 `v-on` 指令(或简写 `@`)处理点击事件: ```vue <template> <!-- 基础点击事件 --> <button @click="handleClick">点击我</button> <!-- 移动端推荐使用 touch 事件 --> <div @touchstart="handleTouchStart" @touchend="handleTouchEnd"> 触摸区域 </div> </template> <script setup> const handleClick = () => { console.log('点击事件触发'); }; const handleTouchStart = (event) => { console.log('触摸开始', event.touches[0].clientX); }; const handleTouchEnd = (event) => { console.log('触摸结束', event.changedTouches[0].clientX); }; </script> ``` ### 2. 解决 300ms 点击延迟问题 移动浏览器默认有 300ms 延迟(用于区分单击和双击)。解决方案: **方法1:CSS touch-action** ```css /* 全局样式 */ .clickable { touch-action: manipulation; /* 消除点击延迟 */ } ``` **方法2:使用 FastClick 库** ```bash npm install fastclick ``` ```js // main.js import FastClick from 'fastclick'; FastClick.attach(document.body); ``` ### 3. 手势事件处理 使用第三方库处理复杂手势(推荐 [Hammer.js](https://hammerjs.github.io/)): ```bash npm install @hammerjs/hammer ``` ```vue <template> <div ref="swipeArea">滑动区域</div> </template> <script setup> import { ref, onMounted } from 'vue'; import Hammer from '@hammerjs/hammer'; const swipeArea = ref(null); onMounted(() => { const hammer = new Hammer(swipeArea.value); hammer.on('swipe', (e) => { console.log(`滑动方向: ${e.direction === 2 ? '左' : '右'}`); }); hammer.on('tap', () => { console.log('轻触事件'); }); }); </script> ``` ### 4. 移动端专属事件处理 ```vue <template> <div @click.self="preventBubble" @touchmove.passive="handleScroll" @click.middle="middleClick"> 复合事件区域 </div> </template> <script setup> // 阻止事件冒泡 const preventBubble = (e) => { e.stopPropagation(); }; // 被动事件优化滚动性能 const handleScroll = () => { /* 滚动处理 */ }; // 中键点击(兼容桌面端) const middleClick = () => { console.log('中键点击'); }; </script> ``` ### 5. 响应式事件处理 根据设备类型切换事件监听: ```vue <script setup> import { ref, onMounted, onBeforeUnmount } from 'vue'; const isMobile = ref(/Android|webOS|iPhone|iPad/i.test(navigator.userAgent)); let eventType = isMobile.value ? 'touchstart' : 'click'; const handleInteraction = () => { console.log(isMobile.value ? '触摸事件' : '点击事件'); }; onMounted(() => { window.addEventListener(eventType, handleInteraction); }); onBeforeUnmount(() => { window.removeEventListener(eventType, handleInteraction); }); </script> ``` ### 最佳实践建议 1. **优先使用 touch 事件**:移动端优先使用 `@touchstart` 和 `@touchend` 替代 `@click` 2. **事件委托优化性能**:对大列表使用事件委托 ```vue <div @click="handleItemClick"> <div v-for="item in items" :data-id="item.id">{{ item.name }}</div> </div> ``` 3. **使用 passive 事件**:对 `touchmove` 添加 `.passive` 修饰符提升滚动性能 4. **防抖处理**:避免快速点击导致多次触发 ```js import { debounce } from 'lodash-es'; const debouncedClick = debounce(() => { /* ... */ }, 300); ``` ### 兼容性处理 ```vue <script setup> const handleUniversalClick = (e) => { // 统一处理鼠标/触摸事件 const clientX = e.clientX || (e.touches && e.touches[0].clientX); console.log('交互位置:', clientX); }; </script> ``` 通过以上方法,可以高效处理 Vue3 在移动端的点击事件,同时解决延迟、手势识别等移动端特有问题[^1][^2]。 [^1]: Tooltips / popovers v-tooltip - Easy tooltips with Vue 2.x.vue-popper-component - Popper.js directive for Vue.js.vue-directive-tooltip - Simple, flexible tooltip directive (based on Popper.js) vue-popperjs - VueJS 2.x popover component based popper.jsvue-tooltipster - VueJS 2.x tooltip component based tooltipster.js. Support html content, hover and hover+click events. Overlay [^2]: angular: webview "pause", "resume", and "resize" events now trigger change detection (#18853) (544e550), closes #18831
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值