elasticsearch API使用方法備忘(Python)

來源:互聯網
上載者:User

標籤:elasticsearch   api   pytho   


安裝模組

pip install elasticsearch

建立串連

from elasticsearch import Elasticsearch

es = Elasticsearch(['192.168.1.1:9200'])

 

多節點

es = Elasticsearch(['192.168.1.1:9200',’192.168.1.2:9200’])

支援SSL的串連

es = Elasticsearch(

      ['localhost:443', 'other_host:443'],

      # turn on SSL

      use_ssl=True,

      # make sure we verify SSL certificates (off by default)

      verify_certs=True,

      # provide a path to CA certs on disk

      ca_certs='/path/to/CA_certs'

)

 

 

建立索引

es.indices.create(index='test_index', ignore=400)

es.indices.delete(index='test_index', ignore=[400, 404])

ignore可以忽略異常,其值是需要忽略的異常對應的返回碼,常見的有400表示索引已存在,404表示索引沒找到。

使用自訂映射建立索引:

mapping = {

    "mappings": {

      "test_type": {

        "properties": {

          "name": {

            "type":    "string",

            "index": "not_analyzed"

          },

          "phone": {

            "type":    "string",

            "index": "not_analyzed"

          }

        }

      }

  }

}

es.indices.create(index='test_index',ignore=400,body=mapping)

查看索引的映射:

es.indices.get_mapping("test_index")

 

寫入資料

插入一條:

es.index(index='test_index’,doc_type='test_type',body={“key”:”value”})

批量寫入:

doc = [

       {"index": {}},

       {'name': 'Jack', 'phone': '123456'},

       {"index": {}},

       {'name': 'Joe', 'phone': '321456' },

       {"index": {}},

       {'name': 'Nicole', 'phone': '654321'},

       {"index": {}},

       {'name': 'Lucy', 'phone': '456123'},

 ]

es.bulk(index='test_index',doc_type='test_type',body=doc)

 

刪除資料

根據id刪除一條資料

es.delete(index="test_index",doc_type="test_type",id="ZTg5IGMBxTpLs9ylvHBz")

根據查詢條件刪除資料:

body = {

      "query":{

            "match":{

                "name":   "Joe"

            }

          }

}

es.delete_by_query(index='test_index',doc_type='test_type',body=body)

 

查詢

查詢所有資料

body = {

    "query":{

          "match_all":{}

    }

}

es.search(index="test_index",doc_type="test_type",body=body)

或者

es.search(index="test_index",doc_type="test_type")

完全符合term:

#搜尋name欄位為Nicole的資料

body = {

      "query":{

            "term":{

                "name":   "Nicole"

            }

          }

}

es.search(index="test_index",doc_type="test_type",body=body)

關鍵字匹配match:

#搜尋name欄位包含Nicole關鍵字的資料

body = {

      "query":{

            "match":{

                "name":   "Nicole"

            }

          }

}

es.search(index="test_index",doc_type="test_type",body=body)

bool聯集查詢,bool有3類查詢關係,must(都滿足),should(其中一個滿足),must_not(都不滿足)

#搜尋name為Nicole並且phone為123456的資料

body = {

    "query":{

        "bool":{

              "must":[

                {

                      "term":{

                          "name": "Nicole"

                    }

                },

                {

                      "term":{

                          "phone": “123456”

                    }

                }

            ]

        }

    }

}

es.search(index="test_index",doc_type="test_type",body=body)

 


elasticsearch API使用方法備忘(Python)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.