博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ES 自己做的例子汇总
阅读量:4107 次
发布时间:2019-05-25

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

 

 

首先初始化数据

POST /forums/articles/_bulk 

{ "index": { "_id": 1 }}
{ "articleID" : "XHDK-A-1293-#fJ3", "userID" : 1, "hidden": false, "postDate": "2017-01-01" } 
{ "index": { "_id": 2 }}
{ "articleID" : "KDKE-B-9947-#kL5", "userID" : 1, "hidden": false, "postDate": "2017-01-02" } 
{ "index": { "_id": 3 }}
{ "articleID" : "JODL-X-1937-#pV7", "userID" : 2, "hidden": false, "postDate": "2017-01-01" } 
{ "index": { "_id": 4 }}
{ "articleID" : "QQPX-R-3956-#aD8", "userID" : 2, "hidden": true, "postDate": "2017-01-02" } 

POST /forums/articles/_bulk

{ "index": { "_id": 5 }}
{ "articleID" : "XHDK-C-1293-#fJ3", "userID" : 2, "hidden": false, "postDate": "2017-02-01" }
{ "index": { "_id": 6 }}
{ "articleID" : "KDKE-D-9947-#kL5", "userID" : 3, "hidden": false, "postDate": "2017-02-02" }
{ "index": { "_id": 7 }}
{ "articleID" : "JODL-E-1937-#pV7", "userID" : 4, "hidden": false, "postDate": "2017-02-01" }
{ "index": { "_id": 8 }}
{ "articleID" : "QQPX-F-3956-#aD8", "userID" : 5, "hidden": true, "postDate": "2017-03-02" }

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"]} }{ "update": { "_id": "5"} }{ "doc" : {"tag" : ["java", "hadoop"]} }{ "update": { "_id": "6"} }{ "doc" : {"tag" : ["java","mysql"]} }{ "update": { "_id": "7"} }{ "doc" : {"tag" : ["hadoop"]} }{ "update": { "_id": "8"} }{ "doc" : {"tag" : ["java", "elasticsearch","mysql"]} }

第一个简单的查询 查询postDate 在2017-01-01的帖子

GET /forums/articles/_search{  "query": {    "constant_score": {      "filter": {          "bool": {            "must": [              { "term": {                "postDate": "2017-01-01"              } }            ]          }      },      "boost": 1.2    }  }}

第二个查询,查询查询postDate 在2017-01-01的帖子或者是用户userid=2发的帖子

POST /forums/articles/_search

{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "should": [
            {
              "term": {
                "postDate": "2017-01-01"
              }
            },
            {
              "term": {
                "userID": 2
              }
            }
          ]
        }
      }
    }
  }
}

第三个查询,查询用户userid=2 在2017-01-01和2017-01-01发的帖子

POST /forums/articles/_search{  "query": {    "constant_score": {      "filter": {        "bool": {          "must": {            "term": {              "userID": 2            }          },          "should": [            {              "term": {                "postDate": "2017-01-01"              }            },            {              "term": {                "postDate": "2017-01-02"              }            }          ]        }      }    }  }}

第4个需求  查询tag中包含java和hadoop的文章

POST /forums/articles/_search{  "query": {    "constant_score": {      "filter": {        "bool": {          "must": [            {              "terms": {                "tag": [                  "java",                  "hadoop"                ]              }            }          ]        }      }    }  }}

第5个需求,我们要查询tag只是java的数据

这时原有数据如果统计的话比较麻烦,我们更新一下原有数据,添加一个新的字段tag_cnt用来表示tag数量,这样只需要查tag中含有java并且tag_cnt=1的数据即可。

POST /forums/articles/_search{  "query": {    "constant_score": {      "filter": {        "bool": {          "must": [            {              "term": {                "tag_cnt": 1              }            },            {              "term": {                "tag": "java"              }            }          ]        }      }    }  }}

第6个需求  添加了字段view_cnt用来表示访问次数,查询访问次数在40到80的文章并且发帖日期在2017年2月1日后

 

POST /forums/articles/_search{  "query": {    "constant_score": {      "filter": {        "bool": {          "must": [            {              "range": {                "view_cnt": {                  "gte": 40,                  "lte": 80                }              }            },            {              "range": {                "postDate": {                  "gte": "2017-02-01"                }              }            }          ]        }      }    }  }}
postDate": {                  "gte": "2017-01-01||+30d"                }

上面这种是针对日期类型的一种用法。

第7个需求:查询title中多个关键词的情况,比如java hadoop mysql 或者elasticsearch中出现3次的

GET /forums/articles/_search{  "query": {    "bool": {      "should": [        {"match": {          "title": "java"        }},        {"match": {          "title": "elasticsearch"        }},        {          "match": {            "title": "hadoop"          }        },        {"match": {          "title": "mysql"        }}      ],        "minimum_should_match": 3    }  }}

boost 可以进行细粒度的权重控制。

muti_query的例子,先更新数据

POST /forums/articles/_bulk{ "update": { "_id": "1"} }{ "doc" : {"content" : "i like to write best elasticsearch article"} }{ "update": { "_id": "2"} }{ "doc" : {"content" : "i think java is the best programming language"} }{ "update": { "_id": "3"} }{ "doc" : {"content" : "i am only an elasticsearch beginner"} }{ "update": { "_id": "4"} }{ "doc" : {"content" : "elasticsearch and hadoop are all very good solution, i am a beginner"} }{ "update": { "_id": "5"} }{ "doc" : {"content" : "spark is best big data solution based on scala ,an programming language similar to java"} }

按单个字段的结果优先排序

POST /forums/articles/_search{  "query": {    "dis_max": {      "queries": [        {          "match": {            "title": "java solution"          }        },        {          "match": {            "content": "java solution"          }        }      ]    }  }}

加入字段权重 同时加入 tie_breaker进行score分值折中 ,title权重为2

POST /forums/articles/_search{  "query": {    "dis_max": {      "queries": [        {          "match": {            "title": { "query": "java solution",            "minimum_should_match": "50%",            "boost" : 2            }                      }        },        {          "match": {            "content": "java solution"          }        }      ],      "tie_breaker": 0.7    }  }}

cross_fields的

POST /forums/articles/_search{  "query": {    "multi_match": {      "query": "Peter Smith",      "type": "cross_fields",      "operator" : "and",      "fields": [        "new_author_first_name",        "new_author_last_name"      ]    }  }}

 

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

你可能感兴趣的文章
ppm/℃是什么单位?什么意思?
查看>>
Java通过引用js脚本引擎实现精确计算
查看>>
面向对象-关于对象
查看>>
单向链表 实现(非线程安全)
查看>>
全国省市区信息,mysql数据库记录
查看>>
1.0 Linux文件系统
查看>>
2.0 Linux进程
查看>>
2.1 Linux 启动新进程
查看>>
2.2.1 进程管理,以及父子进程共享同一个文件资源时,文件的‘读写位置’会相互影响
查看>>
Linux 信号常量表
查看>>
Linux 错误码(error code)列表(头文件 ‘errno.h’)
查看>>
Linux 编程中的错误处理
查看>>
Linux 编程,C 语言中的陷阱 - sizeof(字符串字面量)
查看>>
问题:同一个进程中,先后对同一个文件描述符进行写入 / 读取 操作,读写指针的位置导致读取内容失败
查看>>
Linux 信号(signal)
查看>>
问题:Linux 信号处理,当连续给一个进程同时发送多个信号时,部分信号丢失而未得到处理
查看>>
Linux 进程信号屏蔽字 - 设置进程信号屏蔽字
查看>>
Linux 线程
查看>>
数据结构:缓冲区/缓存简单实现
查看>>
问题:C 语言结构体类型参数 与 结构体指针类型参数 的区别
查看>>