ElasticSearch-用于查询两个具有不同字段名称的索引的Java API

时间:2018-07-25 15:34:56

标签: java elasticsearch elastic-stack

能够使用单个查询字段从单个索引中获取文档。请找到以下代码。

SearchRequest searchCatalogValueRequest = new SearchRequest(CATALOG_INDEX); 
SearchSourceBuilder catalogSearchSourceBuilder = new SearchSourceBuilder();
searchCatalogValueRequest.types(TYPE);

 QueryBuilder qb1 = QueryBuilders.boolQuery()
             .must(QueryBuilders.matchQuery("catalog_value", keyword))
             .filter(QueryBuilders.termQuery("locale", USER_LOCALE));

但是现在,我想从具有两个查询字段的两个索引中获取文档。像下面的代码片段。

SearchRequest searchCatalogValueRequest = new SearchRequest(CATALOG_INDEX, PRODUCT_INDEX); 
SearchSourceBuilder catalogSearchSourceBuilder = new SearchSourceBuilder();
searchCatalogValueRequest.types(TYPE);

QueryBuilder qb1 = QueryBuilders.boolQuery()
     .must(QueryBuilders.matchQuery("catalog_value", keyword)) // here i want to search with product_value field also.
     .filter(QueryBuilders.termQuery("locale", USER_LOCALE));

我想在catalog_valueproduct_value的两个字段中查询。不确定如何实现。

请找到我的地图详细信息 产品字段映射

   PUT product
   {
      "settings": {
        "analysis": {
        "analyzer": {
           "value_analyzer": {
           "type": "custom",
            "tokenizer": "whitespace",
           "char_filter": [
            "html_strip"
           ],
           "filter": ["lowercase", "asciifolding"]
         }
       }
     }
    },
    "mappings": {
      "doc": {
        "properties": {
          "product_value": {
            "type": "text",
            "analyzer": "value_analyzer"
          },

         "locale":{
         "type" : "keyword"
         }

        }
       }
      }
}

目录字段映射:

PUT catalog
 {
      "settings": {
        "analysis": {
        "analyzer": {
           "value_analyzer": {
           "type": "custom",
            "tokenizer": "whitespace",
           "char_filter": [
            "html_strip"
           ],
           "filter": ["lowercase", "asciifolding"]
         }
       }
     }
    },
    "mappings": {
      "doc": {
        "properties": {
          "catalog_attr_value": {
            "type": "text",
            "analyzer": "value_analyzer"
          },
         "locale":{
         "type":"keyword"
         }

        }
       }
      }
    }

0 个答案:

没有答案