弹性搜索索引文档

时间:2015-09-22 00:39:49

标签: elasticsearch

我是Elasticsearch的新手,并试图了解它的基础知识。

我按照教程安装了ES。现在,我试图索引这里提到的文件 -

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

但它让我错误。

在尝试索引文档之前,我应该先创建索引吗?

此外,这里使用了哪些命令?这些CURL命令是什么?

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
~            

响应

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":{}}}

日志错误:

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)

2 个答案:

答案 0 :(得分:0)

您可以使用curl执行这些命令,只需查看所有引号等。例如,这是该教程中的第一个PUT命令,其中删除了一些字段:

curl -X PUT -d" {\" first_name \":\" John \",\" last_name \": \"史密斯\" }"本地主机:9200 / megacorp /雇员/ 1

它将创建索引并添加文档。

如果你转到http://localhost:9200/_plugin/head/,你应该可以看到你的索引。该页面上的“浏览器”选项卡可让您查看索引中的内容。

将使用合理的默认值创建megacorp索引和员工映射,在某些应用程序中,如果您不同意默认值,则可能需要明确定义这些默认值。

忘记提及......如果这还没有解决您的问题,请发布您遇到的错误。

答案 1 :(得分:0)

在索引文档之前,必须先创建索引。要创建索引,您可以发出POST请求。创建索引的curl请求示例是

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

这将返回对true的确认。 现在您可以索引文档,如

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