ElasticSearch 基本操作

ElasticSearch 基本操作

ES中最基本的增删改查操作,这里做个汇总,以后可以看一下。查找部分还有很多没有写出来,内容是在太多了。


1. 增加索引

增加索引my_store,字段有price,productID

curl -X PUT "localhost:9200/my_store" -H 'Content-Type: application/json' -d'
{
	"settings": {
        "number_of_shards": 3,
        "number_of_replicas": 1
	}
    "mappings" : {
        "products" : {
            "properties" : {
            	"price": {
                    "type": "keyword"
            	},
                "productID" : {
                    "type" : "keyword"
                }
            }
        }
    }
}'

2. 增加文档

指定ID

curl -X PUT "localhost:9200/my_store/products/1" -H 'Content-Type: application/json' -d'
{
    "price": 20,
    "productID": "XHDK-A-1293-#fJ3"
}'

不指定ID

curl -X POST "localhost:9200/my_store/products/" -H 'Content-Type: application/json' -d'
{
    "price": 20,
    "productID": "XHDK-A-1293-#fJ3"
}'

1. 删除文档

curl -X DELETE "localhost:9200/my_store/products/1"

2. 删除索引

curl -X DELETE "localhost:9200/my_store"

修改id为1的文档的 price 为 50

curl -X POST "localhost:9200/my_store/products/1/_update" -H 'Content-Type: application/json' -d'
{
    "docs": {
        "price": 50
    }
}'

这里只是粗略介绍

1. 通过 _id 查询

curl -X GET "localhost:9200/my_store/products/1"

2. 条件查询

查询该索引下所有的记录

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '
{
    "query": {
        "match_all": {}
    }
}'

限制条数(从第一条开始,显示一条)

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '
{
    "query": {
        "match_all": {}
    },
    "from": 1,
    "size": 1
}'

限定条件

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '
{
	"query": {
		"match": {
			"price": 30
		}
	}
}'

设定升降序

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '
{
	"query": {
		"match": {
			"price": 30
		}
	},
	"sort": [
        {"productID": {"order": "desc"}}
	]
}'

3. 聚合查询

查询

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '
{
	"aggs": {
        "group_by_word_count": {
            "terms": {
                "field": "word_count"
            }
        },
        "group_by_publish_date": {
            "terms": {
                "field": "publish_date"
            }
        }
	}
}'

计算

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '
{
	"aggs": {
        "grades_word_count": {
            "stats": {
                "field": "word_count"
            }
        }
	}
}'

高级查询

  • 子条件查询 特定字段查询所指特定值

    • Query context

      在查询过程中,除了判断文档是否满足查询条件外,ES还会计算一个_score来标识匹配的程度,旨在判断目标文档和查询条件匹配的有多好。

      • 全文本查询 针对文本类型数据
      • 字段级别查询 针对结构化数据(数字,日期等)
    • Filter context

      在查询过程中,指判断该文档是否满足条件,只有 YES 或 NO。

  • 符合条件查询 以一定的逻辑组合子条件查询

1. query

1.1. 模糊匹配 - 非结构化数据查询

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '
{
	"query": {
        "match": {
            "price": 30
        }
	}
}'

缺陷:查询组合词的时候会被自动拆分,比如说ElasticSearch入门会被拆分为ElasticSearch入门

1.2. 习语匹配 - 非结构化数据查询

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '
{
	"query": {
        "match_phrase": {
            "title": "ElasticSearch入门"
        }
	}
}'

1.3. 多字段匹配 - 非结构化数据查询

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '
{
	"query": {
        "multi_match": {
            "query": "ElasticSearch",
            "fields": ["title", "content"]
        }
	}
}'

1.4. 语法匹配 - 非结构化数据查询

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '
{
	"query": {
        "query_string": {
            "query": "ElasticSearch AND 入门"
        }
	}
}'
curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '
{
	"query": {
        "query_string": {
            "query": "(ElasticSearch AND 入门) OR Python"
        }
	}
}'
curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '
{
	"query": {
        "query_string": {
            "query": "ElasticSearch OR 入门"
            "fields": ["title", "content"]
        }
	}
}'

1.5. 字段查询 - 结构化数据查询

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '
{
	"query": {
        "term": {
            "title": "ElasticSearch"
        }
	}
}'

2. filter

和 query 最大的区别在于没有_score了,并且会对结果进行缓存。

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '
{
	"query": {
        "bool": {
            "filter": {
                "term": {
                    "price": 30
                }
            }
        }
	}
}'

3. 复合查询

3.1. 固定分数查询

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '
{
	"query": {
        "constant_score": {
            "filter": {
                "match": {
                    "title": "ElasticSearch"
                }
            },
            "boost": 2
        }
	}
}'

3.2. 布尔查询

  • should:相当于sql中的 or

    curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '
    {
    	"query": {
            "bool": {
                "should": [
                    {"match": {"title": "ElasticSearch"}},
                    {"match": {"content": "ES"}}
                ]
            }
    	}
    }'
    
  • must:相当于sql中的 is

    curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '
    {
    	"query": {
            "bool": {
                "must": [
                    {"match": {"title": "ElasticSearch"}},
                    {"match": {"content": "ES"}}
                ]
            }
    	}
    }'
    
  • must_not:相当于sql中的 is not

    curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '
    {
    	"query": {
            "bool": {
                "must_not": {
                    "term": {
                        "title": "ElasticSearch"
                    }
                }
            }
    	}
    }'
    
posted @ 2019-01-10 19:14  Yisany  阅读(124)  评论(0编辑  收藏  举报