- Python代碼中動(dòng)態(tài)修改可查詢的最大term數(shù)
es_url = [{"host": config.get("search_es", "host"), "port": config.get("search_es", "port")}]
es = Elasticsearch(es_url)
es.indices.put_settings(index="product", body={"index": {"max_result_window": count}}) #count就是要設(shè)置最多可以返回的查詢條數(shù) 默認(rèn)是10000,如果不設(shè)置,直接設(shè)定size大于10000,將會(huì)報(bào)錯(cuò)
- 查詢es所有的items
search_all = es.search(index="product", doc_type="product",
body={"size": count, "query": {"match_all": {}}}) # count是自定義的要查詢的條數(shù)
- 利用每個(gè)產(chǎn)品名稱和其他公司中的產(chǎn)品名稱做精確匹配
### 'should' 是對(duì)字段“主營(yíng)構(gòu)成”和“主營(yíng)產(chǎn)品詳細(xì)”做'or'的匹配,'must_not'指的是‘不能有’; 'size'設(shè)置要查詢的數(shù)目
search_result = es.search(index="product", doc_type="product",
body={"query": {"bool":
{"should": [{"match_phrase": {"主營(yíng)構(gòu)成": product_item}},
{"match_phrase": {"主營(yíng)產(chǎn)品詳細(xì)": product_item}}],
"must_not": {"match_phrase": {"code": code}}}},
"size": most_like})
- term filter查詢
search_result2 = es.search(index="product", doc_type="product",
body={"size": 20,
"query": {"constant_score": {"filter": {"terms": {"主營(yíng)構(gòu)成": list(related_ind)}}}}}) #返回結(jié)果只是有零星幾個(gè)字眼匹配上了
- should條件中可以設(shè)置至少匹配的項(xiàng)目(如果同時(shí)有must條件,則should中的條件可以沒有匹配到,但是如果只有should那么就要至少匹配到其中的一項(xiàng))
GET /forum/article/_search
{
"query": {
"bool": {
"should": [
{ "match": { "title": "java" }},
{ "match": { "title": "elasticsearch" }},
{ "match": { "title": "hadoop" }},
{ "match": { "title": "spark" }}
],
"minimum_should_match": 3
}
}
}
- 搜索標(biāo)題中包含java和elasticsearch的blog
搜索結(jié)果精準(zhǔn)控制的第一步:靈活使用and關(guān)鍵字,如果你是希望所有的搜索關(guān)鍵字都要匹配的,那么就用and,可以實(shí)現(xiàn)單純match query無(wú)法實(shí)現(xiàn)的效果
GET /forum/article/_search
{
"query": {
"match": {
"title": {
"query": "java elasticsearch",
"operator": "and"
}
}
}
}
- python程序中設(shè)置打印es log信息, 便于debug
import logging
logger = logging.getLogger('elasticsearch')
logger.setLevel(logging.WARNING)