在multi / all elasticsearch嵌套字段中动态搜索

问题描述 投票:1回答:1

我正在使用spring-data-elasticsearch 2.0.8和elasticsearch.2.2.0想要动态搜索嵌套对象。

基本上我的嵌套对象可能有很少的嵌套字段,但我想在所有这些字段中搜索 - 动态。例如,动物文档可以有3个字段来描述他:名称/大小/描述。

我想搜索所有这些,因为我的搜索终点只有一个'描述'自由文本选项。因此,当用户在其入口点键入“15”或“狗”时,搜索将检查“名称”,“大小”和“描述”字段,并将从中返回一些内容。

我的映射看起来像:

{
    "mappings": {
        "animal_doc": {
            "properties": {
                "animal_description": {
                    "type": "nested",
                    "include_in_parent": true,
                    "properties": {
                        "name": {
                            "type": "string"
                        },
                        "size": {
                            "type": "string"
                        },
                        "description": {
                            "type": "string"
                        }
                    }
                },
           }
      }
  }

我读过两个:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.htmlhttps://www.elastic.co/blog/managing-relations-inside-elasticsearch

因为这可以通过使用(spring-data-es)创建查询来解决:

boolQuery.must(QueryBuilders.nestedQuery("animal_description", 
addMatchQuery("animal_description."+field, value)));

因此,每次我将查询Object.nestedField:value但我不想每次都指定字段,并希望它是动态的 - 在嵌套的'animal_description'字段内 - 所有内部字段也将被搜索。

一个理想的解决方案看起来像:

boolQuery.must(QueryBuilders.nestedQueryGetAllFields("animal_description",value)));

谢谢!

elasticsearch nested spring-data spring-data-elasticsearch
1个回答
1
投票

根据您的描述,您似乎可能想要使用一般的query string。查询几个特定字段时。例如,假设您要搜索一般

动物描述

您的查询将如下所示:

GET /_search
{
    "query": {
        "query_string" : {
            "fields" : ["properties.size", "properties.name", "properties.description"],
            "query" : "animal description"
        }
    }
}

您使用此查询的选项非常广泛,我建议您查看here

© www.soinside.com 2019 - 2024. All rights reserved.