在elasticsearch

时间:2017-09-25 06:59:20

标签: elasticsearch

以下是可用的样本数据:

  • 租车
  • 为机场租车
  • 汽车租赁机场接机
  • 租车机场服务
  • 汽车服务机场
  • 机场汽车服务
  • 城市汽车服务

以下是我尝试的查询:

  1. 前缀,它仅为我们提供了开头+短语。 如果我在机场搜索汽车,我们将不会得到像#34;汽车租赁机场服务"或者租车去机场接机。

    示例查询:

    "query": {
     "bool": {
      "must": [
        {
          "prefix": {
            "sfield.exact": {
              "value": "car hire"
            }
          }
        }
      ]
    }
    

    }

  2. 尝试 match_phrase_prefix ,这也类似于前缀

  3. 示例查询:

    "query": {
        "bool": {
          "must": [
            {
              "match_phrase_prefix": {
                "sfield": {
                  "value": "car hire"
                }
              }
            }
          ]
        }
      }
    
    1. Prefix和query_string的组合, 我尝试使用query_string使用前缀和其余单词匹配第一个单词(因为有时我还需要匹配部分匹配,比如" car for air *")。
    2. 示例查询:

      "query": {
              "bool": {
                "must": [
                  {
                    "prefix": {
                      "sfield.exact": {
                        "value": "car "
                      }
                    }
                  },
                  {
                    "query_string": {
                      "default_field": "sfield",
                      "query": "car hire*",
                      "default_operator": "AND"
                    }
                  }
                ]
              }
            }
      

      第3点效果很好,但第一次执行时需要花费大量时间。 之前这个查询返回快速结果,但现在它的响应很慢.. 我使用字段类型作为" text" (用于query_string搜索)和"关键字" (对于前缀搜索),我的总数据大小约为60 GB,我的应用程序是使用PHP创建的。

      请告诉我,如果有任何方法可以在较短的时间内获取startwith +所有匹配的单词(包括部分匹配)..

      示例映射

      "mappings": {
          "ctype": {
              "_all":       { "enabled": false  },
              "properties": {
                  "id": { "type": "long" },
                  "cname": { "type": "text" },
                  "sfield": {
                      "type": "text",
                      "fields": {
                          "exact": {
                              "type": "keyword"
                          }
                      }
                  }
              }
          }
      }
      

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您只需要start with选项。因此,我建议您使用wildcard

"query": {
 "bool": {
  "must": [
    {
      "wildcard": {
        "sfield.exact": "car hire*"
      }
    }
  ]
}

请参阅Wildcard

相关问题