Elasticsearch数组(标签/标签查询

时间:2016-02-09 16:02:40

标签: elasticsearch

我真的认为我试图做的很简单。我只是试图查询N标签。在#34; Elasticsearch: How to use two different multiple matching fields?"中提出并回答了一个明显的例子。然而,这个解决方案似乎并不适用于最新版本的ES(更可能的是,我只是做错了)。

要显示当前数据并演示有效查询,请参阅以下内容:

{
    "query": {
        "filtered": {
             "filter": { 
                 "terms": { 
                    "Price": [10,5]
                }
            }
        }
    }
}

以下是此结果。如您所见,5和10正在显示(这表明基本查询确实有效):

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 6,
    "successful" : 6,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "labelsample",
      "_type" : "entry",
      "_id" : "AVLGnGMYXB5vRcKBZaDw",
      "_score" : 1.0,
      "_source" : {
        "Category" : [ "Medium Signs" ],
        "Code" : "a",
        "Name" : "Sample 1",
        "Timestamp" : 1.455031083799152E9,
        "Price" : "10",
        "IsEnabled" : true
      }
    }, {
      "_index" : "labelsample",
      "_type" : "entry",
      "_id" : "AVLGnGHHXB5vRcKBZaDF",
      "_score" : 1.0,
      "_source" : {
        "Category" : [ "Small Signs" ],
        "Code" : "b",
        "Name" : "Sample 2",
        "Timestamp" : 1.45503108346191E9,
        "Price" : "5",
        "IsEnabled" : true
      }
    }, {
      "_index" : "labelsample",
      "_type" : "entry",
      "_id" : "AVLGnGILXB5vRcKBZaDO",
      "_score" : 1.0,
      "_source" : {
        "Category" : [ "Medium Signs" ],
        "Code" : "c",
        "Name" : "Sample 3",
        "Timestamp" : 1.455031083530215E9,
        "Price" : "10",
        "IsEnabled" : true
      }
    }, {
      "_index" : "labelsample",
      "_type" : "entry",
      "_id" : "AVLGnGGgXB5vRcKBZaDA",
      "_score" : 1.0,
      "_source" : {
        "Category" : [ "Medium Signs" ],
        "Code" : "d",
        "Name" : "Sample 4",
        "Timestamp" : 1.4550310834233E9,
        "Price" : "10",
        "IsEnabled" : true
      }
    }]
  }
}

作为旁注:以下bool查询给出了完全相同的结果:

{
    "query": {
        "bool": {
            "must": [{
                "terms": {
                    "Price": [10,5]
                }
            }]
        }
    }
}

注意Category ...

让我们简单地将类别复制/粘贴到查询中:

{
    "query": {
        "filtered": {
             "filter": { 
                 "terms": { 
                    "Category" : [ "Medium Signs" ]
                }
            }
        }
    }
}

这给出了以下宝石:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 6,
    "successful" : 6,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

同样,这里是bool查询版本,它提供相同的0-hit结果:

{
    "query": {
        "bool": {
            "must": [{
                "terms": {
                    "Category" : [ "Medium Signs" ]
                }
            }]
        }
    }
}

最后,我肯定需要类似"Category" : [ "Medium Signs", "Small Signs" ]工作的东西(与其他标签查询和minimum_should_match一致) - 但我甚至无法做到这一点-bones查询工作)。

我不知道为什么会这样。我倾倒了文档的时间,尝试了我能看到的一切。我是否需要调试各种编码?我的语法是古老的吗?

1 个答案:

答案 0 :(得分:1)

这里的问题是ElasticSearch正在分析和改进类别字段,并且术语过滤器期望完全匹配。这里的一个解决方案是在条目映射中向Category添加原始字段:

PUT labelsample
{
  "mappings": {
    "entry": {
      "properties": {
        "Category": {
          "type": "string",
          "fields": {
            "raw": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        },
        "Code": {
          "type": "string"
        },
        "Name": {
          "type": "string"
        },
        "Timestamp": {
          "type": "date",
          "format": "epoch_millis"
        },
        "Price": {
          "type": "string"
        },
        "IsEnabled": {
          "type": "boolean"
        }
      }
    }
  }
}

...并在原始字段上过滤:

GET labelsample/entry/_search
{
    "query": {
        "filtered": {
             "filter": { 
                 "terms": { 
                    "Category.raw" : [ "Medium Signs" ]
                }
            }
        }
    }
}