简体   繁体   中英

Elastic search indexing document

Im a newbie to Elasticsearch and trying to understand the basics of it.

I followed the tutorial and installed ES. Now, Im trying to index a document as mentioned here -

https://www.elastic.co/guide/en/elasticsearch/guide/current/_indexing_employee_documents.html

but it throws me error.

Should I create my index first before I try to index the document?

Also, What commands are being used here? Are those CURL commands?

import requests
r = requests.get('http://localhost:9200/megacorp')

print r.status_code
print r.text
r = requests.get('http://localhost:9200/twitter')

print r.status_code
print r.text
~            

response

200
{"megacorp":{"aliases":{},"mappings":{"employee":{"properties":{"first_name":{"type":"string"},"last_name":{"type":"string"}}}},"settings":{"index":{"creation_date":"1442881963974","uuid":"5bISz0kqTdyjYgz-Hv548Q","number_of_replicas":"1","number_of_shards":"5","version":{"created":"1070299"}}},"warmers":{}}}
200
{"twitter":{"aliases":{},"mappings":{},"settings":{"index":{"creation_date":"1443018283701","uuid":"3DS6RZPYTWuX0-ah18e-Ww","number_of_replicas":"2","number_of_shards":"3","version":{"created":"1070299"}}},"warmers":{}}}

Error in logs:

SearchRequest@2e5f4063] lastShard [true]
org.elasticsearch.search.SearchParseException: [megacorp][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"facets":{"0":{"date_histogram":{"key_field":"@timestamp","value_field":"primaries.indexing.index_total","interval":"1y"},"global":true,"facet_filter":{"fquery":{"query":{"filtered":{"query":{"query_string":{"query":"_type:indices_stats"}},"filter":{"bool":{"must":[{"match_all":{}}]}}}}}}}},"size":50,"query":{"filtered":{"query":{"query_string":{"query":"_type:cluster_event OR _type:node_event"}},"filter":{"bool":{"must":[{"match_all":{}}]}}}},"sort":[{"@timestamp":{"order":"desc","ignore_unmapped":true}},{"@timestamp":{"order":"desc","ignore_unmapped":true}}]}]]
    at org.elasticsearch.search.SearchService.parseSource(SearchService.java:747)
    at org.elasticsearch.search.SearchService.createContext(SearchService.java:572)
    at org.elasticsearch.search.SearchService.createAndPutContext(SearchService.java:544)
    at org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:306)
    at org.elasticsearch.search.action.SearchServiceTransportAction$5.call(SearchServiceTransportAction.java:231)
    at org.elasticsearch.search.action.SearchServiceTransportAction$5.call(SearchServiceTransportAction.java:228)
    at org.elasticsearch.search.action.SearchServiceTransportAction$23.run(SearchServiceTransportAction.java:559)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.elasticsearch.search.facet.FacetPhaseExecutionException: Facet [0]: (key) field [@timestamp] not found
    at org.elasticsearch.search.facet.datehistogram.DateHistogramFacetParser.parse(DateHistogramFacetParser.java:172)
    at org.elasticsearch.search.facet.FacetParseElement.parse(FacetParseElement.java:93)
    at org.elasticsearch.search.SearchService.parseSource(SearchService.java:731)

You can use curl to execute those commands, just watch all the quotes, etc. For example, here is the first PUT command from that tutorial with some fields removed:

curl -X PUT -d "{ \\"first_name\\": \\"John\\", \\"last_name\\": \\"Smith\\" }" localhost:9200/megacorp/employee/1

It will create the index and add the document.

If you go to http://localhost:9200/_plugin/head/ you should be able to see your index. The Browser tab on that page will let you see what is in your index.

The megacorp index and the employee mapping will be created using sensible defaults, in some applications you may want to define those explicitly if you disagree with the defaults.

Forgot to mention... if this doesn't solve your problem, please post the error you are getting.

You have to create an index first, before indexing documents. For creating an index, you can issue a POST request. An example curl request for creating an index is

curl -XPOST "http://localhost:9200/test_index"

This will return an acknowledge of true. Now you can index document like

curl -XPOST "http://localhost:9200/test_index/test_type" -d "{\"name\" : \"This is my name\", \"age\" : 25}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM