elasticsearch 查询

2019-11-24  本文已影响0人  mugtmag
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就是要设置最多可以返回的查询条数 默认是10000,如果不设置,直接设定size大于10000,将会报错
search_all = es.search(index="product", doc_type="product",
                   body={"size": count, "query": {"match_all": {}}})   # count是自定义的要查询的条数
### 'should' 是对字段“主营构成”和“主营产品详细”做'or'的匹配,'must_not'指的是‘不能有’; 'size'设置要查询的数目
search_result = es.search(index="product", doc_type="product",
                       body={"query": {"bool":
                                      {"should": [{"match_phrase": {"主营构成": product_item}},
                                                  {"match_phrase": {"主营产品详细": product_item}}],
                                                   "must_not": {"match_phrase": {"code": code}}}},
                                                   "size": most_like})   
search_result2 = es.search(index="product", doc_type="product",
                                       body={"size": 20,
                                             "query": {"constant_score": {"filter": {"terms": {"主营构成": list(related_ind)}}}}})   #返回结果只是有零星几个字眼匹配上了
GET /forum/article/_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "title": "java" }},
        { "match": { "title": "elasticsearch"   }},
        { "match": { "title": "hadoop"   }},
        { "match": { "title": "spark"   }}
      ],
      "minimum_should_match": 3 
    }
  }
}

搜索结果精准控制的第一步:灵活使用and关键字,如果你是希望所有的搜索关键字都要匹配的,那么就用and,可以实现单纯match query无法实现的效果

GET /forum/article/_search
{
    "query": {
        "match": {
            "title": {
        "query": "java elasticsearch",
        "operator": "and"
           }
        }
    }
}
import logging
logger = logging.getLogger('elasticsearch')
logger.setLevel(logging.WARNING)
上一篇 下一篇

猜你喜欢

热点阅读