为什么数据不能通过elasticsearch获得?

时间:2018-06-15 19:54:41

标签: elasticsearch

弹性搜索版本6.2.4 我制作了弹性搜索环境并制作了这样的映射。

{
    "state":"open",
    "settings":{
        "index":{
            "number_of_shards":"5",
            "provided_name":"lara_cart",
            "creation_date":"1529082175034",
            "analysis":{
                "filter":{
                    "engram":{
                        "type":"edgeNGram",
                        "min_gram":"1",
                        "max_gram":"36"
                    },
                    "maxlength":{
                        "type":"length",
                        "max":"36"
                    },
                    "word_delimiter":{
                        "split_on_numerics":"false",
                        "generate_word_parts":"true",
                        "preserve_original":"true",
                        "generate_number_parts":"true",
                        "catenate_all":"true",
                        "split_on_case_change":"true",
                        "type":"word_delimiter",
                        "catenate_numbers":"true"
                    }
                },
                "char_filter":{
                    "normalize":{
                        "mode":"compose",
                        "name":"nfkc",
                        "type":"icu_normalizer"
                    },
                    "whitespaces":{
                        "pattern":"\s[2,]",
                        "type":"pattern_replace",
                        "replacement":"\u0020"
                    }
                },
                "analyzer":{
                    "keyword_analyzer":{
                        "filter":[
                            "lowercase",
                            "trim",
                            "maxlength"
                        ],
                        "char_filter":[
                            "normalize",
                            "whitespaces"
                        ],
                        "type":"custom",
                        "tokenizer":"keyword"
                    },
                    "autocomplete_index_analyzer":{
                        "filter":[
                            "lowercase",
                            "trim",
                            "maxlength",
                            "engram"
                        ],
                        "char_filter":[
                            "normalize",
                            "whitespaces"
                        ],
                        "type":"custom",
                        "tokenizer":"keyword"
                    },
                    "autocomplete_search_analyzer":{
                        "filter":[
                            "lowercase",
                            "trim",
                            "maxlength"
                        ],
                        "char_filter":[
                            "normalize",
                            "whitespaces"
                        ],
                        "type":"custom",
                        "tokenizer":"keyword"
                    }
                },
                "tokenizer":{
                    "engram":{
                        "type":"edgeNGram",
                        "min_gram":"1",
                        "max_gram":"36"
                    }
                }
            },
            "number_of_replicas":"1",
            "uuid":"5xyW07F-RRCuIJlvBufNbA",
            "version":{
                "created":"6020499"
            }
        }
    },
    "mappings":{
        "products":{
            "properties":{
                "sale_end_at":{
                    "format":"yyyy-MM-dd HH:mm:ss",
                    "type":"date"
                },
                "image_5":{
                    "type":"text"
                },
                "image_4":{
                    "type":"text"
                },
                "created_at":{
                    "format":"yyyy-MM-dd HH:mm:ss",
                    "type":"date"
                },
                "description":{
                    "analyzer":"keyword_analyzer",
                    "type":"text",
                    "fields":{
                        "autocomplete":{
                            "search_analyzer":"autocomplete_search_analyzer",
                            "analyzer":"autocomplete_index_analyzer",
                            "type":"text"
                        }
                    }
                },
                "sale_start_at":{
                    "format":"yyyy-MM-dd HH:mm:ss",
                    "type":"date"
                },
                "sale_price":{
                    "type":"integer"
                },
                "category_id":{
                    "type":"integer"
                },
                "updated_at":{
                    "format":"yyyy-MM-dd HH:mm:ss",
                    "type":"date"
                },
                "price":{
                    "type":"integer"
                },
                "image_1":{
                    "type":"text"
                },
                "name":{
                    "analyzer":"keyword_analyzer",
                    "type":"text",
                    "fields":{
                        "autocomplete":{
                            "search_analyzer":"autocomplete_search_analyzer",
                            "analyzer":"autocomplete_index_analyzer",
                            "type":"text"
                        },
                        "keyword":{
                            "analyzer":"keyword_analyzer",
                            "type":"text"
                        }
                    }
                },
                "image_3":{
                    "type":"text"
                },
                "categories":{
                    "type":"nested",
                    "properties":{
                        "parent_category_id":{
                            "type":"integer"
                        },
                        "updated_at":{
                            "type":"text",
                            "fields":{
                                "keyword":{
                                    "ignore_above":256,
                                    "type":"keyword"
                                }
                            }
                        },
                        "name":{
                            "analyzer":"keyword_analyzer",
                            "type":"text",
                            "fields":{
                                "autocomplete":{
                                    "search_analyzer":"autocomplete_search_analyzer",
                                    "analyzer":"autocomplete_index_analyzer",
                                    "type":"text"
                                }
                            }
                        },
                        "created_at":{
                            "type":"text",
                            "fields":{
                                "keyword":{
                                    "ignore_above":256,
                                    "type":"keyword"
                                }
                            }
                        },
                        "id":{
                            "type":"long"
                        }
                    }
                },
                "id":{
                    "type":"long"
                },
                "image_2":{
                    "type":"text"
                },
                "stock":{
                    "type":"integer"
                }
            }
        }
    },
    "aliases":[

    ],
    "primary_terms":{
        "0":1,
        "1":1,
        "2":1,
        "3":1,
        "4":1
    },
    "in_sync_allocations":{
        "0":[
            "clYoJWUKTru2Z78h0OINwQ"
        ],
        "1":[
            "MGQC73KiQsuigTPg4SQG4g"
        ],
        "2":[
            "zW6v82gNRbe3wWKefLOAug"
        ],
        "3":[
            "5TKrfz7HRAatQsJudKX9-w"
        ],
        "4":[
            "gqiblStYSYy_NA6fYtkghQ"
        ]
    }
}

我想使用autocomplete提交的建议搜索。 所以我添加了这样的文档。

{
    "_index":"lara_cart",
    "_type":"products",
    "_id":"19",
    "_version":1,
    "_score":1,
    "_source":{
        "id":19,
        "name":"Conqueror, whose.",
        "description":"I should think you'll feel it a bit, if you wouldn't mind,' said Alice: 'besides, that's not a regular rule: you invented it just missed her. Alice caught the flamingo and brought it back, the fight.",
        "category_id":81,
        "stock":79,
        "price":11533,
        "sale_price":15946,
        "sale_start_at":null,
        "sale_end_at":null,
        "image_1":"https://lorempixel.com/640/480/?56260",
        "image_2":"https://lorempixel.com/640/480/?15012",
        "image_3":"https://lorempixel.com/640/480/?14138",
        "image_4":"https://lorempixel.com/640/480/?94728",
        "image_5":"https://lorempixel.com/640/480/?99832",
        "created_at":"2018-06-01 16:12:41",
        "updated_at":"2018-06-01 16:12:41",
        "deleted_at":null,
        "categories":{
            "id":81,
            "name":"A secret, kept.",
            "parent_category_id":"33",
            "created_at":"2018-06-01 16:12:41",
            "updated_at":"2018-06-01 16:12:41",
            "deleted_at":null
        }
    }
}

之后,我尝试通过以下查询进行搜索。 但是,这个查询无法获得任何结果。 你知道怎么解决吗? 我认为原因是映射和设置原因。

{
    "query":{
        "bool":{
            "must":[
                {
                    "term":{
                        "name.autocomplete":"Conqueror"
                    }
                }
            ],
            "must_not":[

            ],
            "should":[

            ]
        }
    },
    "from":0,
    "size":10,
    "sort":[

    ],
    "aggs":{

    }
}

1 个答案:

答案 0 :(得分:0)

这只是因为你正在使用的字段被分析并且“term”不能支持查询

您可以在分析仪自动完成的字段上尝试“匹配”;可能是自动完成的一些基本知识,而n-gram将帮助您更好地理解这个问题。

e.g。 你定义了以下分析器:

PUT /my_index
{
    "settings": {
        "number_of_shards": 1, 
        "analysis": {
            "filter": {
                "autocomplete_filter": { 
                    "type":     "edge_ngram",
                    "min_gram": 1,
                    "max_gram": 20
                }
            },
            "analyzer": {
                "autocomplete": {
                    "type":      "custom",
                    "tokenizer": "standard",
                    "filter": [
                        "lowercase",
                        "autocomplete_filter" 
                    ]
                }
            }
        }
    }
}

之后,您可以使用以下请求测试自动完成:

GET /my_index/_analyze?analyzer=autocomplete
quick brown

在配置为abrove时,自动完成将为输入查询生成n-gram,边缘为1~20。请求的返回值为:

q
qu
qui
quic
quick
b
br
bro
brow
brown

众所周知,术语查询是一个查询,它将搜索完全包含查询世界的字段,就像mysql的where条件一样。