elasticsearch 查詢

  • 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)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容