【发布时间】:2018-02-26 00:45:42
【问题描述】:
我正在 python 中使用 ElasticSearch 构建搜索系统。我加载了一个 csv 并创建了一个搜索索引。
from elasticsearch import helpers, Elasticsearch
import csv
es = Elasticsearch()
with open('/Users/anubhav/Office/elasticsearch-5.6.0/all_products.csv') as f:
reader = csv.DictReader(f)
helpers.bulk(es, reader, index='product-index', doc_type='product-index')
es.indices.create(
index='product-index',
body={
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
}
},
"tokenizer": {
"my_tokenizer": {
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 10,
"token_chars": [
"letter",
"digit",
"whitespace"
]
}
}
}
}
},
# Will ignore 400 errors, remove to ensure you're prompted
ignore=400
)
response = es.search(
index='product-index',
body={
"query": {
"match": {
"product": "PD5MP2 price"
}
},
"aggs": {
"top_10_states": {
"terms": {
"field": "state",
"size": 10
}
}
}
}
)
print response
当我搜索时:
res = es.search(index="product-index", doc_type="product-index", body={"query": {"match": {"product": "DD 350"}}})
这很好用,因为确切的产品在 CSV 中。但是当我将查询更改为
res = es.search(index="product-index", doc_type="product-index", body={"query": {"match": {"product": "DD350"}}})
它不起作用。有人可以帮我解决这个问题吗?
【问题讨论】:
标签: python csv elasticsearch