在弹性搜索中执行AND查询

时间:2016-01-19 20:55:11

标签: elasticsearch nest

我曾尝试寻找另一种解决方案,但ES中的Bool query似乎没有达到我想要的效果。或者我只是没有正确使用它。

在我们当前的搜索实现中,我们试图通过更改查询逻辑来提高每个查询的性能/减少内存占用。今天,如果你搜索" The Red Ball"你可能会收到500万份文件,因为ES会返回任何符合""或者"红色"或者"球"这意味着我们回到WAAAAAY太多无关的文件(主要是因为""术语)。我想更改我们的查询,而不是使用 AND ,因此ES只返回匹配"" AND" red" AND" ball"。

我使用NEST Client使用C#执行此操作,因此使用客户端的示例最好,因为这似乎是我无法弄清楚要做什么的地方。感谢

2 个答案:

答案 0 :(得分:0)

您只需将query string queryAND operator一起使用。

{
  "query": {
    "query_string": {
      "default_field": "your_field", <--- remove this if you want to search on all fields
      "query": "the red ball",
      "default_operator": "AND"
    }
  }
}

或只是

{
  "query": {
    "query_string": {
      "query": "the AND red AND ball"
    }
  }
}

我不知道C#,但这就是它在nest中的样子(每个人都可以自由编辑)

client.Search<your_index>(q => q
    .Query(qu => qu
    .QueryString(qs=>qs
        .OnField(x=>your_field).Query("the AND red AND ball")
        )
    )
);

答案 1 :(得分:0)

我找到了使用NEST客户端的相应查询:

    func tableView(tableView: UITableView, cellForRowAtIndexPath         indexPath: NSIndexPath) -> UITableViewCell {
    let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell")!
    var dict = datas[indexPath.row]
    cell.textLabel?.text = dict["userId"] as? String
    cell.detailTextLabel?.text = dict["id"] as? String
    return cell
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return datas.count
}

其中SearchDescriptor<BackupEntitySearchDocument> desc = new SearchDescriptor<BackupEntitySearchDocument>(); desc.Query(qq => qq.MultiMatch(m => m.OnFields(_searchFields).Query(query).Operator(Operator.And))); var searchResp = await _client.SearchAsync<BackupEntitySearchDocument>(desc).ConfigureAwait(false); _searchFields,其中包含要匹配的字段,List<string>是要搜索的字词。

相关问题