elasticsearch 数组包含查询

数组类型

默认情况下,任何字段都可以包含零个或多个值,当包含多个值时,它就表示array了。但是,数组中的所有值必须具有相同的数据类型(要么同为字符串,要么同为整型,不能数组中一些值为字符串,另一些值为整型)。

在Kinbana中使用:

mapping映射创建索引:

PUT /user_index
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      },
      "loves": {
        "type": "keyword"
      }
    }
  }
}

添加数据:

PUT /user_index/_doc/1
{
  "name": "孙悟空",
  "loves":["打八戒","吃桃子","紫霞仙子"]
}

查询其中一段信息:

GET /user_index/_search
{
  "query": {
        "term": {
          "loves": {
            "value": "紫霞仙子"
          }
      }
    }
}

查询结果:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.39556286,
    "hits" : [
      {
        "_index" : "user_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.39556286,
        "_source" : {
          "name" : "孙悟空",
          "loves" : [
            "打八戒",
            "吃桃子",
            "紫霞仙子"
          ]
        }
      }
    ]
  }
}

在代码中使用:

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
class SearchApplicationTests {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    /**************************创建索引***************************/
    @Test
    public void CreateIndex() throws IOException {
        CreateIndexRequest request = new CreateIndexRequest("user_index");
        request.source(UserMappings.MAPPING_TEMPLATE, XContentType.JSON);
        CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
    }
    /************************查询索引是否存在**********************/
    @Test
    public void ExitIndex() throws IOException{
        GetIndexRequest request = new GetIndexRequest("user_index");
        boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
        if (exists){
            log.info("存在索引");
        }
    }
    /**************************添加数据***************************/
    @Test
    public void CreateDoc() throws IOException{
        List loves = new ArrayList<>();
        loves.add("打八戒");
        loves.add("吃桃子");
        loves.add("紫霞仙子");
        User user = new User();
        user.setName("孙悟空");
        user.setLoves(loves);
        IndexRequest request = new IndexRequest("user_index");
        request.id("1");
        request.timeout(TimeValue.timeValueSeconds(5));
        request.source(JSON.toJSONString(user),XContentType.JSON);
        IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
        System.out.println(response);
    }
    /**************************查询文档***************************/
    @Test
    public void SearchDoc() throws IOException{
        SearchRequest request = new SearchRequest("user_index");
        request.source().query(QueryBuilders.termQuery("loves","紫霞仙子"));
        SearchResponse search = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        for (SearchHit hit : hits) {
            String json = hit.getSourceAsString();
            User user = JSON.parseObject(json, User.class);
            log.info("用户信息:{}",user);
        }
    }
}

打印结果:

elasticsearch 数组包含查询_第1张图片

 

你可能感兴趣的:(elasticsearch,大数据,搜索引擎)