Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
wwwanlingxiao
mall
Commits
a76e46e5
Commit
a76e46e5
authored
Jun 22, 2018
by
zhh
Browse files
集成ik分析器
parent
9523533c
Changes
4
Hide whitespace changes
Inline
Side-by-side
mall-search/src/main/java/com/macro/mall/search/domain/EsProduct.java
View file @
a76e46e5
package
com.macro.mall.search.domain
;
package
com.macro.mall.search.domain
;
import
com.macro.mall.model.PmsProductAttributeValue
;
import
com.macro.mall.model.PmsProductAttributeValue
;
import
org.springframework.data.annotation.Id
;
import
org.springframework.data.elasticsearch.annotations.Document
;
import
org.springframework.data.elasticsearch.annotations.Document
;
import
org.springframework.data.elasticsearch.annotations.Field
;
import
org.springframework.data.elasticsearch.annotations.FieldIndex
;
import
org.springframework.data.elasticsearch.annotations.FieldType
;
import
java.io.Serializable
;
import
java.io.Serializable
;
import
java.math.BigDecimal
;
import
java.math.BigDecimal
;
...
@@ -11,18 +15,25 @@ import java.util.List;
...
@@ -11,18 +15,25 @@ import java.util.List;
* 搜索中的商品信息
* 搜索中的商品信息
* Created by macro on 2018/6/19.
* Created by macro on 2018/6/19.
*/
*/
@Document
(
indexName
=
"pms"
,
type
=
"product"
)
@Document
(
indexName
=
"pms"
,
type
=
"product"
,
shards
=
1
,
replicas
=
0
)
public
class
EsProduct
implements
Serializable
{
public
class
EsProduct
implements
Serializable
{
private
static
final
long
serialVersionUID
=
-
1L
;
private
static
final
long
serialVersionUID
=
-
1L
;
@Id
private
Long
id
;
private
Long
id
;
@Field
(
index
=
FieldIndex
.
not_analyzed
,
type
=
FieldType
.
String
)
private
String
productSn
;
private
String
productSn
;
private
Long
brandId
;
private
Long
brandId
;
@Field
(
index
=
FieldIndex
.
not_analyzed
,
type
=
FieldType
.
String
)
private
String
brandName
;
private
String
brandName
;
private
Long
productCategoryId
;
private
Long
productCategoryId
;
@Field
(
index
=
FieldIndex
.
not_analyzed
,
type
=
FieldType
.
String
)
private
String
productCategoryName
;
private
String
productCategoryName
;
private
String
pic
;
private
String
pic
;
@Field
(
analyzer
=
"ik_max_word"
,
type
=
FieldType
.
String
)
private
String
name
;
private
String
name
;
@Field
(
analyzer
=
"ik_max_word"
,
type
=
FieldType
.
String
)
private
String
subTitle
;
private
String
subTitle
;
@Field
(
analyzer
=
"ik_max_word"
,
type
=
FieldType
.
String
)
private
String
keywords
;
private
String
keywords
;
private
BigDecimal
price
;
private
BigDecimal
price
;
private
Integer
sale
;
private
Integer
sale
;
...
...
mall-search/src/main/java/com/macro/mall/search/service/impl/EsProductServiceImpl.java
View file @
a76e46e5
...
@@ -105,11 +105,11 @@ public class EsProductServiceImpl implements EsProductService {
...
@@ -105,11 +105,11 @@ public class EsProductServiceImpl implements EsProductService {
}
}
//搜索
//搜索
FunctionScoreQueryBuilder
functionScoreQueryBuilder
=
QueryBuilders
.
functionScoreQuery
()
FunctionScoreQueryBuilder
functionScoreQueryBuilder
=
QueryBuilders
.
functionScoreQuery
()
.
add
(
QueryBuilders
.
match
Phrase
Query
(
"name"
,
keyword
),
.
add
(
QueryBuilders
.
matchQuery
(
"name"
,
keyword
),
ScoreFunctionBuilders
.
weightFactorFunction
(
1000
))
ScoreFunctionBuilders
.
weightFactorFunction
(
1000
))
.
add
(
QueryBuilders
.
match
Phrase
Query
(
"subTitle"
,
keyword
),
.
add
(
QueryBuilders
.
matchQuery
(
"subTitle"
,
keyword
),
ScoreFunctionBuilders
.
weightFactorFunction
(
500
))
ScoreFunctionBuilders
.
weightFactorFunction
(
500
))
.
add
(
QueryBuilders
.
match
Phrase
Query
(
"keywords"
,
keyword
),
.
add
(
QueryBuilders
.
matchQuery
(
"keywords"
,
keyword
),
ScoreFunctionBuilders
.
weightFactorFunction
(
200
))
ScoreFunctionBuilders
.
weightFactorFunction
(
200
))
.
scoreMode
(
"sum"
).
setMinScore
(
10
f
);
.
scoreMode
(
"sum"
).
setMinScore
(
10
f
);
if
(
StringUtils
.
isEmpty
(
keyword
))
{
if
(
StringUtils
.
isEmpty
(
keyword
))
{
...
...
mall-search/src/main/resources/dao/EsProductDao.xml
View file @
a76e46e5
...
@@ -23,7 +23,7 @@
...
@@ -23,7 +23,7 @@
p.recommand_status recommandStatus,
p.recommand_status recommandStatus,
p.stock stock,
p.stock stock,
p.promotion_type promotionType,
p.promotion_type promotionType,
P.keywords keywords
P.keywords keywords
,
p.sort sort,
p.sort sort,
a.id attr_id,
a.id attr_id,
a.value attr_value,
a.value attr_value,
...
...
mall-search/src/test/java/com/macro/mall/search/MallSearchApplicationTests.java
View file @
a76e46e5
...
@@ -2,19 +2,24 @@ package com.macro.mall.search;
...
@@ -2,19 +2,24 @@ package com.macro.mall.search;
import
com.macro.mall.search.dao.EsProductDao
;
import
com.macro.mall.search.dao.EsProductDao
;
import
com.macro.mall.search.domain.EsProduct
;
import
com.macro.mall.search.domain.EsProduct
;
import
com.macro.mall.search.repository.EsProductRepository
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.data.elasticsearch.core.ElasticsearchTemplate
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
@RunWith
(
SpringRunner
.
class
)
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
@SpringBootTest
public
class
MallSearchApplicationTests
{
public
class
MallSearchApplicationTests
{
@Autowired
@Autowired
private
EsProductDao
productDao
;
private
EsProductDao
productDao
;
@Autowired
private
ElasticsearchTemplate
elasticsearchTemplate
;
@Test
@Test
public
void
contextLoads
()
{
public
void
contextLoads
()
{
}
}
...
@@ -23,5 +28,11 @@ public class MallSearchApplicationTests {
...
@@ -23,5 +28,11 @@ public class MallSearchApplicationTests {
List
<
EsProduct
>
esProductList
=
productDao
.
getAllEsProductList
(
null
);
List
<
EsProduct
>
esProductList
=
productDao
.
getAllEsProductList
(
null
);
System
.
out
.
print
(
esProductList
);
System
.
out
.
print
(
esProductList
);
}
}
@Test
public
void
testEsMapping
(){
elasticsearchTemplate
.
putMapping
(
EsProduct
.
class
);
Map
mapping
=
elasticsearchTemplate
.
getMapping
(
EsProduct
.
class
);
System
.
out
.
println
(
mapping
);
}
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment