通过术语弹性​​搜索限制过滤器

时间:2016-03-02 16:41:09

标签: search elasticsearch

我会为每个术语设置一个大小限制,3个检索术语“标记”的结果,5个结果用于术语“档案”,1个结果用于术语“个性”。

我可以使用限制过滤器或其他解决方案吗?

{
    "_source":{
      "include":[
        "path",
        "type"
      ]
    },
    "query":{
      "bool":{
        "should":[
          {
            "match":{
              "title.acp":{
                "query":"car",
                "boost":10
              }
            }
          },
          {
            "match":{
              "title.acp":{
                "query":"car",
                "fuzziness":"AUTO",
                "prefix_length":3
              }
            }
          }
        ],
        "filter":[
          {
            "terms":{
              "type":[
                "tag",
                "dossier",
                "personality"
              ]
            }
          }
        ]
      }
    },
    "highlight":{
      "fields":{
        "title.acp":{}
      }
    }
  };

1 个答案:

答案 0 :(得分:0)

看起来像一个给定的标题'您希望每个类型的前x个文档都是' x'因type

而异

执行此操作的一种方法是使用聚合filtertop-hits

示例:

{
   "size": 0,
   "query": {
      "bool": {
         "should": [
            {
               "match": {
                  "title.acp": {
                     "query": "car",
                     "boost": 10
                  }
               }
            },
            {
               "match": {
                  "title.acp": {
                     "query": "car",
                     "fuzziness": "AUTO",
                     "prefix_length": 3
                  }
               }
            }
         ],
         "filter": [
            {
               "terms": {
                  "type": [
                     "tag",
                     "dossier",
                     "personality"
                  ]
               }
            }
         ]
      }
   },
   "aggs": {
      "tag": {
         "filter": {
            "term": {
               "type": "tag"
            }
         },
         "aggs": {
            "tag_top_hits": {
               "top_hits": {
                  "_source": {
                     "include": [
                        "path",
                        "type"
                     ]
                  },
                  "size": 3,
                  "highlight": {
                     "fields": {
                        "title.acp": {}
                     }
                  }
               }
            }
         }
      },
      "dossier": {
         "filter": {
            "term": {
               "type": "dossier"
            }
         },
         "aggs": {
            "dossier_top_hits": {
               "top_hits": {
                  "_source": {
                     "include": [
                        "path",
                        "type"
                     ]
                  },
                  "size": 5,
                  "highlight": {
                     "fields": {
                        "title.acp": {}
                     }
                  }
               }
            }
         }
      },
      "personality": {
         "filter": {
            "term": {
               "type": "personality"
            }
         },
         "aggs": {
            "personality_top_hits": {
               "top_hits": {
                  "_source": {
                     "include": [
                        "path",
                        "type"
                     ]
                  },
                  "size": 1,
                  "highlight": {
                     "fields": {
                        "title.acp": {}
                     }
                  }
               }
            }
         }
      }
   }
}
相关问题