Elastic search integrates spring boot to realize search function

Keywords: MySQL Java Spring JSON

Catalog

1 define data structure by mapping of es

Default data type of Es

The mapping structure of es is defined as follows

2 import Jest dependency

3. Import mysql data for es

Configure application.properties

Define the java class corresponding to es data structure

Insert mysql data into es

Test query

4. Complex query of es

Mobile phones with the following conditions

Union processing in es

Query api

es uses jet to execute query statements

Query the encapsulation tool class of dsl

5 search service completion + sorting + highlighting

pojo defining query parameters

Controller

service

Result

1 define data structure by mapping of es

Default data type of Es

The mapping structure of es is defined as follows

PUT gmall0105
{
 "mappings": {
   "PmsSkuInfo":{
     "properties": {
       "id":{
        "type": "double"
      },
      "skuName":{
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "skuDesc":{
        "type": "text"
        , "analyzer": "ik_smart"
      },
      "catalog3Id":{
        "type": "keyword"
      },
      "price":{
        "type": "double"
      },
      "skuDefaultImg":{
        "type": "keyword",
        "index": false
      },
      "hotScore":{
        "type": "double"
      },
      "productId":{
        "type": "keyword"
      },
      "skuAttrValueList":{
        "properties": {
          "attrId":{
            "type":"keyword"
          },
          "valueId":{
            "type":"keyword"
          }
        }
      }
     } 
   }
 } 
}

Note: the string type in es (for example, keyword and text may have problems when sorting later, so it will not become a number comparison, but a string comparison. So id is defined as double)

2 import Jest dependency

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
<dependency>
   <groupId>io.searchbox</groupId>
   <artifactId>jest</artifactId>
   <version>5.3.3</version>
</dependency>


<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna -->
<dependency>
   <groupId>net.java.dev.jna</groupId>
   <artifactId>jna</artifactId>
   <version>4.5.1</version>
</dependency>

 

For jest and jna, please include the version number and part of it into GMALL parent. Spring boot starter data elastic search does not need to manage the version number. Its version follows the 1.5.10 version number of spring boot.

3. Import mysql data for es

Configure application.properties

spring.elasticsearch.jest.uris=http://192.168.67.163:9200

Define the java class corresponding to es data structure

public class PmsSkuSearchInfo implements Serializable {
    @Id
    private long id;
    private String skuName;
    private String skuDesc;
    private String catalog3Id;
    private BigDecimal price;
    private String skuDefaultImg;
    private double hotScore;
    private String productId;
    private List<PmsSkuAttrValue> skuAttrValueList;
}

Insert mysql data into es

@RunWith(SpringRunner.class)
@SpringBootTest
public class GmallSearchServiceApplicationTests {

    @Reference
    private SkuService skuService; //Query mysql

    @Autowired
    private JestClient jestClient;

    @Test
    public void put() throws IOException {
        // Query mysql data
        List<PmsSkuInfo> pmsSkuInfoList = new ArrayList<>();

        pmsSkuInfoList = skuService.getAllSku();

        // Data structure converted to es
        List<PmsSkuSearchInfo> pmsSearchSkuInfos = new ArrayList<>();

        for (PmsSkuInfo pmsSkuInfo : pmsSkuInfoList) {
            PmsSkuSearchInfo pmsSearchSkuInfo = new PmsSkuSearchInfo();

            BeanUtils.copyProperties(pmsSkuInfo,pmsSearchSkuInfo);

            pmsSearchSkuInfo.setId(Long.parseLong(pmsSkuInfo.getId()));

            pmsSearchSkuInfos.add(pmsSearchSkuInfo);

        }

        // Import es
        for (PmsSkuSearchInfo pmsSearchSkuInfo : pmsSearchSkuInfos) {
            Index put = new Index.Builder(pmsSearchSkuInfo).index("gmall0105").type("PmsSkuInfo").id(pmsSearchSkuInfo.getId()+"").build();
            jestClient.execute(put);
        }
    }
}

Be careful:

Test query

4. Complex query of es

Mobile phones with the following conditions

1. Query all names with Huawei

2 query all memory under 4 inches, 16G

Query{

Bool: {/ / filter first, then query

   Filter:{term,term}

   must:{match}

}

}

Union processing in es

Join Union in a filter

Query api

Search Search = new search.builder ("json statement of DSL"). addIndex("index name"). addType("table name"). build();

es uses jet to execute query statements

​​ @Test
    public void get() throws IOException {
       // Search Search = new search.builder ("json statement of DSL"). addIndex("index name"). addType("table name"). build();
        // Executing complex query with api
        List<PmsSkuSearchInfo> pmsSearchSkuInfos = new ArrayList<>();

        Search search = new Search.Builder("{\n" +
                "  \"query\": {\n" +
                "    \"bool\": {\n" +
                "      \"filter\": [\n" +
                "        {\n" +
                "          \"term\": {\n" +
                "          \"skuAttrValueList.valueId\": \"39\"\n" +
                "          }\n" +
                "        },\n" +
                "        {\n" +
                "          \"term\": {\n" +
                "          \"skuAttrValueList.valueId\": \"45\"\n" +
                "          }\n" +
                "        }\n" +
                "        ],\n" +
                "        \"must\": [\n" +
                "          {\n" +
                "            \"match\": {\n" +
                "              \"skuName\": \"millet\"\n" +
                "            }\n" +
                "          }\n" +
                "        ]\n" +
                "      }\n" +
                "    }\n" +
                "  }").addIndex("gmall0105").addType("PmsSkuInfo").build();

        SearchResult execute = jestClient.execute(search);

        List<SearchResult.Hit<PmsSkuSearchInfo, Void>> hits = execute.getHits(PmsSkuSearchInfo.class);

        for (SearchResult.Hit<PmsSkuSearchInfo, Void> hit : hits) {
            PmsSkuSearchInfo source = hit.source;

            pmsSearchSkuInfos.add(source);
        }
        System.out.println(pmsSearchSkuInfos.size());
    }

Query the encapsulation tool class of dsl

@Test
    public void get() throws IOException {

        // The dsl tool of jest
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // bool
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        // filter
        TermQueryBuilder termQueryBuilder = new TermQueryBuilder("skuAttrValueList.valueId","43");
        boolQueryBuilder.filter(termQueryBuilder);
        // must
        MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("skuName","millet");
        boolQueryBuilder.must(matchQueryBuilder);
        // query
        searchSourceBuilder.query(boolQueryBuilder);
        // from
        searchSourceBuilder.from(0);
        // size
        searchSourceBuilder.size(20);
        // highlight
        searchSourceBuilder.highlight(null);

        String dslStr = searchSourceBuilder.toString();

        System.err.println(dslStr);

       // Search Search = new search.builder ("json statement of DSL"). addIndex("index name"). addType("table name"). build();
        // Executing complex query with api
        List<PmsSkuSearchInfo> pmsSearchSkuInfos = new ArrayList<>();

        Search search = new Search.Builder("").addIndex("gmall0105").addType("PmsSkuInfo").build();

        SearchResult execute = jestClient.execute(search);

        List<SearchResult.Hit<PmsSkuSearchInfo, Void>> hits = execute.getHits(PmsSkuSearchInfo.class);

        for (SearchResult.Hit<PmsSkuSearchInfo, Void> hit : hits) {
            PmsSkuSearchInfo source = hit.source;

            pmsSearchSkuInfos.add(source);
        }
        System.out.println(pmsSearchSkuInfos.size());
    }

5 search service completion + sorting + highlighting

(front end code omitted)

pojo defining query parameters

public class PmsSearchParam implements Serializable {
    private String catalog3Id;
    private String keyword;
    private List<PmsSkuAttrValue> skuAttrValueList;
}

Controller

@Controller
@CrossOrigin
public class SearchController {
    @Reference
    private SearchService searchService;

    @RequestMapping("/list.html")
    public String list(PmsSearchParam pmsSearchParam, ModelMap map){ //Three level classification id, keyword
        //Call search service and return search results
        List<PmsSkuSearchInfo> pmsSkuSearchInfos = searchService.list(pmsSearchParam);
        map.put("skuLsInfoList",pmsSkuSearchInfos);
        return "list";
    }
}

service

@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    private JestClient jestClient;

    @Override
    public List<PmsSkuSearchInfo> list(PmsSearchParam pmsSearchParam) {

        String dslStr =  getSearchDsl(pmsSearchParam);

        System.err.println(dslStr);

        // Executing complex query with api
        List<PmsSkuSearchInfo> pmsSearchSkuInfos = new ArrayList<>();

        Search search = new Search.Builder(dslStr).addIndex("gmall0105").addType("PmsSkuInfo").build();

        SearchResult execute = null;
        try {
            execute = jestClient.execute(search);
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<SearchResult.Hit<PmsSkuSearchInfo, Void>> hits = execute.getHits(PmsSkuSearchInfo.class);

        for (SearchResult.Hit<PmsSkuSearchInfo, Void> hit : hits) {
            PmsSkuSearchInfo source = hit.source;

            //Get highlights
            Map<String, List<String>> highlight = hit.highlight;
            String skuName = highlight.get("skuName").get(0);
            source.getSkuName();

            pmsSearchSkuInfos.add(source);
        }

        System.err.println(pmsSearchSkuInfos.size());

        return pmsSearchSkuInfos;
    }

    private String getSearchDsl(PmsSearchParam pmsSearchParam) {
        List<PmsSkuAttrValue> skuAttrValueList = pmsSearchParam.getSkuAttrValueList();
        String keyword = pmsSearchParam.getKeyword();
        String catalog3Id = pmsSearchParam.getCatalog3Id();

        // The dsl tool of jest
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // bool
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        // filter
        if (StringUtils.isNotBlank(catalog3Id)){
            TermQueryBuilder termQueryBuilder = new TermQueryBuilder("catalog3Id",catalog3Id);
            boolQueryBuilder.filter(termQueryBuilder);
        }
        if (skuAttrValueList != null) {
            for (PmsSkuAttrValue pmsSkuAttrValue : skuAttrValueList) {
                TermQueryBuilder termQueryBuilder = new TermQueryBuilder("skuAttrValueList.valueId", pmsSkuAttrValue.getValueId());
                boolQueryBuilder.filter(termQueryBuilder);
            }
        }
        // must
        if (StringUtils.isNotBlank(keyword)) {
            MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("skuName", keyword);
            boolQueryBuilder.must(matchQueryBuilder);
        }
        // query
        searchSourceBuilder.query(boolQueryBuilder);
        // from
        searchSourceBuilder.from(0);
        // size
        searchSourceBuilder.size(20);
        // highlight: highlight
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<span style='color:red;'>");//prefix
        highlightBuilder.field("skuName");
        highlightBuilder.postTags("</span>");//Suffix
        searchSourceBuilder.highlight(highlightBuilder);
        //sort
        searchSourceBuilder.sort("id", SortOrder.DESC);

        String dslStr = searchSourceBuilder.toString();

        return dslStr;
    }
}

 

 

 

 

96 original articles published, 16 praised, 20000 visitors+
Private letter follow

Posted by dubc07 on Sat, 11 Jan 2020 00:08:20 -0800