【问题标题】:Elastic Search fulltext search query and filtersElastic Search 全文搜索查询和过滤器
【发布时间】:2015-11-19 13:27:39
【问题描述】:

我想执行全文搜索,但我也想使用一个或多个可能的过滤器。我的文档的简化结构,用/things/_search?q=*foo*搜索时:

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
            {
                "_index": "things",
                "_type": "thing",
                "_id": "63",
                "_score": 1,
                "fields": {
                    "name": [
                        "foo bar"
                    ],
                    "description": [
                        "this is my description"
                    ],
                    "type": [
                        "inanimate"
                    ]
                }
            }
        ]
    }
}

这很好用,但是如何将过滤器与查询结合起来?假设我想在包含多个文档的索引中搜索“foo”,但我只想获取 type == "inanimate" 的那些?

这是我目前的尝试:

{
    "query": {
        "filtered": {
            "query": {
                "query_string": {
                    "query": "*foo*"
                }
            },
            "filter": {
                "bool": {
                    "must": {
                        "term": { "type": "inanimate" }
                    }
                }
            }
        }
    }
}

当我删除 filter 部分时,它会返回一组准确的文档命中。但是使用此过滤器定义它不会返回任何内容,即使我可以手动验证是否存在带有type == "inanimate" 的文档。

【问题讨论】:

  • 你把type_type搞混了吗??
  • 你如何索引你的数据?
  • 我不确定你的意思,但我使用 logstash-input-jdbc 来索引 MySQL 数据库中的数据?
  • 你能把curl -XGET 'http://localhost:9200/your_index/_mapping/your_type的输出贴出来
  • 很明显这个例子被模拟为不显示真实数据。但是字段“type”是类型(heh)“string”。我没有做过任何“显式映射”。

标签: elasticsearch


【解决方案1】:

由于您尚未进行显式映射,term 查询正在寻找精确匹配。您需要将 "index : not_analyzed" 添加到 type 字段,然后您的查询将起作用。

这将为您提供正确的文件

{
  "query": {
    "match": {
      "type": "inanimate"
    }
  }
}

但这不是解决方案,您需要按照我所说的进行显式映射。

【讨论】:

  • 谢谢!我设置了显式映射,它工作得很好!
猜你喜欢
  • 1970-01-01
  • 2020-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多