es查询参考

PUT /jzt_study_content
{
  "mappings": {
    "content":{
      "properties":{
        "id":{
          "type":"keyword"
        },
        "title":{
          "type":"text",
          "analyzer":"ik_max_word"
        },
        "sort_date":{
          "type":"date",
          "format":"yyyy-MM-dd HH:mm:ss"
        },
        "desc":{
           "type":"text",
          "analyzer":"ik_max_word"
        },
         "views":{
           "type":"long"
        },
        "content_type":{
           "type":"keyword"
        },
        "channelIds":{
           "type":"text"
        }
      }
    }
  }
}
GET /jzt_study_content/content/_search
{
  "from": 0,
  "size": 5, 
  "query": {
      "bool": {
        "should": [
           {
            "match": {
              "title": {
                "query":"展现出价快车触点",
                "boost": 10 //拉高标题匹配的评分
              }
            }
          },
          {
            "match": {
              "desc": {
                "query":"展现出价快车触点"
              }
            }
          }
        ],
        "must": [
          {
            "match": {
              "channelIds": {
                "query": "86 73 79",
                "operator":"and" //栏目id必须完全匹配,顺序可以忽略
              }
            }
          }
        ],
        "filter": {
            "term": {
              "content_type": {
                "value": "word" //先过滤,后match,增加效率
              }
            }
        }
      }
  },"sort": [
    {
      "sort_date": {
        "order": "desc"
      }
    }
  ]
}

GET /_analyze
{
  "analyzer":"ik_max_word",
  "text":"展现 出价"
}

输出结果:

{
  "took" : 8,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 12,
    "max_score" : 184.62587,
    "hits" : [
      {
        "_index" : "jzt_study_content",
        "_type" : "content",
        "_id" : "728",
        "_score" : 184.62587,
        "_source" : {
          "content_type" : "word",
          "id" : "728",
          "title" : "广告展现与出价",
          "channelIds" : "86 73 79",
          "sort_date" : "2015-05-07 19:01:09",
          "views" : "17234",
          "desc" : """
京准通后台系统会根据您广告的价格和质量度,计算竞争分数,按照分数对所有商家的广告进行排序,竞争分数最高的广告得到展现。
点击价格是由您和竞争分数在您的下一位的商家广告的价格和质量度共同决定。
"""
        }
      },
      {
        "_index" : "jzt_study_content",
        "_type" : "content",
        "_id" : "741",
        "_score" : 98.66294,
        "_source" : {
          "content_type" : "word",
          "id" : "741",
          "title" : "推荐广告位出价设置",
          "channelIds" : "86 73 79",
          "sort_date" : "2015-05-11 16:49:37",
          "views" : "21879",
          "desc" : """
推荐广告位固定CPC(按点击)计费,商家自定义输入推荐位出价,最低出价0.1元/点击,出价金额最多保留小数点后一位。
基于京准通后台实时数据生成【出价建议】,商家可查看相应类目的今日出价和近7天出价统计图表,有效帮助合理出价。
"""
        }
      },
      {
        "_index" : "jzt_study_content",
        "_type" : "content",
        "_id" : "1317",
        "_score" : 69.073944,
        "_source" : {
          "content_type" : "word",
          "id" : "1317",
          "title" : "京东快车分时段折扣功能",
          "channelIds" : "86 73 79",
          "sort_date" : "2017-09-06 22:22:29",
          "views" : "18663",
          "desc" : "凌晨时间段流量转化不好,希望降低整体计划的出价?分时段折扣功能可以让您在不同的时间段设置不同的折扣出价"
        }
      },
      {
        "_index" : "jzt_study_content",
        "_type" : "content",
        "_id" : "742",
        "_score" : 64.678,
        "_source" : {
          "content_type" : "word",
          "id" : "742",
          "title" : "京东快车操作基础之添加推广创意",
          "channelIds" : "86 73 79",
          "sort_date" : "2016-09-20 16:44:26",
          "views" : "20495",
          "desc" : "在创建推广单元并完善各项设置后,您需要新增创意并上传图片素材至后台进行审核,审核通过后即可进行广告竞价。"
        }
      },
      {
        "_index" : "jzt_study_content",
        "_type" : "content",
        "_id" : "1204",
        "_score" : 61.045647,
        "_source" : {
          "content_type" : "word",
          "id" : "1204",
          "title" : "京东快车-商品推荐功能介绍",
          "channelIds" : "86 73 79",
          "sort_date" : "2017-05-16 14:50:31",
          "views" : "15793",
          "desc" : "针对手动输入SKU ID,记忆成本大,选品困难等问题,为您智能推荐适合推广的商品,满足多场景推广需求,降低手动输入成本。"
        }
      },
      {
        "_index" : "jzt_study_content",
        "_type" : "content",
        "_id" : "724",
        "_score" : 53.968124,
        "_source" : {
          "content_type" : "word",
          "id" : "724",
          "title" : "京东快车操作基础之建立推广单元",
          "channelIds" : "86 73 79",
          "sort_date" : "2015-05-07 17:05:59",
          "views" : "30014",
          "desc" : "在创建完推广计划后,即可新增推广单元。您可在推广单元中设置投放地域、定向人群、关键词及匹配类型、搜索溢价系数、广告出价及无线出价系数。"
        }
      },
      {
        "_index" : "jzt_study_content",
        "_type" : "content",
        "_id" : "919",
        "_score" : 53.23211,
        "_source" : {
          "content_type" : "word",
          "id" : "919",
          "title" : "京东快车操作基础之活动一键推广",
          "channelIds" : "86 73 79",
          "sort_date" : "2016-08-03 14:48:54",
          "views" : "9154",
          "desc" : "活动一键推广是京东快车为降低用户操作成本、帮助商家快速创建活动推广而提供的的高效解决方案。"
        }
      },
      {
        "_index" : "jzt_study_content",
        "_type" : "content",
        "_id" : "723",
        "_score" : 48.78261,
        "_source" : {
          "content_type" : "word",
          "id" : "723",
          "title" : "京东快车操作基础之创建推广计划",
          "channelIds" : "86 73 79",
          "sort_date" : "2015-05-07 17:05:19",
          "views" : "77957",
          "desc" : "建立推广计划是京准通数字营销的第一步骤,通过推广商品、推广时间、推广单元等在内的基础资料输入,不仅帮助商家准确定向,也进一步规范管理平台推广行为。"
        }
      },
      {
        "_index" : "jzt_study_content",
        "_type" : "content",
        "_id" : "1090",
        "_score" : 9.904886,
        "_source" : {
          "content_type" : "word",
          "id" : "1090",
          "title" : "多日预算操作说明",
          "channelIds" : "86 73 79",
          "sort_date" : "2016-10-24 11:54:13",
          "views" : "20101",
          "desc" : "多日预算适用于京东快车和品牌聚效两条产品线。多日预算功能可以支持您在计划级别设置每天的预算时,无需在每日凌晨进行更改,可以直接设置从今日起往后30天的预算值。"
        }
      },
      {
        "_index" : "jzt_study_content",
        "_type" : "content",
        "_id" : "874",
        "_score" : 9.626433,
        "_source" : {
          "content_type" : "word",
          "id" : "874",
          "title" : "站内搜索广告位设置",
          "channelIds" : "86 73 79",
          "sort_date" : "2016-05-26 10:46:17",
          "views" : "9264",
          "desc" : "在新增/编辑推广单元页面中,可对站内搜索广告位进行设置;主要包括关键词出价及匹配方式、搜索人群的设置。"
        }
      },
      {
        "_index" : "jzt_study_content",
        "_type" : "content",
        "_id" : "740",
        "_score" : 9.381319,
        "_source" : {
          "content_type" : "word",
          "id" : "740",
          "title" : "站内搜索广告之关键词",
          "channelIds" : "86 73 79",
          "sort_date" : "2015-05-11 16:48:24",
          "views" : "32640",
          "desc" : "关键词推荐系统是京准通->京东快车中所提供的功能,所推荐词语综合考虑了买家搜索情况、产品属性等相关信息,是获取关键词最便捷有效的方法,建议大家做为买词的首选渠道。"
        }
      },
      {
        "_index" : "jzt_study_content",
        "_type" : "content",
        "_id" : "908",
        "_score" : 6.2458725,
        "_source" : {
          "content_type" : "word",
          "id" : "908",
          "title" : "站内推荐广告位设置",
          "channelIds" : "86 73 79",
          "sort_date" : "2016-07-12 16:55:38",
          "views" : "11529",
          "desc" : "在新增/编辑推广单元时,可进行推荐广告位的设置,此部分设置主要用于定向,即向符合设置条件的目标人群播放广告。"
        }
      }
    ]
  }
}
View Code

java RestHighLevel:

public ESContentOutCMS searchResult(ESSearchParams esSearchParams) {
        ESContentOutCMS esContentOutCMS = new ESContentOutCMS();
        List<String> channelIds = esSearchParams.getChannelIds();
        String contentType = esSearchParams.getContentType();
        String keywords = esSearchParams.getKeywords();
        String pageSize = esSearchParams.getPageSize();
        String sort = esSearchParams.getSort();
        String startIndex = esSearchParams.getStartIndex();
        //创建搜索请求对象
        SearchRequest searchRequest = new SearchRequest("jzt_study_content"); //索引
        //创建查询的bool对象
        BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
        //检索字段title,desc
        MatchQueryBuilder titleQuery = QueryBuilders.matchQuery("title", keywords);
        titleQuery.boost(10);//提高标题匹配权重
        QueryBuilder descQuery = QueryBuilders.matchQuery("desc", keywords);
        boolBuilder.should(titleQuery);
        boolBuilder.should(descQuery);
        //控制栏目过滤
        if (CollectionUtils.isNotEmpty(channelIds)) {
            MatchQueryBuilder channelIdsQuery =
                    QueryBuilders.matchQuery("channelIds", String.join(" ", channelIds));
            channelIdsQuery.operator(Operator.AND);//必须完全匹配,可以不管顺序,但此处顺序无影响
            boolBuilder.must(channelIdsQuery);
        }
        //控制类型选择,视频或文章
        if (StringUtils.isNotBlank(contentType)) {
            QueryBuilder contentTypeQuery = QueryBuilders.termQuery("content_type", contentType);
            boolBuilder.filter(contentTypeQuery);
        }

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(boolBuilder);
        //排序方式处理
        if (StringUtils.isNotBlank(sort) && ("views".equals(sort) || "date".equals(sort))) {
            searchSourceBuilder.sort(sort, SortOrder.DESC);
        }

        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); //设置一个可选的超时,控制允许搜索的时间
        searchSourceBuilder.fetchSource();
        searchSourceBuilder.from(Integer.valueOf(startIndex));
        searchSourceBuilder.size(Integer.valueOf(pageSize));
        log.info("搜索对象:{}", JSON.toJSONString(searchSourceBuilder));
        //设置查询对象到请求对象中
        searchRequest.source(searchSourceBuilder);
        try {
            SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            SearchHits searchHit = response.getHits();
            long total = searchHit.totalHits;
            esContentOutCMS.setTotal(total);
            SearchHit hitAry[] = searchHit.getHits();
            List<ContentCMS> contentCMSList = new LinkedList<>();
            for (int i = 0; i < hitAry.length; i++) {
                SearchHit item = hitAry[i];
                Map<String, String> dataMap = (Map) item.getSourceAsMap();
                ContentCMS contentCMS = new ContentCMS();
                contentCMS.setContent_id(dataMap.get("id"));
//                dataMap.get("content_type");
                contentCMS.setTitle(dataMap.get("title"));
                String ids = dataMap.get("channelIds");
                contentCMS.setChannelIds(ids.replaceAll(" ", ","));
                contentCMS.setSort_date(dataMap.get("sort_date"));
                contentCMS.setView_count(dataMap.get("views"));
                contentCMS.setDescription(dataMap.get("desc"));
                contentCMSList.add(contentCMS);
            }
            esContentOutCMS.setList(contentCMSList);
            System.out.println();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return esContentOutCMS;
    }
View Code

 

posted @ 2020-07-24 14:22  soft.push("zzq")  Views(124)  Comments(0Edit  收藏  举报