ES使用笔记

1、创建带有类型、映射的索引

PUT /cscdp_standard_item_dev
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "_doc": {
      "dynamic": "strict",
      "properties": {
        "id":{
          "type": "keyword"
        },
        "question": {
          "type": "nested",
          "properties": {
            "title": {
              "type": "text"
            },
            "orderNum": {
              "type": "integer"
            }
          }
        },
        "operateTime": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
        }
      }
    }
  }
}

2、查看索引信息

GET /cscdp_standard_item_dev/_mappings

3、删除索引

DELETE /cscdp_standard_item_dev

4、普通字段查询

GET /cscdp_standard_item_dev/_search
{
  "query" : {
    "match" : {
      "standardUid": "SD0000187"
      }
  }
}

5、查询出tags为 "" 或者为 null 的数据

{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": [
        {
          "terms": {
            "_id": [
              "ec5db526a7b32bdd1f6ac865cf830436",
              "e0422a0641334293810275a5302b5014"
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "term": {
                  "tags": ""
                }
              },
              {
                "bool": {
                  "must_not": [
                    {
                      "exists": {
                        "field": "tags"
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  },
  "size": 100,
  "sort": [],
  "_source": [
    "uuid",
    "tags"
  ]
}

java代码:

BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
 
boolQueryBuilder.must(
    QueryBuilders.boolQuery()
        .should(QueryBuilders.termQuery("要查的列名",""))
        .should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("要查的列名"))
);

 

6、查询original字段不为""或null

es查询:
GET /cscdp_standard_item_dev/_search
{
  "query": {
    "bool": {
      "must": [
        {
            "term" : {
              "standardUid" : {
                "value" : "SD0000196"
              }
            }
          },
          {
            "term" : {
              "standardVersion" : {
                "value" : "0501001"
              }
            }
          },
        {
          "bool": {
            "must": {
              "exists": {
                "field": "original"
              }
            },
            "must_not": [
              {
                  "term":{
                      "original": ""
                  }
              }    
            ]
          }
        }
      ]
    }
  }
}

java代码:

BoolQueryBuilder filter = QueryBuilders.boolQuery();
 filter.must(QueryBuilders.existsQuery(field)).mustNot(QueryBuilders.termQuery(field, ""));

 

7、按指定条件查询后进行分组

GET /scdp_standard_item/_search
{
    "query": {
    "bool": {
      "must": [
        {
            "term" : {
              "standardUid" : {
                "value" : "SD0000199"
              }
            }
          },
          {
            "term" : {
              "standardVersion" : {
                "value" : "v 0.3"
              }
            }
          },
        {
          "bool": {
            "must": {
              "exists": {
                "field": "original"
              }
            },
            "must_not": [
              {
                  "term":{
                      "original": ""
                  }
              }    
            ]
          }
        }
      ]
    }
  },
  "size": 0, 
  "aggs": {
    "GROUP_ORIGINAL": {
      "terms": {
        "field": "original"
      }
    }
  }
}

结果:

ES使用笔记_第1张图片

8、查出某一个字段去重后数量,这个字段不为""且不为null

ES使用笔记_第2张图片

你可能感兴趣的:(ES学习笔记)