Python 2.7 如何使用python从elasticsearach搜索数据?
以下是json:Python 2.7 如何使用python从elasticsearach搜索数据?,python-2.7,
elasticsearch,Python 2.7,
elasticsearch,以下是json: { "took": 3, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 12, "max_score": 1, "hits": [ { "_index": "test", "_type": "skills",
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 12,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "skills",
"_id": "AV9OR3LcpPcQ2GTzXhWp",
"_score": 1,
"_source": {
"skill": "j2ee",
"company name": "avr",
"No of emp": 50
}
},
{
"_index": "test",
"_type": "skills",
"_id": "AV9OVa_rpPcQ2GTzXhWu",
"_score": 1,
"_source": {
"skill": "c++",
"company name": "abc",
"No of emp": 70
}
},
{
"_index": "test",
"_type": "skills",
"_id": "AV9ORxCxpPcQ2GTzXhWn",
"_score": 1,
"_source": {
"skill": "python",
"company name": "avr",
"No of emp": 20
}
},
{
"_index": "test",
"_type": "skills",
"_id": "AV9OVRYIpPcQ2GTzXhWs",
"_score": 1,
"_source": {
"skill": "c#",
"company name": "edge",
"No of emp": 70
}
},
{
"_index": "test",
"_type": "skills",
"_id": "AV9OVw9ypPcQ2GTzXhWx",
"_score": 1,
"_source": {
"skill": "oracle",
"company name": "wipro",
"No of emp": 90
}
},
{
"_index": "test",
"_type": "skills",
"_id": "AV9OVmx6pPcQ2GTzXhWv",
"_score": 1,
"_source": {
"skill": "c",
"company name": "hcl",
"No of emp": 70
}
},
{
"_index": "test",
"_type": "skills",
"_id": "AV9OVtvRpPcQ2GTzXhWw",
"_score": 1,
"_source": {
"skill": "sql",
"company name": "wipro",
"No of emp": 90
}
},
{
"_index": "test",
"_type": "skills",
"_id": "AV9OV6r9pPcQ2GTzXhWz",
"_score": 1,
"_source": {
"skill": "elastic",
"company name": "avr",
"No of emp": 60
}
},
{
"_index": "test",
"_type": "skills",
"_id": "AV9ORt68pPcQ2GTzXhWm",
"_score": 1,
"_source": {
"skill": "python",
"company name": "avr",
"No of emp": 100
}
},
{
"_index": "test",
"_type": "skills",
"_id": "AV9ORzv0pPcQ2GTzXhWo",
"_score": 1,
"_source": {
"skill": "JAVA",
"company name": "avr",
"No of emp": 30
}
},
{
"_index": "test",
"_type": "skills",
"_id": "AV9OVV0SpPcQ2GTzXhWt",
"_score": 1,
"_source": {
"skill": ".net",
"company name": "networks",
"No of emp": 70
}
},
{
"_index": "test",
"_type": "skills",
"_id": "AV9OV2dUpPcQ2GTzXhWy",
"_score": 1,
"_source": {
"skill": "mongo",
"company name": "wipro",
"No of emp": 90
}
}
]
}
}
以下是我的映射:
{
"state": "open",
"settings": {
"index": {
"creation_date": "1508846730700",
"number_of_shards": "5",
"uuid": "rfgABcAlTC2sXDlqpKerjw",
"version": {
"created": "1070299"
},
"number_of_replicas": "0"
}
},
"mappings": {
"skills": {
"properties": {
"No of emp": {
"type": "long"
},
"skill": {
"index": "not_analyzed",
"type": "string"
},
"company name": {
"type": "string"
}
}
}
},
"aliases": []
}
问题:
1) 如果emp的编号大于50,我想打印公司名称
2) 如果emp的编号小于50,我想打印技能
3) 我想打印技能
,如果emp的编号
大于50或公司名称
为avr
4) 我想打印公司名称
,如果emp的编号
大于50且技能
是j2ee
或公司名称
是avr
使用python我应该解决这个问题。
我使用的是elasticsearch 1.7
提前感谢。在python中:
from elasticsearch import Elasticsearch
# create the elastic client instace:
es = Elasticsearch(hosts="IPadd:port")
# now you can execute the queries and get the response:
# response = es.search(index=index, body=query)
要回答您的四个问题:
1) 如果emp的编号大于50,则打印公司名称:
我想你想要所有员工超过50人的公司的名字。因此,我将(在这里和其他问题中)使用-
2) 我想打印技能
,如果emp的编号小于50:
{
"size": 0,
"query": {
"bool": {
"must": [
{
"range": {
"No of emp": {
"lt": 50
}
}
}
]
}
},
"aggregations": {
"skills": {
"terms": {
"field": "skill"
}
}
}
}
3) 我想打印技能
,如果emp的编号
大于50或公司名称
为avr
:
{
"size": 0,
"query": {
"bool": {
"should": [
{
"range": {
"No of emp": {
"gt": 50
}
}
},
{
"term": {
"company name": "avr"
}
}
],
"minimum_should_match": 1
}
},
"aggregations": {
"skills": {
"terms": {
"field": "skill"
}
}
}
}
{
"size": 0,
"query": {
"bool": {
"must": [
{
"range": {
"No of emp": {
"gt": 50
}
}
}
],
"should": [
{
"term": {
"skill": "j2ee"
}
},
{
"term": {
"company name": "avr"
}
}
],
"minimum_should_match": 1
}
},
"aggregations": {
"companies": {
"terms": {
"field": "company name"
}
}
}
}
4) 如果emp的编号大于50且skill
是j2ee
或company name
是avr
,我想打印company name
:
{
"size": 0,
"query": {
"bool": {
"should": [
{
"range": {
"No of emp": {
"gt": 50
}
}
},
{
"term": {
"company name": "avr"
}
}
],
"minimum_should_match": 1
}
},
"aggregations": {
"skills": {
"terms": {
"field": "skill"
}
}
}
}
{
"size": 0,
"query": {
"bool": {
"must": [
{
"range": {
"No of emp": {
"gt": 50
}
}
}
],
"should": [
{
"term": {
"skill": "j2ee"
}
},
{
"term": {
"company name": "avr"
}
}
],
"minimum_should_match": 1
}
},
"aggregations": {
"companies": {
"terms": {
"field": "company name"
}
}
}
}
在python中:
from elasticsearch import Elasticsearch
# create the elastic client instace:
es = Elasticsearch(hosts="IPadd:port")
# now you can execute the queries and get the response:
# response = es.search(index=index, body=query)
要回答您的四个问题:
1) 如果emp的编号大于50,则打印公司名称:
我想你想要所有员工超过50人的公司的名字。因此,我将(在这里和其他问题中)使用-
2) 我想打印技能
,如果emp的编号小于50:
{
"size": 0,
"query": {
"bool": {
"must": [
{
"range": {
"No of emp": {
"lt": 50
}
}
}
]
}
},
"aggregations": {
"skills": {
"terms": {
"field": "skill"
}
}
}
}
3) 我想打印技能
,如果emp的编号
大于50或公司名称
为avr
:
{
"size": 0,
"query": {
"bool": {
"should": [
{
"range": {
"No of emp": {
"gt": 50
}
}
},
{
"term": {
"company name": "avr"
}
}
],
"minimum_should_match": 1
}
},
"aggregations": {
"skills": {
"terms": {
"field": "skill"
}
}
}
}
{
"size": 0,
"query": {
"bool": {
"must": [
{
"range": {
"No of emp": {
"gt": 50
}
}
}
],
"should": [
{
"term": {
"skill": "j2ee"
}
},
{
"term": {
"company name": "avr"
}
}
],
"minimum_should_match": 1
}
},
"aggregations": {
"companies": {
"terms": {
"field": "company name"
}
}
}
}
4) 如果emp的编号大于50且skill
是j2ee
或company name
是avr
,我想打印company name
:
{
"size": 0,
"query": {
"bool": {
"should": [
{
"range": {
"No of emp": {
"gt": 50
}
}
},
{
"term": {
"company name": "avr"
}
}
],
"minimum_should_match": 1
}
},
"aggregations": {
"skills": {
"terms": {
"field": "skill"
}
}
}
}
{
"size": 0,
"query": {
"bool": {
"must": [
{
"range": {
"No of emp": {
"gt": 50
}
}
}
],
"should": [
{
"term": {
"skill": "j2ee"
}
},
{
"term": {
"company name": "avr"
}
}
],
"minimum_should_match": 1
}
},
"aggregations": {
"companies": {
"terms": {
"field": "company name"
}
}
}
}
到目前为止,你的Python代码是什么?我刚刚打印了索引中显示的所有文档…我是弹性搜索新手…到目前为止,你的Python代码是什么?我刚刚打印了索引中显示的所有文档…我是弹性搜索新手…完美答案…感谢完美答案…谢谢