【问题标题】:ElasticSearch: search inside the array of objectsElasticSearch:在对象数组中搜索
【发布时间】:2015-09-12 18:19:14
【问题描述】:

我在查询数组中的对象时遇到问题。 让我们创建一个非常简单的索引,添加一个带有一个字段的类型并添加一个带有对象数组的文档(我使用感觉控制台):

PUT /test/
PUT /test/test/_mapping
{
    "test": {
        "properties": {
            "parent": {"type": "object"}
        }
    }
}
POST /test/test
{
    "parent": [
        {
            "name": "turkey",
            "label": "Turkey"
        },
        {
            "name": "turkey,mugla-province",
            "label": "Mugla (province)"
        }
    ]
}

现在我想同时搜索名称 "turkey""turkey,mugla-province" 。第一个查询工作正常:

GET /test/test/_search {"query":{ "term": {"parent.name": "turkey"}}}

但是第二个什么都不返回:

GET /test/test/_search {"query":{ "term": {"parent.name": "turkey,mugla-province"}}}

我尝试了很多东西,包括:

"parent": {
    "type": "nested",
    "include_in_parent": true,
    "properties": {
         "label": {
             "type": "string",
             "index": "not_analyzed"
         },
         "name": {
             "type": "string",
             "store": true
         }
     }
}

但没有任何帮助。我错过了什么?

【问题讨论】:

  • 您是否要搜索与 turkey AND mugla-province 或 turkey OR mugla-province 匹配的文档?

标签: elasticsearch search-engine


【解决方案1】:

这是一种方法,您可以使用 nested docs:

我定义了一个这样的索引:

PUT /test_index
{
   "mappings": {
      "doc": {
         "properties": {
            "parent": {
               "type": "nested",
               "properties": {
                  "label": {
                     "type": "string"
                  },
                  "name": {
                     "type": "string"
                  }
               }
            }
         }
      }
   }
}

为您的文档编制索引:

PUT /test_index/doc/1
{
   "parent": [
      {
         "name": "turkey",
         "label": "Turkey"
      },
      {
         "name": "turkey,mugla-province",
         "label": "Mugla (province)"
      }
   ]
}

那么这些查询中的任何一个都会返回它:

POST /test_index/_search
{
    "query": {
        "nested": {
           "path": "parent",
           "query": {
               "match": {
                  "parent.name": "turkey"
               }
           }
        }
    }
}

POST /test_index/_search
{
    "query": {
        "nested": {
           "path": "parent",
           "query": {
               "match": {
                  "parent.name": "turkey,mugla-province"
               }
           }
        }
    }
}

这是我使用的代码:

http://sense.qbox.io/gist/6258f8c9ee64878a1835b3e9ea2b54e5cf6b1d9e

【讨论】:

  • 感谢重播,但我需要完全匹配。抱歉没有指定。
  • “完全匹配”是什么意思?这种技术也适用于terms 查询/过滤器,您可能只需要在映射字段上启用"index": "not_analzyed"。我可以更新以表明它是否有帮助。
  • "index": "not_analzyed" - 这就是我需要的!我已经指出其他答案是正确的,但是您的套件更适合我。我将使用此解决方案,感谢您的宝贵时间!
【解决方案2】:

要搜索多个术语,请使用术语查询而不是术语查询。

"terms" : {
        "tags" : [ "turkey", "mugla-province" ],
        "minimum_should_match" : 1
    }

有多种方法可以构造这个查询,但这是当前版本的 ElasticSearch (1.6) 中最简单、最优雅的方法

【讨论】:

  • GET /test/test/_search { "query":{ "terms" : { "parent.name" : ["turkey", "turkey,mugla-province" ] } } } - 这个很好用!谢谢,我想知道我能以某种方式指出没有“火鸡”的唯一一个术语“火鸡,穆拉省”吗?
  • 您可以通过简单地说明 minimum_should_match : 2 来匹配两者的文档。此外,您可能想检查您的分析器是否在 , 或 - 这是可能的,然后重试您的 Term 查询,或者您可以将嵌套文档与不同的分析器一起使用,有很多方法可以使用 ElasticSearch 完成特定的事情,但我恐怕我不明白你在找什么。
  • "[terms] 查询不支持[minimum_should_match]",在v7.5 terms 中每个term 的key 就是你要搜索的属性,例如:"terms":{"parent.name":[ "turkey", "mugla-province" ]}
猜你喜欢
  • 2015-12-05
  • 2018-07-05
  • 2012-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-21
  • 2011-04-07
  • 2013-09-03
相关资源
最近更新 更多