1. 程式人生 > >elasticsearch的匹配與排序問題

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也完全沒生效,請問如何能做到準確匹配跟使模糊查詢結果排到後面