我的查询:

POST /testqueryidx/testQuery/_search
{
  "size" : 10,
  "query" : {
    "bool" : {
      "must" : [ {
         "multi_match": {
          "query": "sales*",
          "fields": ["skills"]
      }
     }, {
          "query_string" : {
          "query" : "jay12",
          "fields" : [ "idNum" ]
         }
      } ]
    }
  },
 "aggregations" : {
    "aggs" : {
      "terms" : {
          "field" : "skills_sort",
           "size" : 0,
           "order" : {
               "_term" : "asc"
               }
            }
       }
   }
}

查询结果:
{
    "took": 3,
    "timed_out": false,
     "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
 },
     "hits": {
  "total": 1,
   "max_score": 0.9734945,
   "hits": [
    {
    "_index": "testqueryidx",
    "_type": "testQuery",
    "_id": "56909fbdaecb813e8c64e1e8",
    "_score": 0.9734945,
    "_source": {
       "skills": [
          "Account Management",
          "Sales force",
          "Adobe Creative Suite"
       ],
       "_id": "56909fbdaecb813e8c64e1e8",
       "idNum": "jay12"
    }
  }
 ]
},
  "aggregations": {
  "aggs": {
  "doc_count_error_upper_bound": 0,
  "sum_other_doc_count": 0,
  "buckets": [
     {
        "key": "Account Management",
        "doc_count": 1
     },
     {
        "key": "Adobe Creative Suite",
        "doc_count": 1
     },
     {
       "key": "Sales force",
       "doc_count": 1
     }
   ]
  }
 }
}

在这里,我搜索了现场技能中的关键字“销售”,并找到了匹配的文档。您可以在下面看到一个匹配的示例:
"skills": [
             "Account Management",
             "Sales force",
             "Adobe Creative Suite"
          ],

但是我不希望查询结果中以及查询聚合中使用“帐户管理”和“Adob​​e Creative Suite”。请参阅以下汇总结果:
"buckets": [
        {
           "key": "Account Management",
           "doc_count": 1
        },
        {
           "key": "Adobe Creative Suite",
           "doc_count": 1
        },
        {
           "key": "Sales force",
          "doc_count": 1
        }
     ]

以同样的方式,我不想在“key”:“Account Management” 和“ key”:“Adob​​e Creative Suite” 以上的聚合结果中,因为我只搜索sales *。

我之所以能看到以上突出显示的文字,是因为文档中的“技能”字段具有这三个技能,但是我只对搜索到的关键字感兴趣。如果有人对此有解决方案,请帮助我。

最佳答案

我认为这是可以实现的。您可以使用include进行术语汇总,这只会给您sales*。就查询而言,您必须使用highlight来仅获取任何字段的特定值,并且可以使用source filtering检索其他值。这是我的设置

POST only_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "skills": {
          "type": "string",
          "fields": {
            "raw": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        },
        "idNum" : {
          "type" : "string"
        }
      }
    }
  }
}

为您的文档建立索引后,我运行以下查询
GET only_index/_search
{
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "sales*",
            "fields": [
              "skills"
            ]
          }
        },
        {
          "query_string": {
            "query": "jay12",
            "fields": [
              "idNum"
            ]
          }
        }
      ]
    }
  },
  "aggregations": {
    "aggs": {
      "terms": {
        "field": "skills.raw",
        "size": 0,
        "include": "(?i)sales.*",
        "order": {
          "_term": "asc"
        }
      }
    }
  },
  "highlight": {
    "fields": {
      "skills": {}
    }
  },
  "_source": [
    "idNum"
  ]
}

我已经将(?i)标志用于case insensitive match。这就是我得到的
"hits": {
      "total": 1,
      "max_score": 0.29834434,
      "hits": [
         {
            "_index": "only_index",
            "_type": "my_type",
            "_id": "1",
            "_score": 0.29834434,
            "_source": {
               "idNum": "jay12"
            },
            "highlight": {
               "skills": [
                  "<em>Sales</em> force"
               ]
            }
         }
      ]
   },
   "aggregations": {
      "aggs": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "Sales force",
               "doc_count": 1
            }
         ]
      }
   }

希望这可以帮助!!

10-07 11:59
查看更多