如何索引包含内部对象过滤器的过滤器查询?

时间:2016-01-13 11:38:45

标签: elasticsearch

使用Elasticsearch 2.1.1

我有内部对象的文档:

{
    "level1": {
        "level2": 42
    }
}

我想注册在内部属性上应用过滤器的过滤器查询:

$ curl -XPUT http://localhost:9200/myindex/.percolator/myquery?pretty -d '{
    "query": {
        "filtered": {
            "filter": {
                "range": {
                    "level1.level2": {
                        "gt": 10
                    }
                }
            }
        }
    }
}'

它失败了,因为我没有映射:

{
  "error" : {
    "root_cause" : [ {
      "type" : "query_parsing_exception",
      "reason" : "Strict field resolution and no field mapping can be found for the field with name [level1.level2]",
      "index" : "myindex",
      "line" : 1,
      "col" : 58
    } ],
    "type" : "percolator_exception",
    "reason" : "failed to parse query [myquery]",
    "index" : "myindex",
    "caused_by" : {
      "type" : "query_parsing_exception",
      "reason" : "Strict field resolution and no field mapping can be found for the field with name [level1.level2]",
      "index" : "myindex",
      "line" : 1,
      "col" : 58
    }
  },
  "status" : 500
}

所以我重新开始,但这次我在之前添加了一个映射模板:

curl -XDELETE http://localhost:9200/_template/myindex
curl -XDELETE http://localhost:9200/myindex
curl -XPUT http://localhost:9200/_template/myindex?pretty -d 'x
{             
  "template": "myindex",
  "mappings" : {
    "mytype" : {
      "properties" : {
        "level1" : {
          "properties" : {
            "level2" : {
              "type" : "long"
            }
          }
        }
      }
    }
  }
}
'

我尝试再次注册我的过滤器查询:

curl -XPUT http://localhost:9200/myindex/.percolator/myquery?pretty -d '{
    "query": {
        "filtered": {
            "filter": {
                "range": {
                    "level1.level2": {
                        "gt": 10
                    }
                }
            }
        }
    }
}'

现在它成功了:

{
  "_index" : "myindex",
  "_type" : ".percolator",
  "_id" : "myquery",
  "_version" : 1,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "created" : true
}

我可以看到已创建的映射:

curl http://localhost:9200/myindex/_mapping?pretty
{
  "myindex" : {
    "mappings" : {
      ".percolator" : {
        "properties" : {
          "query" : {
            "type" : "object",
            "enabled" : false
          }
        }
      },
      "mytype" : {
        "properties" : {
          "level1" : {
            "properties" : {
              "level2" : {
                "type" : "long"
              }
            }
          }
        }
      }
    }
  }
}

现在我的问题是我还需要在我的过滤器查询和the default percolate mapping doesn’t index the query field上执行搜索。

所以我再次开始,这次在我的映射模板中指定我希望将过滤器查询编入索引(注意"enabled": true):

curl -XPUT http://localhost:9200/_template/myindex?pretty -d ' 
{
  "template": "myindex",
  "mappings" : {
    ".percolator" : {
      "properties" : {
        "query" : {
          "type" : "object",
          "enabled" : true
        }
      }
    },
    "mytype" : {
      "properties" : {
        "level1" : {
          "properties" : {
            "level2" : {
              "type" : "long"
            }
          }
        }
      }
    }
  }
}
'

我尝试再次注册我的过滤器查询:

curl -XPUT http://localhost:9200/myindex/.percolator/myquery?pretty -d '{
    "query": {
        "filtered": {
            "filter": {
                "range": {
                    "level1.level2": {
                        "gt": 10
                    }
                }
            }
        }
    }
}'

但现在我收到了一个错误:

{
  "error" : {
    "root_cause" : [ {
      "type" : "mapper_parsing_exception",
      "reason" : "Field name [level1.level2] cannot contain '.'"
    } ],
    "type" : "mapper_parsing_exception",
    "reason" : "Field name [level1.level2] cannot contain '.'"
  },
  "status" : 400
}

如何创建索引与内部属性匹配的过滤器查询?

0 个答案:

没有答案