ElasticSearch中的简单查询

前言

  最近修改项目,又看了下ElasticSearch中的搜索,所以简单整理一下其中的查询语句等。都是比较基础的。PS,好久没写博客了。。大概就是因为懒吧。闲言少叙书归正传。

查询示例 http://*.*.*.*:9200/dbname/table/

  1. 最简单粗暴的查询  
    1. {
        "query": {
          "match_all": {}
        }
      }
  2. 简单的主键或者某一个条件查询
    1. {
        "query": {
          "term": {
            "artid": 1479282922430
          }
        }
      }
  3. filter,bool查询(可以进行条件联合查询,and,or等),
    1. {
        "query": {
          "filtered": {
            "filter": {
              "bool": {
                "should": {
                  "term": {
                    "pid": 6267
                  }
                }
              }
            }
          }
        }
      }
      {
        "query": {
          "filtered": {
            "filter": {
              "bool": {
                "should": {
                  "and": [
                    {
                      "term": {
                        "pid": 6267     //两个条件  一个是   pid=6267 and  privacy=1
                      }
                    },
                    {
                      "term": {
                        "privacy": 1
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ids 查询  id in (1,2,3,4,5,6),此查询可以联合bool 查询
    1. {
        "query": {
          "ids": {
            "values": [ 1,2,3,4,5,6 ]
          }
        }
      }
  4. 一个综合示例。 包含分页,排序,and条件查询,关键字查询
    1. {
        "query": {
          "filtered": {
            "filter": {
              "bool": {
                "should": [
                  {
                    "and": [
                      {
                        "term": {
                          "showpublic": 1
                        }
                      },
                      {
                        "term": {
                          "privacy": "1"
                        }
                      },
                      {
                        "or": [   //此处为匹配两个字段的关键字,符合其中一个就可以,用 or
                          {
                            "query": {
                              "match_phrase": {
                                "title": {
                                  "query": "关键字",
                                  "slop": 1
                                }
                              }
                            }
                          },
                          {
                            "query": {
                              "match_phrase": {
                                "name": {
                                  "query": "关键字",
                                  "slop": 1
                                }
                              }
                            }
                          }
                        ]
                      }
                    ]
                  }
                ]
              }
            }
          }
        },
        "from": 0,
        "size": 20,
        "sort": [
          {
            "ordertime": {
              "order": "desc"
            }
          },
          {
            "artid": {
              "order": "desc"
            }
          }
        ]
      }

      就写这么多吧。Over。

原文地址:https://www.cnblogs.com/panzi/p/6088361.html