elasticsearch的匹配与排序问题


后台搜索使用的java api


 public List<Team> searchQueryTeam(String term, int limit, int offset, int isHighlight, long seed) throws Exception {

        final List<Team> list = new ArrayList<Team>();

        SearchRequestBuilder srbTeam = client.prepareSearch(INDEX_NAME)
                .setTypes("Team")
                .setSearchType(SearchType.DEFAULT);

        if (StringUtils.isNotEmpty(term)) {

            BoolQueryBuilder qbTeam = QueryBuilders.boolQuery().must(QueryBuilders.termQuery("rowState", 0))
                    .mustNot(QueryBuilders.termQuery("status", 1));

            qbTeam.must(QueryBuilders
                    .boolQuery()
                    .should(QueryBuilders.wildcardQuery("name", "*" + term + "*").boost(10f))
                    .should(QueryBuilders.fuzzyLikeThisQuery("name", "description", "tag").analyzer("ik")
                            .likeText(term)).boost(0.1f));

            srbTeam.setQuery(qbTeam);
        }
        ……

当term是10时,在head里复制了上面程序生成的语句


 {
  "from" : 0,
  "size" : 10,
  "query" : {
    "bool" : {
      "must" : [ {
        "term" : {
          "rowState" : 0
        }
      }, {
        "bool" : {
          "should" : [ {
            "wildcard" : {
              "name" : {
                "wildcard" : "*10*",
                "boost" : 10.0
              }
            }
          }, {
            "flt" : {
              "fields" : [ "name", "tag","description" ],
              "like_text" : "10",
              "analyzer" : "ik"
            }
          } ],
          "boost" : 0.1
        }
      } ],
      "must_not" : {
        "term" : {
          "status" : 1
        }
      }
    }
  }
}

查询的结果


 hits: {
       total: 2
       max_score: 0.9193326
       hits: [
           {
           _index: tmwk
           _type: Team
           _id: 309
           _score: 0.9193326
           _source: {
                   createDate: 2014-12-11 15:41:25
                   rowState: 0
                   id: 309
                   name: 产品研发团队
                   description: 产品研发团队是公司的重中之重,所有产品的源头来源于需求
                   status: 0
                   tag: 19, 17, 狗, 18, 15, 四六级, 我的
                   projectCount: 0
                   userCount: 0
                   role: null
                   lastModifiedDate: 2014-12-24 16:26:38
                   }
          }
          {
          _index: tmwk
          _type: Team
          _id: 270
          _score: 0.90041924
          _source: {
                   createDate: 2014-11-21 17:00:09
                   rowState: 0
                   id: 270
                   name: 10101010
                   description: sddd四六级额哦如萨拉杜甫,算法链接偶萨拉非。三等奖哦额乳酸代理费见,斯蒂芬就咯额ulsdjfsd。
                   status: 0
                   tag: 牛逼, 埃菲尔, 英国, 乒乓, 自行车, 天鹅
                   projectCount: 0
                   userCount: 0
                   role: null
                   lastModifiedDate: 2014-11-21 17:00:09
                   }
          }
       ]
}

建索引前,先设置了mapping,对name字段不做分词,其他的字段analyzer都设置为ik,不明白查询关键词是10时,为何会匹配到 tag: 19, 17, 狗, 18, 15, 四六级, 我的 这条,而且排名第一,程序里设置的boost也完全没生效,请问如何能做到准确匹配跟使模糊查询结果排到后面

elasticsearch

纯洁的怪蜀黍 10 years, 4 months ago

Your Answer