elasticsearch

时间:2015-10-13 06:36:19

标签: elasticsearch

我想更多地了解下一个情况。我试图将这样的文档编入索引:

PUT 'server/index/test/1'
{
    to: "to1",
    to: "to2",
    to: "to3"
}

看来,elasticsearch吞下了这份文件。好的,没问题。不过,我想知道一组数值之间的区别。

如何进行搜索?

编辑:

我已尝试过此搜索:

GET 'server/index/test/_search?q=to:to1&pretty'
GET 'server/index/test/_search?q=to:to2&pretty'
GET 'server/index/test/_search?q=to:to3&pretty'

和ES在每次执行后向我显示文档:

{
   "took" : 44,
   "timed_out" : false,
   "_shards" : {
       "total" : 5,
       "successful" : 5,
       "failed" : 0
   },
   "hits" : {
   "total" : 1,
   "max_score" : 1.0,
       "hits" : [ {
           "_index" : "index",
           "_type" : "test",
           "_id" : "1",
           "_score" : 1.0,
           "_source":
           {
               to: "to1",
               to: "to2",
               to: "to3"
           }
       } ]
    }
}

貌似,ES索引每个重复的字段值...它真的是这样吗?或者我表演或做错了什么?

1 个答案:

答案 0 :(得分:1)

在JSON文档中,您不能有两个具有相同名称的字段,但ES将默默“修复”您的文档,并仅索引单个to字段。此外,由于JSON文档基本上是一个本质上无序的映射,因此无法保证三个to字段中的哪一个被索引。

因此,如果您为该文档编制索引,它就可以使用,但您只会在其中看到一个to字段,这可能不是您想要的

PUT 'server/test/1'
{
    to: "to1",
    to: "to2",
    to: "to3"
}

GET 'server/test/1'
{
    to: "to3"            <--- could also be to1 or to2
}

但是,如果您将to设为值数组,则所有值都将被编入索引

PUT 'server/test/1'
{
    to: ["to1", "to2", "to3"]
}

GET 'server/test/1'
{
    to: ["to1", "to2", "to3"]
}