ES常用查询收录

1、等于查询

SELECT * FROM A WHERE B = 'C'

GET A/_search
{
  "query": {
    "match_phrase": {
      "B": "C"
    }
  }
}

2、like查询

SELECT * FROM A WHERE B LIKE '%C%'

GET A/_search
{
  "query": {
      "wildcard": {
        "B.keyword": {
          "value": "*C*"
        }
      }
  } 
}

3、and逻辑查询(都等于)

SELECT * FROM A WHERE B = 'C' AND D = 'E'

GET A/_search
{
	"query":{
	  "bool": {
	    "must": [
	      {
	        "match_phrase": {
	          "B": "C"
	        }
	      },
	      {
	        "match_phrase": {
	          "D": "E"
	        }
	      }
	    ]
	  }
	}
}

4、and逻辑查询(一个等于、一个不等于)

 SELECT * FROM A WHERE B = 'C' AND D <> 'E'

GET A/_search
{
	"query":{
	  "bool": {
	    "must": [
	      {
	        "match_phrase": {
	          "B": "C"
	        }
	      }
	    ],
	    "must_not": [
	      {
	        "match_phrase": {
	          "D" : "E"
	        }
	      }
	    ]
	  }
	}
}

 5、and逻辑查询(一个等于、一个小于)

 SELECT * FROM A WHERE B = 'C' AND D < E

GET A/_search
{
	"query":{
	  "bool": {
	    "must": [
	      {
	        "match_phrase": {
	          "B": "C"
	        }
	      }
	    ], 
	    "filter": {
	      "range": {
	        "price": {
	          "D": "E"
	        }
	      }
	    }
	  }
	}
}

  其中 gt -- > , lt -- < , gte -- >= , lte -- <=

6、or逻辑查询(都等于)

  SELECT * FROM A WHERE B = 'C' OR B = 'D'

GET A/_search
{
	"query":{
	  "bool": {
	    "should": [
	      {
	        "match_phrase": {
	          "B": "C"
	        }
	      },
	      {
	        "match_phrase": {
	          "B": "D"
	        }
	      }
	    ]
	  }
	}
}

 7.聚合

先把字段设置为fielddata, 才能做聚合

SELECT count(*) FROM A GROUP BY subject

PUT test/_mapping?pretty
{
  "properties": {
    "subject": { 
      "type": "text",
      "fielddata": true
    }
  }
}

GET test/_search
{
  "aggs": {
      "subject_count": {
        "terms": {
          "field": "subject.keyword",
          "size": 10000
        }
      }
  }
}
相关标签:
  • elasticsearch
0人点赞

发表评论

当前游客模式,请登陆发言

所有评论(0)