ElasticSearch:使用query_string的IN运算符查询

时间:2017-07-07 12:32:55

标签: elasticsearch

我想将记录与查询字符串进行匹配,因此我编写了以下查询,该工作正常:

{
  "query": {
    "query_string": {
      "fields": ["rollno","name"],
        "query": "*John*"
      }
  }
}

现在,除了匹配fields之外,我还想对另一个字段实施IN查询。我尝试了this查询,如下:

{
"query": {
    "query_string": {
        "fields": ["rollno", "name"],
        "query": "*John*"
    },
    "match": {
        "majorSubject": ["Biology", "Chemistry"]
    }
  }
}

我得到的只是search_parse_exception

如何进行此IN操作?

1 个答案:

答案 0 :(得分:2)

您需要使用bool/must查询才能合并两个子查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "fields": [
              "rollno",
              "name"
            ],
            "query": "*John*"
          }
        },
        {
          "terms": {
            "majorSubject": [
              "Biology",
              "Chemistry"
            ]
          }
        }
      ]
    }
  }
}

如果majorSubject字段是已分析的字符串,请改用小写字词:

{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "fields": [
              "rollno",
              "name"
            ],
            "query": "*John*"
          }
        },
        {
          "terms": {
            "majorSubject": [
              "biology",
              "chemistry"
            ]
          }
        }
      ]
    }
  }
}