Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 在嵌套集合中查找时,Elasticsearch排除空字符串_String_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Search - Fatal编程技术网 elasticsearch,search,String,elasticsearch,Search" /> elasticsearch,search,String,elasticsearch,Search" />

String 在嵌套集合中查找时,Elasticsearch排除空字符串

String 在嵌套集合中查找时,Elasticsearch排除空字符串,string,elasticsearch,search,String,elasticsearch,Search,我试图做一个看似简单的弹性查询,但我失败了,我真的找不到关于这个主题的任何东西 我有以下模型(我们称之为响应) 某些答案可能没有实际答案(即答案字段为空字符串) 我有两个我有问题的场景 我无法解决的第一种情况是,我必须找到所有类型为type=1的嵌套对象,并且在“答案”字段中有一些内容。我提出了这个查询这个选项,它为我提供了至少有一个答案类型为1的所有答案,包括答案为空字符串的答案,我想排除这些答案 { "query": { "nested": { "path": "su

我试图做一个看似简单的弹性查询,但我失败了,我真的找不到关于这个主题的任何东西

我有以下模型(我们称之为响应)

某些答案可能没有实际答案(即答案字段为空字符串)

我有两个我有问题的场景

我无法解决的第一种情况是,我必须找到所有类型为
type=1
的嵌套对象,并且在“答案”字段中有一些内容。我提出了这个查询这个选项,它为我提供了至少有一个答案类型为1的所有答案,包括答案为空字符串的答案,我想排除这些答案

{
  "query": {
    "nested": {
      "path": "surveyResponseAnswers",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "surveyResponseAnswers.questionType": 1
              }
            }
          ]
        }
      }
    }
  }
}
我试着添加一个

"must_not": [{
  "match": {
    "surveyResponseAnswers.answer": ""
  }
]
在与
必须
相同的级别上,但我认为它会给我响应父级,因为还有其他答案在answer属性中有一个值,对此我不在乎

我的第二种情况是,我必须找到类型为
type=1
的所有嵌套对象,并且应答字段包含另一个字符串/子字符串,我为其提出了以下查询,但这只会为匹配完整搜索词的应答提供结果

{
  "query": {
    "nested": {
      "path": "answers",
      "query": {
        "bool": {
          "must": [{
            "match": {
              "answers.type": 1
            }
          }, {
            "match": {
              "answers.answer": "text"
            }
          }]
        }
      }
    }
  }
}

对于第一个问题,请尝试使用“常量_分数””然后进行筛选。您的查询将如下所示

{
    "query": {
      "nested": {
        "path": "surveyResponseAnswers",
        "query": {
          "bool": {
            "must": [
              {
                "match": {
                  "surveyResponseAnswers.questionType": 1
                }
              },
              {
                "constant_score": {
                  "filter": {
                    "exists": {
                      "field": "surveyResponseAnswers.answer"
                    }
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
对于第二个问题,如果要搜索整个单词,请尝试使用“术语””,或者根据需要使用“前缀”

有关更多详细信息,请参阅

{
    "query": {
      "nested": {
        "path": "surveyResponseAnswers",
        "query": {
          "bool": {
            "must": [
              {
                "match": {
                  "surveyResponseAnswers.questionType": 1
                }
              },
              {
                "constant_score": {
                  "filter": {
                    "exists": {
                      "field": "surveyResponseAnswers.answer"
                    }
                  }
                }
              }
            ]
          }
        }
      }
    }
  }