【问题标题】:How to use sql Like Operator in elastic search jdbc importer如何在弹性搜索 jdbc 导入器中使用 sql Like 运算符
【发布时间】:2017-01-29 22:27:47
【问题描述】:

我的项目中使用的技术(Spring mvc,elasticsearch,rest api),问题是,我的弹性搜索 jdbc 导入器无法运行类似操作符的 sql,我正在获取空数据,所以请任何人帮助我。

控制器

package com.technoshinelabs.ulearn.rest.controller;

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;

import com.technoshinelabs.ulearn.api.elasticsearch.repository.AutocompleteRepository; import com.technoshinelabs.ulearn.controller.BaseController; import com.technoshinelabs.ulearn.rest.entity.ResponseEntity; import com.technoshinelabs.ulearn.rest.util.RestStatus; import com.technoshinelabs.ulearn.util.ControllerUriConstant;

@RestController @RequestMapping(value = ControllerUriConstant.rest_autocomplete) public class RestAutocompleteController extends BaseController {

    @Autowired  AutocompleteRepository autoCompleteRepository;

    /**      *       * @param autoComplete   * @return   */     @RequestMapping(value = ControllerUriConstant.rest_autocompletesearch, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)    public ResponseEntity getOverLayWords(@RequestParam("q") String name) {         System.out.println("check search value" + " " + name);      return getResponseEntity(autoCompleteRepository.findByName(name), RestStatus.OK.getCode(), RestStatus.OK.getText());    } }

存储库

package com.technoshinelabs.ulearn.api.elasticsearch.repository;

import java.util.List;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import com.technoshinelabs.ulearn.api.elasticsearch.entities.Autocomplete;

public interface AutocompleteRepository extends ElasticsearchRepository<Autocomplete, String>{

    public List<Autocomplete> findByName(String name); }

AutoComplete.bat

 @echo off

set DIR=%~dp0 set LIB=%DIR%..\lib\* set BIN=%DIR%..\bin

REM ??? echo {^
    "type" : "jdbc",^
    "jdbc" : {^
        "url" : "jdbc:mysql://localhost:3306/ulearn_app",^
        "user" : "root",^
        "password" : "root",^
        "sql" :  "Select id as _id, id, name from course where name like '%name%'",^
        "treat_binary_as_string" : true,^
        "elasticsearch" : {^    
             "cluster" : "elasticsearch",^
             "host" : "localhost",^
             "port" : 9300^
        },^
        "index" : "ind_autocomplete"^
      }^ }^ | "%JAVA_HOME%\bin\java" -cp "%LIB%" -Dlog4j.configurationFile="%BIN%\log4j2.xml" "org.xbib.tools.Runner" "org.xbib.tools.JDBCImporter"

【问题讨论】:

  • 请格式化您的代码...

标签: java spring-mvc elasticsearch


【解决方案1】:

至少有两种方法可以做到这一点。我使用第二个,因为我不必在 jdbc 中使用 ' to \' 更改并更改 \' to ' 来测试 sql 工具中的查询。

  1. 使用\' for each '
  2. OR.. 将查询作为视图存储在数据库中。然后用 \' 调用视图而不是 sql

    • 在您的数据库中:

      CREATE VIEW your_jdbc_view AS Select id as _id, id, name from course where name like '%name%';

    • 将您的 elasticsearch 文件中的行更改为:

      “sql”:"Select * from your_jdbc_view",

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-29
    • 2011-07-08
    • 2019-08-24
    • 1970-01-01
    • 2023-03-28
    • 2012-10-11
    • 2018-11-02
    • 1970-01-01
    相关资源
    最近更新 更多