【问题标题】:Elasticsearch query with wildcard and match conditions带有通配符和匹配条件的 Elasticsearch 查询
【发布时间】:2018-02-27 13:52:04
【问题描述】:

我有这个索引:

    {
            "mappings": {
                "records" : {
                    "properties" : {
                        "suggest" : {
                            "type" : "completion",
                            "contexts": [
                                { 
                                    "name": "year",
                                    "type": "category",
                                    "path": "year"
                                }
                            ]
                        }
                    }
                }
            }
}

我放了一些记录:

POST http://localhost:9200/example/records
{
            "suggest": {
                "input": "foo123" ,
                "contexts": {
                    "year": "1999"
                }
            }
}

POST http://localhost:9200/example/records
{
            "suggest": {
                "input": "some123" ,
                "contexts": {
                    "year": "1999"
                }
            }
}
POST http://localhost:9200/example/records
{
            "suggest": {
                "input": "thing123" ,
                "contexts": {
                    "year": "2000"
                }
            }
}

现在我会做这个查询(类似sql):

SELECT * FROM example WHERE SUGGEST LIKE %123% AND YEAR=1999

如何在 Elastic Search 中进行操作?

我输入:

    POST http://localhost:9200/example/records/_search?pretty
    {
    "query": {
        "bool": {
            "must": [
                   { "wildcard" : { "suggest" : "*123*" } }

            ], 
            "filter":[
                { "term" : { "year" : "1999" } }
                ]
        }
    }
}

我已返回此响应,但结果为空白:

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

我希望返回这些记录:

  • foo123,1999 年
  • some123,1999 年

我该怎么办?

【问题讨论】:

    标签: regex elasticsearch elasticsearch-query


    【解决方案1】:

    如果你关心分数,你需要使用 bool query 和 must:

    {
        "query": {
            "bool": {
                "must": [
                    { "wildcard" : { "name" : "*foo*" } },
                    { "term" : { "year" : "1999" } }
                ]
            }
        }
    }
    

    如果您只想过滤值并可能缓存过滤器,则使用过滤器:

    {
        "query": {
            "filter": {
                "must": [
                    { "wildcard" : { "name" : "*foo*" } },
                    { "term" : { "year" : "1999" } }
                ]
            }
        }
    }
    

    【讨论】:

    • 我认为我做的不是很好……你能看到我编辑的带有索引的帖子吗? @slawek
    • 你已经完全改变了问题。建议使用不同的search API。我对它不够熟悉,无法帮助你。我不认为您可以使用建议器进行多条件搜索,但我可能会弄错。
    猜你喜欢
    • 2016-04-25
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 2017-11-30
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多