खोज…


पैरामीटर

पैरामीटर विवरण
मेजबान host और port वाले ऑब्जेक्ट के रूप में मेजबानों की सरणी। डिफ़ॉल्ट host 'लोकलहोस्ट' है और port 9200 है। एक नमूना प्रविष्टि ऐसा दिखता है जैसे [{"host": "ip of es server", "port": 9200}]
sniff_on_start बुलियन यदि आप चाहते हैं कि ग्राहक स्टार्टअप पर नोड्स सूँघे, सूँघने का मतलब है इलास्टिक्स खोज क्लस्टर में नोड्स की सूची प्राप्त करना
sniff_on_connection_fail यदि ग्राहक सक्रिय है तो कनेक्शन विफल होने पर सूँघने की क्रिया के लिए बूलियन
sniffer_timeout प्रत्येक सूंघ के बीच सेकंड में समय का अंतर
sniff_timeout सेकंड में सूँघने का एक भी अनुरोध के लिए समय
retry_on_timeout Booelan के लिए यदि ग्राहक को एक अलग इलास्टिक्स खोज नोड से संपर्क करने के लिए ट्रिगर करना चाहिए या केवल त्रुटि फेंकनी चाहिए
http_auth बेसिक http प्रमाणीकरण यहाँ username:password के रूप में प्रदान किया जा सकता है

एक दस्तावेज़ अनुक्रमण (यानी। एक नमूना जोड़ना)

के माध्यम से आवश्यक पायथन लाइब्रेरी स्थापित करें:

$ pip install elasticsearch

एलिटिक्स खोज से कनेक्ट करें, दस्तावेज़ बनाएं (उदाहरण के लिए डेटा प्रविष्टि) और एलिस्टिक्स खोज का उपयोग करके "इंडेक्स" दस्तावेज़।

from datetime import datetime
from elasticsearch import Elasticsearch

# Connect to Elasticsearch using default options (localhost:9200)
es = Elasticsearch()

# Define a simple Dictionary object that we'll index to make a document in ES
doc = {
    'author': 'kimchy',
    'text': 'Elasticsearch: cool. bonsai cool.',
    'timestamp': datetime.now(),
}

# Write a document
res = es.index(index="test-index", doc_type='tweet', id=1, body=doc)
print(res['created'])

# Fetch the document
res = es.get(index="test-index", doc_type='tweet', id=1)
print(res['_source'])

# Refresh the specified index (or indices) to guarantee that the document
#  is searchable (avoid race conditions with near realtime search)
es.indices.refresh(index="test-index")

# Search for the document
res = es.search(index="test-index", body={"query": {"match_all": {}}})
print("Got %d Hits:" % res['hits']['total'])

# Show each "hit" or search response (max of 10 by default)
for hit in res['hits']['hits']:
    print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])

एक क्लस्टर के लिए कनेक्शन

es = Elasticsearch(hosts=hosts, sniff_on_start=True, sniff_on_connection_fail=True, sniffer_timeout=60, sniff_timeout=10, retry_on_timeout=True)

खाली इंडेक्स बनाना और मैपिंग सेट करना

इस उदाहरण में, हम इसकी मैपिंग को परिभाषित करके एक खाली सूचकांक बनाते हैं (हम इसमें कोई दस्तावेज़ नहीं अनुक्रमित करते हैं)।

सबसे पहले, हम एक ElasticSearch उदाहरण बनाते हैं और फिर हम अपनी पसंद के मानचित्रण को परिभाषित करते हैं। अगला, हम जांचते हैं कि क्या सूचकांक मौजूद है और यदि नहीं, तो हम क्रमशः index और body मापदंडों को निर्दिष्ट करके बनाते हैं जिसमें क्रमशः सूचकांक का नाम और मानचित्रण का शरीर होता है।

from elasticsearch import Elasticsearch

# create an ElasticSearch instance
es = Elasticsearch()
# name the index
index_name = "my_index"
# define the mapping
mapping = {
    "mappings": {
        "my_type": {
                "properties": {
                    "foo": {'type': 'text'},
                    "bar": {'type': 'keyword'}
                }
            }
        }
    }
    
# create an empty index with the defined mapping - no documents added
if not es.indices.exists(index_name):
    res = es.indices.create(
        index=index_name,
        body=mapping
    )
    # check the response of the request
    print(res)
    # check the result of the mapping on the index
    print(es.indices.get_mapping(index_name))

आंशिक अद्यतन और क्वेरी द्वारा अद्यतन

आंशिक अद्यतन: आंशिक दस्तावेज़ अद्यतन किए जाने की आवश्यकता होने पर इसका उपयोग किया जाता है, अर्थात निम्नलिखित उदाहरण में आईडी doc_id साथ दस्तावेज़ का फ़ील्ड name 'जॉन' के लिए अद्यतन होने जा रहा है। ध्यान दें कि यदि फ़ील्ड गायब है, तो इसे दस्तावेज़ में जोड़ा जाएगा।

doc = {
    "doc": {
        "name": "John"
    }
}
es.update(index='index_name',
          doc_type='doc_name',
          id='doc_id',
          body=doc)

क्वेरी द्वारा अपडेट करें: किसी शर्त को पूरा करने वाले दस्तावेजों को अपडेट करने के लिए आवश्यक होने पर उपयोग किया जाता है, अर्थात निम्नलिखित उदाहरण में हम उन दस्तावेजों की उम्र को अपडेट करते हैं जिनके name फ़ील्ड 'जॉन' से मेल खाते हैं।

q = {
  "script": {
    "inline": "ctx._source.age=23",
    "lang": "painless"
  },
  "query": {
    "match": {
        "name": "John"
    }
  }
}

es.update_by_query(body=q, 
                   doc_type='doc_name', 
                   index='index_name')


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow