如何通过elasticsearch搜索嵌套过滤器

时间:2016-03-17 17:58:14

标签: elasticsearch

我想使用[嵌套过滤器]功能。 因为,我只想过滤嵌套数据。

例如,如果ElasticSearch拥有此数据。 我想得到它。但我尝试使用[嵌套过滤器],我无法获得这些数据。 你知道好的解决方案吗?

实际上,我想在嵌套类型中有2个条件。 这就像sql, select * from document Inner Join on document_comments.document_id = document.id,其中document_comments.deleted_at为null,document_comments.comment为' test'

[数据]

"_source": {
"id": 4,
"name": "hogehoge.csv",
"deleted_at": null,
"hard_delete": null,
"document_comments": [
{
"id": 8,
"comment": "test",
"document_id": 4,
"deleted_at": "2016-03-03T13:43:10"
}
,
{
"id": 11,
"comment": "test",
"document_id": 4,
"deleted_at": null
}
]
}

[映射]

 "documents": {
"search_analyzer": "default_search",
"dynamic_templates": [
  {
    "string_template": {
      "mapping": {
        "type": "multi_field",
        "fields": {
          "ja": {
            "analyzer": "ja_analyzer",
            "index": "analyzed",
            "type": "string"
          },
          "{name}": {
            "analyzer": "ngram_analyzer",
            "index": "analyzed",
            "type": "string"
          },
          "yomi": {
            "analyzer": "yomi_analyzer",
            "index": "analyzed",
            "type": "string"
          },
          "full": {
            "index": "not_analyzed",
            "type": "string"
          }
        }
      },
      "match_mapping_type": "string",
      "match": "*"
    }
  }
],
"properties": {
  "@timestamp": {
    "format": "dateOptionalTime",
    "type": "date"
  },
  "document_comments": {
    "type": "nested",
    "properties": {
      "deleted_at": {
        "format": "dateOptionalTime",
        "type": "date"
      },
      "document_id": {
        "type": "integer"
      },
      "comment": {
        "index": "no",
        "type": "string",
        "fields": {
          "ja": {
            "analyzer": "ja_analyzer",
            "type": "string"
          },
          "yomi": {
            "analyzer": "yomi_analyzer",
            "type": "string"
          },
          "ngram": {
            "analyzer": "ngram_analyzer",
            "type": "string"
          },
          "full": {
            "index": "not_analyzed",
            "type": "string"
          }
        }
      },
      "id": {
        "type": "long"
      },
      "deleted_at": {
        "format": "dateOptionalTime",
        "type": "date"
      }
    }
  },
  "name": {
    "index": "no",
    "type": "string",
    "fields": {
      "ja": {
        "analyzer": "ja_analyzer",
        "type": "string"
      },
      "yomi": {
        "analyzer": "yomi_analyzer",
        "type": "string"
      },
      "ngram": {
        "analyzer": "ngram_analyzer",
        "type": "string"
      },
      "full": {
        "index": "not_analyzed",
        "type": "string"
      }
    }
  },
  "delete_at": {
    "format": "dateOptionalTime",
    "type": "date"
  },
  "id": {
    "type": "integer"
  },
  "hard_delete": {
    "format": "dateOptionalTime",
    "type": "date"
  }
}

}

[查询]

{
  "_source": [
    "id",
    "deleted_at",
    "document_comments.comment",
    "document_comments.deleted_at"
  ],
  "min_score": 0.05,
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "must": [],
          "should": [
            {
              "multi_match": {
                "query": "test",
                "type": "cross_fields",
                "fields": [
                  "document.name.ja"
                ]
              }
            },
            {
              "nested": {
                "path": "document_comments",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "multi_match": {
                          "query": "test",
                          "type": "cross_fields",
                          "fields": [
                            "document_comments.comment.ja"
                          ]
                        }
                      }
                    ],
                    "must_not": [
                      {
                        "filter": {
                          "exists": {
                            "field": "document_comments.deleted_at"
                          }
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      },
      "filter": {
        "bool": {
          "must": [
            [
              {
                "missing": {
                  "field": "deleted_at",
                  "existence": "true",
                  "null_value": "true"
                }
              },
              {
                "missing": {
                  "field": "hard_delete",
                  "existence": "true",
                  "null_value": "true"
                }
              }
            ],
            {
              "type": {
                "value": "document"
              }
            },
            {
              "term": {
                "id": "3"
              }
            }
          ]
        }
      }
    }
  },
  "sort": [
    {
      "id": "desc"
    },
    {
      "_score": "asc"
    }
  ]
}

0 个答案:

没有答案