ElasticSearch : 基础

#新建索引以及类型:
PUT http://10.18.43.3:9200/test
{
    "settings": {
        "number_of_shards": 3,
        "number_of_replicas": 0
    },
    "mappings": {
        "type1": {
            "properties": {
                "id": {
                    "type": "long"
                    },
                "ukey": {
                    "type": "keyword"
                },
                "startid": {
                    "type": "long"
                },
                "title": {
                    "type": "text"
                },
                "fetch_time": {
                    "type": "date",
                    "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
                }
            }
        }
    }
}
keyword类型的字段是不可切分的,text可以分词

#插入数据: PUT http://10.18.43.3:9200/test/type1/1
{
    "key": value,
    "key2": value2
}
,如果需要自动生成id,需要把方法改成POST,然后把url改成http://10.18.43.3:9200/test/type1

#更新数据
POST http://10.18.43.3:9200/test222/type1/1/_update
{
    "doc": {
        "content":"更新content2"
    }

}
更新数据需要用POST方法而且后面要加_update,更新的字段要包含在 doc 字段里面

#删除数据
DELETE http://10.18.43.3:9200/test/type1/1

#删除索引
DELETE http://10.18.43.3:9200/test

#简单查询
GET http://10.18.43.3:9200/test/type1/1

#条件查询
POST http://10.18.43.3:9200/test/_search
{
    "query": {
        "match_all": {}
    },
    "from": 1,
    "size": 2
}
from 是从哪一行开始,size 是查询显示多少条

POST http://10.18.43.3:9200/test/_search
{
    "query": {
        "match": {
            "html_snapshot": "医生"
        }
    },
    "sort": [
        {
            "id": {
                "order": "desc"
            }
        }    
    ]
}
查询所有html_snapshot包含"医生"的数据,类似于模糊查询(查询html_snapshot包含"医生"的数据),需要注意这个模糊匹配会把查询条件再次分词,如
条件为"html_snapshot": "医生和护士"的话,那么很可能会分成"医生","护士"两个词来分别匹配,如果需要像SQL那样 %xxx%这种形式的模糊匹配的话,
那么需要把 "match"改为"match_phrase",这样如果匹配"医生和护士",那么会匹配里面包含 xxx医生和护士xxxx 这种类型的数据
这儿html_snapshot是text类型,如果是keyword类型的话查询结果是精确查询的结果(查询html_snapshot等于"医生"的数据)
按照id降序排列

POST http://10.18.43.3:9200/test/_search
{
    "query": {
        "query_string": {
            "query": "(非常感谢 AND 医院) OR 医生",
            "fields": ["html_snapshot", "content"]
        }
    }
}
查询多个字段,这样查询也是会先自动分词然后进行匹配

POST http://10.18.43.3:9200/test/_search
{
    "query": {
        "term": {
            "startid": 2
        }
    }
}
精确查询 startid == 2

POST http://10.18.43.3:9200/test/_search
{
    "query": {
        "range": {
            "startid": {
                "gte": 2,
                "lte": 3
            }
        }
    }
}
startid 大于等于2,小于等于3的数据

POST http://10.18.43.3:9200/test/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "startid": 2
                    }
                },
                {
                    "match": {
                        "html_snapshot": "客气"
                    }
                }
            ]
        }
    }
}
查询 startid必须为2(因为是keyword类型) 和 html_snapshot必须包含(text类型)"客气" 的所有数据

#聚合查询
POST http://10.18.43.3:9200/test/_search
{
    "aggs": {
        "group_by_startid": {
            "terms": {
                "field": "startid",
                "size": 3
            }
        }
    }
}
根据startid统计所有数据count,返回统计结果前3的聚合数据,size是显示3条聚合数据
可以同时统计多组聚合信息

POST http://10.18.43.3:9200/test/_search
{
    "aggs": {
        "group_by_startid": {
            "stats": {
                "field": "startid"
            }
        }
    }
}
stats是计算聚合关键字,里面有startid的总数,最大值最小值平均值总和等数据

猜你喜欢

转载自www.cnblogs.com/cccy0/p/9579275.html