ElasticSearchとpythonのインタラクション

22798 ワード

試験環境ElasticSearch 7.7.0 python 3.8
1.基本クエリーデータ
from elasticsearch import Elasticsearch

es = Elasticsearch()

query = {
    "query": {
        "match_all": {

        }
    }
}
res = es.search(index='movies', doc_type='movie', body=query)
print(res)
#     movies  type movie     

termとtermsの使い方
from elasticsearch import Elasticsearch

es = Elasticsearch()

query = {
    "query": {
        "term": {
            "title": "Kill"
        }
    }
}
res = es.search(index='movies', doc_type='movie', body=query)
print(res)
#       movies  type movie   title  kill     
from elasticsearch import Elasticsearch

es = Elasticsearch()

query = {
    "query": {
        "terms": {
            "title":
                [
                    "Kill", "Lawrence of Arabia"
                ]
        }
    }
}
res = es.search(index='movies', doc_type='movie', body=query)
print(res)
#       movies  type movie   title  kill title  Lawrence of Arabia     

matchとmulti_match
from elasticsearch import Elasticsearch

es = Elasticsearch()

query = {
    "query": {
        "match": {
            "title": "kill"

        }
    }
}
res = es.search(index='movies', doc_type='movie', body=query)
print(res)
#       movies  type movie   title  kill     
from elasticsearch import Elasticsearch

es = Elasticsearch()

query = {
    "query": {
        "multi_match": {
            "query": "kill",
            "fields": ["title", "genres"]

        }
    }
}
res = es.search(index='movies', doc_type='movie', body=query)
print(res)
#       movies  type movie      title genres     kill     

ids
from elasticsearch import Elasticsearch

es = Elasticsearch()

query = {
    "query": {
        "ids": {
            "type": "movie",
            "values": [
                "1", "2"
            ]

        }
    }
}
res = es.search(index='movies', body=query)
print(res)
#       movies  type movie  id 1  2     

複合クエリー
boolには3種類のクエリー関係があり、must(いずれも満足)、should(そのうちの1つが満足)、must_not(いずれも満足していない)
from elasticsearch import Elasticsearch

es = Elasticsearch()

query = {
    "query": {
        "bool": {
            "must": [{
                "match": {
                    "city": "Brogan"
                }
            },
                {
                    "match": {
                        "state": "IL"
                    }
                }
            ]

        }
    }
}
res = es.search(index='bank', doc_type="typeName", body=query)
print(res)
#    bank  type typeName city  Brogan state  IL     ,
#   match  term      

スライスクエリfrom size
from elasticsearch import Elasticsearch

es = Elasticsearch()

query = {
    "query": {
        "match_all": {},
    },
    "from": 2,
    "size": 4
}
res = es.search(index='bank', doc_type="typeName", body=query)
print(res)
#from        ,size          sql  skip  limit   

範囲クエリーrange
from elasticsearch import Elasticsearch

es = Elasticsearch()

query = {
    "query": {
        "range": {
            "age": {
                "gte": 20,
                "lte": 25
            }
        },
    }

}
res = es.search(index='bank', doc_type="typeName", body=query)
print(res)
#      20-25       

接頭辞によるクエリー
from elasticsearch import Elasticsearch

es = Elasticsearch()

query = {
    "query": {
        "prefix": {
            "title": "T"
        }
    }
}
res = es.search(index='movies', doc_type="movie", body=query)
print(res)
  title    T       


ツールバーの
from elasticsearch import Elasticsearch

es = Elasticsearch()

query = {
    "query": {
        "match_all": {}
    },
    "sort": {
        "age": {  #   age      
            "order": "asc"  # asc  ,desc  
        }
    }

}
res = es.search(index='bank', doc_type="typeName", body=query)
print(res)
        

フィルタリングとmongoのクエリーに応答して、一部のフィールドが表示されます.
#           
from elasticsearch import Elasticsearch

es = Elasticsearch()
query=["hits.hits._id"]
es.search(index="bank",doc_type="typeName",filter_path=query)
 
#       
query=["hits.hits._*"]
es.search(index="bank",doc_type="typeName",filter_path=query)

クエリー照合数
es.count(index="bank",doc_type="typeName")