elasticsearch - 按文档类型查询特定字段

标签 elasticsearch

我目前正在使用Elasticsearch,并且正在尝试构建一个搜索查询,以根据文档类型在不同的字段中查找关键字。

Example:

Index Name: index1

Document types:

  • doc1 (_id, name, size, color, weight)
  • doc2 (_id, name, duration, length, width, height)

现在,我想查询index1中的doc1doc2类型的文档,但是在doc1中,我只想在namesizeweight列中搜索,而在doc2中,我想在namelengthheight列中搜索。
如何在单个查询中实现?

最佳答案

您可以按类型和名称引用这些字段,并使用 bool(boolean) 查询。下面是一个基本示例。

{
    "query": {
        "bool": {
            "should": [
               {
                   "bool": {
                       "must": [
                          {
                              "term": {
                                 "doc1.name": {
                                    "value": "2"
                                 }
                              }
                          },
                          {
                              "term": {
                                 "doc1.size": {
                                    "value": "23"
                                 }
                              }
                          },
                          {
                              "term": {
                                 "doc1.weight": {
                                    "value": "52"
                                 }
                              }
                          }
                       ]
                   }
               },
               {
                   "bool": {
                       "must": [
                          {
                               "term": {
                                 "doc2.name": {
                                    "value": "2"
                                 }
                              }
                          },
                          {
                              "term": {
                                 "doc2.length": {
                                    "value": "23"
                                 }
                              }
                          },
                          {
                              "term": {
                                 "doc2.height": {
                                    "value": "52"
                                 }
                              }
                          }
                       ]
                   }
               }
            ]
        }
    }
}

关于elasticsearch - 按文档类型查询特定字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21757535/

相关文章:

elasticsearch - Elasticsearch应该在多个字段上进行运算

elasticsearch - spring-data-elasticsearch - 注册自定义分析器

javascript - 如何将 Kaggle 数据集添加到 elasticsearch 中?

docker - 应用程序日志文件到 ELK

elasticsearch - Elasticsearch中排序后的大小查询的并集

search - Lucene中同一存储位置上的多个索引器

elasticsearch - TFS 2017工作项搜索索引问题

elasticsearch - 检查文档是否是Elasticsearch查询的一部分?

elasticsearch - 在Elasticsearch中键入内容时检索不同的搜索值

json - 更新映射对我的索引不起作用(elasticsearch 1.4)