使用弹性搜索查询字符串

时间:2018-03-29 07:40:02

标签: node.js amazon-web-services elasticsearch

我是弹性搜索的新手,我有以下格式的数据:

{
  "person_name" : "Abraham Benjamin deVilliers",
  "name": "Abraham",
  "office":{
     "name":"my_office"
  }
},
{
  "person_name" : "John Bradshaw",
  "name": "john",
  "office": {
     "name":"Abraham deVilliers"
  }
},
{
  "person_name" : "John Bradshaw",
  "name": "Abraham deVilliers",
  "office": {
     "name":"blabla"
  }
},
{
  "person_name" : "John Bradshaw",
  "name": "Abraham",
  "office": {
     "name":"deVilliers"
  }
},
{
  "person_name" : "Abraham",
  "name": "deVilliers",
  "office": {
     "name":"blabla"
  }
}

我正在使用搜索查询来匹配包含三个字段person_namenameoffice.name的字符串,这些字段应通过检查用户输入的查询字段来返回响应,如下所示:

如果输入的查询字符串为Abraham deVilliers,那么它应该返回同时包含AbrahamdeVilliers作为其person_namename或{{{}的一部分的用户1}}如下:

office.name

我尝试使用弹性搜索的 { "person_name" : "Abraham Benjamin deVilliers", "name": "Abraham", "office":{ "name":"my_office" } }, { "person_name" : "John Bradshaw", "name": "john", "office": { "name":"Abraham deVilliers" } }, { "person_name" : "John Bradshaw", "name": "Abraham deVilliers", "office": { "name":"blabla" } } 搜索查询,如下所示:

query_string

搜索查询会返回预期结果,但它也会返回{ "query": { "query_string": { "fields": ["person_name", "name", "office.name"], "query": "Abraham AND deVilliers" } } } 的{​​{1}}和first_name: Abraham name的回复,我不想要这样做。

我希望通过last_name: deVilliers查询

排除以下个人资料
office.name

1 个答案:

答案 0 :(得分:1)

这是多匹配查询的工作,尝试这样的事情:

GET <index_name>/_search?explain=true
{
  "query": {
    "multi_match" : {
      "query":      "Abraham deVilliers",
      "type":       "best_fields",
      "fields":     [ "person_name", "name", "office.name" ],
      "operator":"and"
    }
  }
}