2015-10-30 47 views
0

我有以下JSON:如何在elasticsearch中訪問嵌套的json?

metadata: { 
    authors: [ 

    ], 
    links: [ 
     { 
      href: "http://www.latimes.com/opinion/readersreact/la-le-1028-wednesday-meat-cancer-20151028-story.html#navtype=outfit", 
      value: "Why hot dogs and bacon aren't as dangerous as cigarettes" 
     }, 
     { 
      href: "http://www.latimes.com/opinion/readersreact/la-le-1028-wednesday-porter-ranch-lausd-20151028-story.html#navtype=outfit", 
      value: "LAUSD school in Porter Ranch shows the importance of parent involvement" 
     }, 
     { 
      href: "http://www.latimes.com/opinion/readersreact/la-le-1028-wednesday-billboards-20151028-story.html#navtype=outfit", 
      value: "Maine and Vermont show L.A. what life is like without billboards" 
     }, 
     { 
      href: "http://www.latimes.com/opinion/readersreact/la-le-1028-wednesday-broad-beach-20151028-story.html#navtype=outfit", 
      value: "Malibu beach-front homeowners, meet King Canute" 
     } 
    ] 
}, 

我想只搜索metadata.links.value在elasticsearch:

requestBuilder.setQuery(QueryBuilders.matchQuery("metadata.links.value", "Malibu")); 

但不幸的是,這並不工作。當我輸入一個值時,我獲得0次點擊。

我到底做錯了什麼?


更新:

這裏是我完整的代碼

public List<ArticleExtraction> search(String searchQuery, SearchProvider searchProvider) { 
    TransportClient client = searchProvider.getClient(); 
    Map<String, String> query = new HashMap<>(); 
    ArrayList<String> singleQuery = new ArrayList<>(); 
    if (searchQuery.length() > 0 && searchQuery.contains(":")) { 
     String[] queries = searchQuery.split(","); 
     for (String q : queries) { 
      String[] jsonQuery = q.split(":"); 
      query.put(jsonQuery[0], jsonQuery[1]); 
     } 
    } else { 
     String[] queries = searchQuery.split(","); 
     for (String q : queries) { 
      singleQuery.add(q); 
     } 
    } 
    SearchRequestBuilder requestBuilder = client.prepareSearch("crawlbot") 
      .setTypes("item") 
      .setSize(100); 

    for (Map.Entry<String, String> e : query.entrySet()) { 
     requestBuilder.setQuery(QueryBuilders.matchQuery(e.getKey(), e.getValue())); 
    } 
    for (String q : singleQuery) { 
     requestBuilder.setQuery(QueryBuilders.queryStringQuery(q)); 
    } 
    SearchResponse response = requestBuilder.execute().actionGet(); 

    List<ArticleExtraction> articles = new ArrayList<>(); 

    SearchHit[] hits = response.getHits().getHits(); 
    for (SearchHit hit : hits) { 
     String sourceAsString = hit.getSourceAsString(); 
     if (sourceAsString != null) { 
      JsonObject json = new JsonParser().parse(sourceAsString).getAsJsonObject(); 
      if (json.has("article")) { 
       Gson gson = new Gson(); 
       articles.add(gson.fromJson(json.get("article"), ArticleExtraction.class)); 
      } 
     } 
    } 

    return articles; 

說明:

的SEARCHQUERY的輸入可能是這樣的:

metadata.links.value :馬里布

或者如果它是一個singlequery:馬里布

我做了一些代碼,所以這兩個查詢可以得到接受

映射(如果這得到SRY大)

mappings: { 
    item: { 
     properties: { 
      article: { 
       properties: { 
        description: { 
         type: "string" 
        }, 
        description_html: { 
         type: "string" 
        }, 
        entities: { 
         properties: { 
          count: { 
           type: "long" 
          }, 
          meta: { 
           type: "object" 
          }, 
          name: { 
           type: "string" 
          }, 
          type: { 
           type: "string" 
          } 
         } 
        }, 
        favicon_url: { 
         type: "string" 
        }, 
        images: { 
         properties: { 
          colors: { 
           properties: { 
            color: { 
             type: "long" 
            } 
           } 
          }, 
          entropy: { 
           type: "double" 
          }, 
          height: { 
           type: "long" 
          }, 
          url: { 
           type: "string" 
          }, 
          width: { 
           type: "long" 
          } 
         } 
        }, 
        keywords: { 
         properties: { 
          label: { 
           type: "string" 
          }, 
          score: { 
           type: "double" 
          } 
         } 
        }, 
        language: { 
         type: "string" 
        }, 
        metadata: { 
         properties: { 
          authors: { 
           properties: { 
            name: { 
             type: "string" 
            } 
           } 
          }, 
          links: { 
           properties: { 
            href: { 
             type: "string" 
            }, 
            value: { 
             type: "string" 
            } 
           } 
          }, 
          twitter: { 
           type: "string" 
          } 
         } 
        }, 
        provider_display: { 
         type: "string" 
        }, 
        provider_name: { 
         type: "string" 
        }, 
        provider_url: { 
         type: "string" 
        }, 
        published: { 
         type: "string" 
        }, 
        published_long: { 
         type: "long" 
        }, 
        summary: { 
         type: "string" 
        }, 
        title: { 
         type: "string" 
        }, 
        url: { 
         type: "string" 
        } 
       } 
      }, 
      id: { 
       properties: { 
        _inc: { 
         type: "long" 
        }, 
        _machine: { 
         type: "long" 
        }, 
        _new: { 
         type: "boolean" 
        }, 
        _time: { 
         type: "long" 
        } 
       } 
      }, 
      job: { 
       properties: { 
        api: { 
         type: "long" 
        }, 
        crawl_depth: { 
         type: "long" 
        }, 
        max_pages: { 
         type: "long" 
        }, 
        name: { 
         type: "string" 
        }, 
        status: { 
         type: "long" 
        }, 
        url: { 
         type: "string" 
        }, 
        userid: { 
         type: "long" 
        } 
       } 
      }, 
      query: { 
       properties: { 
        match: { 
         properties: { 
          name: { 
           type: "string" 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
}, 
+0

我試過了 - 不工作SRY。 我現在把整個代碼在那裏 - 如果某事。目前還不清楚請告訴我:) 只注意到它的愚蠢不斷初始化GSON - 請忽略;) –

+0

嘗試的第一件事是「名仕」,而不是「名仕」(見:[標準分析儀(https://開頭www.elastic.co/search?q=standard+analyzer))。如果這不起作用,你也應該發佈你的映射,因爲它的配置如何與你所看到的行爲有關。 –

+0

我沒有簡單的方法來設置測試Java代碼,很抱歉;我不做Java編程。如果您可以直接從Elasticsearch向我展示JSON映射,我可能會提供幫助。 –

回答

2

元數據包含在文章根內目的。

因此,你的查詢應該被解釋爲:

QueryBuilders.matchQuery("article.metadata.links.value"‌​, "Malibu");