Elasticsearch simple包含使用Java API的搜索

时间:2015-08-02 10:51:15

标签: elasticsearch

我正在尝试使用Elastic Search的Java API,并努力寻找合适的查询构建器来使用。

我的JSON有以下字段:名称,描述等。

我的JSON(一个样本):

{ 
    "_id" : { "$oid" : "5160988c96cc620a5db6dafa" },
    "name" : "Spinach Strata Recipe", 
    "ingredients" : "8 ounces day-old crusty bread, such as pain au levain, large dice (about 6 cups)\n2 cups coarsely chopped baby spinach leaves (about 3 1/2 ounces)\n3/4 cup crumbled provolone cheese (about 3 3/4 ounces)\n2 tablespoons extra-virgin olive oil, plus more for coating the pan\n1 tablespoon finely grated lemon zest (from about 1 medium lemon)\n2 teaspoons Dijon mustard\n1/8teaspoon kosher salt\n1/2 teaspoon freshly ground black pepper\n6 ex   large eggs\n2 cups whole milk\n1/2 teaspoon finely chopped fresh oregano leaves", 
    "url" : "http://www.chow.com/users/recipes/29915-spinach-strata",     
    "image" : null,
    "ts" : { "$date" : 1365285004038 },
    "cookTime" : null,
    "source" : "chow",
    "recipeYield" : "6 servings",
    "prepTime" : null,
    "description" : "Aside from being comforting and filling, stratas are really easy to make. Whisked eggs and milk are poured over bread and your favorite fillings, then everything..." 
}

这就是我索引的方式:

Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elasticsearch_sai").build();
    final TransportClient client = new TransportClient(settings);
    Client _client = client.addTransportAddress(new InetSocketTransportAddress("localhost", Integer.parseInt("9300")));
    reader.lines().limit(10000).forEach(json -> {
        IndexResponse response = _client.prepareIndex("openrecipe", "recipe")
                .setSource(json)
                .execute()
                .actionGet();
     System.out.println("Indexed: "+response.getId()+" --> "+response.isCreated());
    });

    _client.admin().indices().prepareRefresh().execute().actionGet()   ;

我想搜索名称字段中包含单词三明治的所有文档(不区分大小写)。

但这不起作用:

   SearchResponse response = _client.prepareSearch("openrecipe")        
       .setTypes("recipe")                                          
       .setFrom(0).setSize(60)                                      
       .setSearchType(SearchType.QUERY_AND_FETCH)                   
       .setQuery(QueryBuilders.termQuery("name", "sandwich"))       
       .execute()                                                   
       .actionGet();                                                

0 个答案:

没有答案