elasticsearch在一次调用中运行多个不同的查询

时间:2014-10-23 10:03:44

标签: angularjs elasticsearch

我想在elasticsearch服务上进行一次调用,运行3个不同的查询(在同一个索引上)。

这可能吗?


更详细的问题:

我在angularjs中创建一个自动完成功能,搜索3个字段并显示它们: " T_FAMILY" +" T_GENUS" +" T_SCIENTIFICNAME"。

我只做了一个查询:

"query" : { "query_string" : { "query" : "T_FAMILY:" +value +" or T_GENUS:"+value+ " or T_SCIENTIFICNAME:"+value} }
// value is the user input and can contain wildcard *

但不是重要的结果。

现在我想制作3个不同的查询并按分数对每个查询进行排序。最后获得3个结果和 将它们合并到一个数组中并按分数排序(我通过我的addKeyword()函数执行此操作)。

var keywords = [];
keywords.push(val);
$scope.isHide.logo=true;
return elasticQuery.search({
 index: $scope.domaine,
 size: 20,
 _source: false,
 body: {
    "fields" : ["T_FAMILY","T_GENUS","T_SCIENTIFICNAME"],
    "query": { "bool" : {"must" : [{"wildcard" : { "T_FAMILY" : val }}]}},
    "sort" : { "_score" : "desc" }
 }
}).then(function (response){
    addKeyword(response,keywords);
    return elasticQuery.search({
       index: $scope.domaine,
       size: 20,
       _source: false,
       body: {
          "fields" : ["T_FAMILY","T_GENUS","T_SCIENTIFICNAME"],
          "query": {"bool" : {"must" : [{"wildcard" : { "T_GENUS" : val }}]}},
          "sort" : { "_score" : "desc" }
       }
    }).then(function (response) {
       addKeyword(response,keywords);
       return elasticQuery.search({
          index: $scope.domaine,
          size: 20,
          _source: false,
          body: {
             "fields" : ["T_FAMILY","T_GENUS","T_SCIENTIFICNAME"],
             "query": {"bool" : {"must" : [{"wildcard" : { "T_SCIENTIFICNAME" : val }}]}},
             "sort" : { "_score" : "desc" }
          }
       }).then(function (response) {
          addKeyword(response,keywords);
          return keywords;
       });

       return keywords;
    });
});

我没有找到任何帮助,所以我在我的js代码中对弹性传感器进行了3次imcoicated调用,但这不是最好的方法。

谢谢

1 个答案:

答案 0 :(得分:0)

可以在elasticsearch中使用“dismax”运行不同的查询:

return elasticQuery.search({
     index: $scope.domaine,
     size: 20,
     _source: false,
     body: {
        //"min_score" : 0.50,
        "fields" : ["T_FAMILY","T_GENUS","T_SCIENTIFICNAME"],
        "highlight": {"pre_tags": ["<strong>"], "post_tags": ["</strong>"], "fields": { "T_FAMILY": {},"T_GENUS" : {}, "T_SCIENTIFICNAME": {} }},
        "query" : {
           "dis_max" : {
              "tie_breaker" : 0.0,
              "boost" : 1.0,
              "queries" : [
                 {"wildcard" : { "T_FAMILY" : val }},
                 {"wildcard" : { "T_GENUS" : val }},
                 {"wildcard" : { "T_SCIENTIFICNAME" : val }}
              ]
           }
        },
        "sort" : { "_score" : "desc" }
     }
  }).then(function (response){
    return addKeywords(response,keywords);
  });