gpt4 book ai didi

elasticsearch - Elasticsearch多个搜索词

转载 作者:行者123 更新时间:2023-12-03 01:23:04 27 4
gpt4 key购买 nike

我已经设定了 flex 指标。我有100,000个文档,都包含以下字段

{
"Make": "NISSAN",
"Model": "FUGA",
"Body Type": "SEDAN",
"Year of Manufacture": 2012,
"Country": "JAPAN",
"Fuel Type": "PETROL"
}

我需要根据四个可能的术语进行搜索
  • 使
  • 模型
  • 生产年份
  • 燃料类型

  • 以下是搜索查询的四种可能组合
    2012 nissan fuga petrol nissan fuga 2012 petrol petrol 2012 nissan fuga nissan fuga petrol 2012
    假设我们对搜索查询的拼写正确,以下是我尝试根据搜索查询进行搜索的方式
    curl -X GET "localhost:9200/vehicles/_search?pretty" -H 'Content-Type: application/json' -d'
    {
    "query": {
    "simple_query_string" : {
    "query": "2012 NISSAN FUGA PETROL",
    "fields": ["Make","Model","Year of Manufacture","Fuel Type"]
    }
    }
    }

    出乎意料的是,搜索返回以下错误
        {
    "error": {
    "root_cause": [
    {
    "type": "query_shard_exception",
    "reason": "failed to create query: {\n \"simple_query_string\" : {\n \"query\" : \"2012 NISSAN FUGA PETROL\",\n \"fields\" : [\n \"Model^1.0\",\n \"Make^1.0\",\n \"Year of Manufacture^1.0\",\n \"Fuel Type^1.0\"\n ],\n \"flags\" : -1,\n \"default_operator\" : \"or\",\n \"analyze_wildcard\" : false,\n \"auto_generate_synonyms_phrase_query\" : true,\n \"fuzzy_prefix_length\" : 0,\n \"fuzzy_max_expansions\" : 50,\n \"fuzzy_transpositions\" : true,\n \"boost\" : 1.0\n }\n}",
    "index_uuid": "3vd2zOgHRIq3BUAJ_EATVQ",
    "index": "vehicles"
    }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
    {
    "shard": 0,
    "index": "vehicles",
    "node": "Xl_WpfXyTcuAi2uadgB4oA",
    "reason": {
    "type": "query_shard_exception",
    "reason": "failed to create query: {\n \"simple_query_string\" : {\n \"query\" : \"2012 NISSAN FUGA PETROL\",\n \"fields\" : [\n \"Model^1.0\",\n \"Make^1.0\",\n \"Year of Manufacture^1.0\",\n \"Fuel Type^1.0\"\n ],\n \"flags\" : -1,\n \"default_operator\" : \"or\",\n \"analyze_wildcard\" : false,\n \"auto_generate_synonyms_phrase_query\" : true,\n \"fuzzy_prefix_length\" : 0,\n \"fuzzy_max_expansions\" : 50,\n \"fuzzy_transpositions\" : true,\n \"boost\" : 1.0\n }\n}",
    "index_uuid": "3vd2zOgHRIq3BUAJ_EATVQ",
    "index": "vehicles",
    "caused_by": {
    "type": "number_format_exception",
    "reason": "For input string: \"NISSAN\""
    }
    }
    }
    ]
    },
    "status": 400
    }

    以下是有关我的Elastic版本的更多信息
    {
    "name": "salim-HP-EliteBook-840-G5",
    "cluster_name": "elasticsearch",
    "cluster_uuid": "mSWKP4G1TSSq9rI3Hc0f6w",
    "version": {
    "number": "7.5.1",
    "build_flavor": "default",
    "build_type": "tar",
    "build_hash": "3ae9ac9a93c95bd0cdc054951cf95d88e1e18d96",
    "build_date": "2019-12-16T22:57:37.835892Z",
    "build_snapshot": false,
    "lucene_version": "8.3.0",
    "minimum_wire_compatibility_version": "6.8.0",
    "minimum_index_compatibility_version": "6.0.0-beta1"
    },
    "tagline": "You Know, for Search"

    }

    以下是 vehicles索引的索引映射
        {
    "vehicles": {
    "mappings": {
    "_meta": {
    "created_by": "ml-file-data-visualizer"
    },
    "properties": {
    "Body Type": {
    "type": "keyword"
    },
    "Country": {
    "type": "keyword"
    },
    "Fuel Type": {
    "type": "keyword"
    },
    "Make": {
    "type": "keyword"
    },
    "Model": {
    "type": "text"
    },
    "Year of Manufacture": {
    "type": "long"
    }
    }
    }
    }
    }

    如何根据搜索条件进行成功搜索?

    最佳答案

    更新

    cross_fields token 过滤器的synonym

    一个工作示例:

    映射(已更新)

    PUT my_index
    {
    "mappings": {
    "properties": {
    "Make": {
    "type": "text"
    },
    "Model": {
    "type": "text"
    },
    "Body Type": {
    "type": "text"
    },
    "Year of Manufacture": {
    "type": "text",
    "fields": {
    "long": {
    "type": "long"
    }
    }
    },
    "Country": {
    "type": "text"
    },
    "Fuel Type": {
    "type": "text"
    }
    }
    },
    "settings": {
    "index": {
    "analysis": {
    "filter": {
    "my_syn_filt": {
    "type": "synonym",
    "synonyms": [
    "nisson,nissen => nissan",
    "foga => fuga"
    ]
    }
    },
    "analyzer": {
    "my_synonyms": {
    "filter": [
    "lowercase",
    "my_syn_filt"
    ],
    "tokenizer": "standard"
    }
    }
    }
    }
    }
    }

    索引少量文件
    PUT my_index/_doc/1
    {
    "Make": "NISSAN",
    "Model": "FUGA",
    "Body Type": "SEDAN",
    "Year of Manufacture": 2012,
    "Country": "JAPAN",
    "Fuel Type": "PETROL"
    }

    PUT my_index/_doc/2
    {
    "Make": "NISSAN",
    "Model": "FUGA",
    "Body Type": "SEDAN",
    "Year of Manufacture": 2013,
    "Country": "JAPAN",
    "Fuel Type": "PETROL"
    }

    PUT my_index/_doc/3
    {
    "Make": "FIAT",
    "Model": "FUGA",
    "Body Type": "SEDAN",
    "Year of Manufacture": 2014,
    "Country": "JAPAN",
    "Fuel Type": "PETROL"
    }

    搜索查询(已更新)
    GET my_index/_search
    {
    "query": {
    "multi_match": {
    "query": "NISSON FOGA 2012 PETROL", ---> nisson and foga
    "fields": ["Make","Model","Year of Manufacture","Fuel Type"],
    "type": "cross_fields",
    "operator": "and",
    "analyzer": "my_synonyms"
    }
    }
    }

    结果
    "hits" : [
    {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "1",
    "_score" : 1.2605431,
    "_source" : {
    "Make" : "NISSAN",
    "Model" : "FUGA",
    "Body Type" : "SEDAN",
    "Year of Manufacture" : 2012,
    "Country" : "JAPAN",
    "Fuel Type" : "PETROL"
    }
    }
    ]

    希望这可以帮助

    关于elasticsearch - Elasticsearch多个搜索词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59443091/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com