弹性搜索 - 使用动态对象和通配符进行查询

时间:2017-09-02 10:59:30

标签: elasticsearch

我的数据格式如下:

    public static void divideFile(File f) {
    int partCounter = 1;//I like to name parts from 001, 002, 003, 
    //you can change it to 0 if you want 000, 001,003 

    int sizeOfFiles = 1024 * 1024;// 1MB
    byte[] buffer = new byte[sizeOfFiles];

    String fileName = f.getName();

    //try-with-resources to ensure closing stream
    try (FileInputStream fis = new FileInputStream(f);
         BufferedInputStream bis = new BufferedInputStream(fis)) {

        int bytesAmount = 0;
        while ((bytesAmount = bis.read(buffer)) > 0) {
            //write each chunk of data into separate file with different number in name
            String filePartName = String.format("%s.%03d", fileName, partCounter++);
            File newFile = new File(f.getParent(), filePartName);
            try (FileOutputStream out = new FileOutputStream(newFile)) {
                out.write(buffer, 0, bytesAmount);
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    }

我需要通过弹性搜索来搜索数据,这将查询s_id =?以及data_detail对象中可用的任何文本。示例s_id = 121211 AND ABC。我需要在data_detail对象上使用通配符。

data_detail对象的键不固定。

提前致谢。

2 个答案:

答案 0 :(得分:1)

我会考虑使用带有multi_match的bool查询和这样的术语查询。我没有对此进行测试,但我认为这些方面的内容应该可行。

.pure-g.homepage
  = react_component("SectionA", {foo: @bar}, class: "pure-u-1")
  = react_component("SectionB", {foo: @bar}, class: "pure-u-1")
  = react_component("SectionC", {foo: @bar, foo2: @bar2, foo3: @bar3}, class: "pure-u-1")

答案 1 :(得分:0)

使用以下查询解决了这个问题:

{
  "query": {
    "bool": {
      "must": [
        {
           "query_string":{
              "fields":["data_detail.*"],
              "query": "*str*",
              "analyze_wildcard":true
            }
          },
        {
          "term": {
            "s_id": {
              "value": "121211"
            }
          }
        }
      ]
    }
  }
 }