ElasticSearch查询无法在某些字段

时间:2016-08-05 05:56:02

标签: java elasticsearch spring-data-elasticsearch

我正在使用以下搜索参数在我的webapp中创建搜索。此查询不适用于某些字段,如MaxScore,Experience,Salary。当我在邮递员中单独提出相同的查询时,它正在工作。请仔细阅读我的代码,让我知道我哪里出错,因为这是我的第一个弹性查询,因此有很多改进的范围。 如果有更好的方法可以在弹性

中分享您的观点

下面是我的搜索参数生成的JAVA代码和查询,但在搜索MaxScore,Experience,Salary等个别参数时,这不起作用。 我必须以什么方式创建映射和分析器才能使我的代码得到理想的结果?在范围查询中,匹配不符合查询

>

 {
  "bool" : {
    "must" : {
      "term" : {
        "sectionalscores.examTitleId" : 3
      }
    },
    "should" : [ {
      "range" : {
        "userdata.expectedSalary" : {
          "from" : 500000.0,
          "to" : 1000000.0,
          "include_lower" : true,
          "include_upper" : true
        }
      }
    }, {
      "range" : {
        "userdata.experienceInYears" : {
          "from" : 0,
          "to" : 1,
          "include_lower" : true,
          "include_upper" : true
        }
      }
    }, {
      "range" : {
        "userdata.higestQualPercentCgpa" : {
          "from" : 2014,
          "to" : 100.0,
          "include_lower" : true,
          "include_upper" : true
        }
      }
    }, {
      "range" : {
        "userdata.higestQualPassingYear" : {
          "from" : 2014,
          "to" : 2100,
          "include_lower" : true,
          "include_upper" : true
        }
      }
    }, {
      "range" : {
        "examscores.scorePercent" : {
          "from" : 70.0,
          "to" : 100,
          "include_lower" : true,
          "include_upper" : true
        }
      }
    }, {
      "match" : {
        "userdata.keySkills" : {
          "query" : "Tomcat Jboss",
          "type" : "phrase"
        }
      }
    }, {
      "range" : {
        "userdata.xPercentageCgpa" : {
          "from" : 80.0,
          "to" : 100,
          "include_lower" : true,
          "include_upper" : true
        }
      }
    }, {
      "range" : {
        "userdata.xIIPercentageCgpa" : {
          "from" : 90.0,
          "to" : 100,
          "include_lower" : true,
          "include_upper" : true
        }
      }
    }, {
      "range" : {
        "sectionalscores.scorePercent" : {
          "from" : 70.0,
          "to" : 100,
          "include_lower" : true,
          "include_upper" : true
        }
      }
    } ]
  }
}

这是JAVA代码

public List<String> getCandidatesbySearchParameters(RecruiterSearchParameters params) throws UnknownHostException {

        ElasticSearch elastic = ElasticSearch.Factory.getInstance();
        String examTakenFrom= "now-"+params.getDaysForlastExamTaken()+"d";
        elastic.addIndexes("km-test");
        elastic.addTypes("km-user");
        BoolQueryBuilder query = QueryBuilders.boolQuery();
        if (params.getCountry() != null && !params.getCountry().isEmpty())
            query.must(QueryBuilders.matchQuery("userdata.country", params.getCountry()));

        if (params.getExpectedSalaryMax() > 0 && params.getExpectedSalaryMin() > 0 && params.getExpectedSalaryMax()> params.getExpectedSalaryMin())
            query.should(QueryBuilders.rangeQuery("userdata.expectedSalary").lte(params.getExpectedSalaryMax()).gte(params.getExpectedSalaryMin()));

        if (params.getExperienceMax() > 0 && params.getExperienceMin() >= 0 && params.getExperienceMax() > params.getExperienceMin())
            query.should(QueryBuilders.rangeQuery("userdata.experienceInYears").lte(params.getExperienceMax()).gte(params.getExperienceMin()));

        if (params.getGender() != null && !params.getGender().isEmpty())
            query.must(QueryBuilders.termQuery("userdata.gender", params.getGender()));

        if (params.getHighestDegreePercent() > 0)
            query.should(QueryBuilders.rangeQuery("userdata.higestQualPercentCgpa").gte(params.getHighestQualPassingYear()).lte(100.0));

        if (params.getHighestQualPassingYear() > 0)
            query.should(QueryBuilders.rangeQuery("userdata.higestQualPassingYear").gte(params.getHighestQualPassingYear()).lte(2100));

        if (params.geMaxScore() > 0)
            query.should(QueryBuilders.rangeQuery("examscores.scorePercent").gte(params.getMaxScore()).lte(100));

        if (params.getKeySkills() != null && !params.getKeySkills().isEmpty())
            //query.should(QueryBuilders.multiMatchQuery("userdata.keySkills", params.getKeySkills().get(0),params.getKeySkills().get(1),params.getKeySkills().get(2)));
        query.should(QueryBuilders.matchPhraseQuery("userdata.keySkills","Tomcat Jboss"));

        if (params.getDaysForlastExamTaken() > 0)
            query.must(QueryBuilders.rangeQuery("examscores.examDate").from(examTakenFrom).to("now"));      

        if (params.getLocation() != null && !params.getLocation().isEmpty())
            query.must(QueryBuilders.termQuery("userdata.currentlocation", params.getLocation()));

        if (params.getPercentageX() > 0)
            query.should(QueryBuilders.rangeQuery("userdata.xPercentageCgpa").gte(params.getPercentageX()).lte(100));

        if (params.getPercentageXII() > 0)
            query.should(QueryBuilders.rangeQuery("userdata.xIIPercentageCgpa").gte(params.getPercentageXII()).lte(100));

        if (params.getExamtitleId() > 0)
            query.must(QueryBuilders.termQuery("sectionalscores.examTitleId", params.getExamtitleId()));

        if (params.getSectionalScore() > 0)
            query.should(QueryBuilders.rangeQuery("sectionalscores.scorePercent").gte(params.getSectionalScore()).lte(100));

        if (params.getVideoInterview() == true)
            query.must(QueryBuilders.existsQuery("userdata.videoIntroUrl"));

        //Make a list of Objects with fields title id and sectional score percentage
        for(Integer titleId : params.getSectionalScoresMap().keySet()){
            query.must(QueryBuilders.nestedQuery("sectionalscores", QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("sectionalscores.examTitleId",titleId ))
                    .must(QueryBuilders.rangeQuery("sectionalscores.scorePercent").gte(params.getSectionalScoresMap().get(titleId)).lte(100))));
        }
          elastic.addQuery(query);
        elastic.setPage(1);
          elastic.setSize(60); 
          SearchResponse sr = elastic.execute(); 
          SearchHit[] results =  sr.getHits().hits(); List<String> sourceAsString = new
          ArrayList<String>(); for(SearchHit hit : results){
          sourceAsString.add(hit.getSourceAsString()); }

        return sourceAsString;
    }

}

0 个答案:

没有答案