博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
4_ElaticSearch 使用terms搜索多个值
阅读量:3727 次
发布时间:2019-05-22

本文共 2968 字,大约阅读时间需要 9 分钟。

4_ElaticSearch 使用terms搜索多个值

更多干货

概述

  • es 中如何实现 sql 中的in,使用terms实现

  • 语法如下:

term: {
"field": "value"}terms: {
"field": ["value1", "value2"]}
  • sql中的in
select * from tbl where col in ("value1", "value2")

例子一:

1、为帖子数据增加tag字段

  • 为文章添加tag标签 其中id = i tag 为 java和hadoop.
  • id= 2 tag为 java
  • id= 3的 tag 为 hadoop
  • id=4 的 tag 为 java elasticsearch
POST /forum/article/_bulk{ "update": { "_id": "1"} }{ "doc" : {
"tag" : ["java", "hadoop"]} }{ "update": { "_id": "2"} }{ "doc" : {
"tag" : ["java"]} }{ "update": { "_id": "3"} }{ "doc" : {
"tag" : ["hadoop"]} }{ "update": { "_id": "4"} }{ "doc" : {
"tag" : ["java", "elasticsearch"]} }

2、搜索articleID为KDKE-B-9947-#kL5或QQPX-R-3956-#aD8的帖子

GET /forum/article/_search {  "query": {    "constant_score": {      "filter": {        "terms": {          "articleID": [            "KDKE-B-9947-#kL5",            "QQPX-R-3956-#aD8"          ]        }      }    }  }}

搜索tag中包含java的帖子

GET /forum/article/_search{    "query" : {        "constant_score" : {            "filter" : {                "terms" : {                     "tag" : ["java"]                }            }        }    }}

查询返回结果

"took": 2,  "timed_out": false,  "_shards": {    "total": 5,    "successful": 5,    "failed": 0  },  "hits": {    "total": 3,    "max_score": 1,    "hits": [      {        "_index": "forum",        "_type": "article",        "_id": "2",        "_score": 1,        "_source": {          "articleID": "KDKE-B-9947-#kL5",          "userID": 1,          "hidden": false,          "postDate": "2017-01-02",          "tag": [            "java"          ]        }      },      {        "_index": "forum",        "_type": "article",        "_id": "4",        "_score": 1,        "_source": {          "articleID": "QQPX-R-3956-#aD8",          "userID": 2,          "hidden": true,          "postDate": "2017-01-02",          "tag": [            "java",            "elasticsearch"          ]        }      },      {        "_index": "forum",        "_type": "article",        "_id": "1",        "_score": 1,        "_source": {          "articleID": "XHDK-A-1293-#fJ3",          "userID": 1,          "hidden": false,          "postDate": "2017-01-01",          "tag": [            "java",            "hadoop"          ]        }      }    ]  }}

例子3

  • 搜索结果仅仅搜索tag只包含java的帖子
  • 添加tag_cnt 字段 表示doc 中 tag的数量。
POST /forum/article/_bulk{ "update": { "_id": "1"} }{ "doc" : {
"tag_cnt" : 2} }{ "update": { "_id": "2"} }{ "doc" : {
"tag_cnt" : 1} }{ "update": { "_id": "3"} }{ "doc" : {
"tag_cnt" : 1} }{ "update": { "_id": "4"} }{ "doc" : {
"tag_cnt" : 2} }

查询条件:

GET /forum/article/_search{  "query": {    "constant_score": {      "filter": {        "bool": {          "must": [            {              "term": {                "tag_cnt": 1              }            },            {              "terms": {                "tag": ["java"]              }            }          ]        }      }    }  }}

4、知识点

  • 1、terms多值搜索
  • 2、优化terms多值搜索的结果
  • 3、相当于SQL中的in语句

相关文章

转载地址:http://weonn.baihongyu.com/

你可能感兴趣的文章
《设计模式》
查看>>
单例设计模式
查看>>
面试题集锦(一)
查看>>
Calendar类方法——编写万年历的两种方式
查看>>
File类的使用——遍历所有文件及子文件以及遍历删除
查看>>
内存流的使用——基本使用
查看>>
RandomAccessFile 类的使用——基本使用
查看>>
Properties实现类——基本使用
查看>>
结构型模式——装饰者设计模式
查看>>
线程的同步——Synchronized和ReentrantLock
查看>>
网络编程基础
查看>>
python实现快速排序
查看>>
python实现归并排序
查看>>
二叉树的镜像实现(python版)
查看>>
ptqt5控件了解(三)
查看>>
自学C++(一)
查看>>
51单片机介绍(二)
查看>>
STM32F103 入门篇-5-初识STM32
查看>>
后台框架的frameset
查看>>
Spring Jdbc
查看>>